Tableau function Aggregate

Tableau ATTR to SQL

ATTR(expression) returns the value if every non-null row in the group has the same one, and an asterisk (*) if they differ. Nulls are ignored. SQL has no ATTR: return '*' when the group has more than one distinct value, and min() otherwise, which is null for an all-null group.

Updated · Checked on the xmetric engine

What is the syntax?

ATTR(expression)
expression
a field of any type, usually a dimension you want to show without splitting the marks by it.

Returns the single value of the expression in the group, * when there is more than one, null when every value is null.

What does it return, and what is the SQL?

Each example runs on the same 12 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

Which product does each region sell?

Tableau

ATTR([Product])

SQL (DuckDB)

SELECT
  Region,
  CASE
    WHEN count(DISTINCT Product) > 1 THEN '*'
    ELSE min(Product)
    END AS result
FROM input
GROUP BY Region
ORDER BY Region

West and South sell one product each, so ATTR shows it. East sells three, so ATTR shows *. North names none: null.

RegionResult
East*
Northnull
SouthChair
WestDesk

The Tableau formula and the SQL return the same 4 rows.

Where does a straight translation go wrong?

Common mistake

Why does min(Product) show Chair for East?

MIN always picks a value, even when the group has several. ATTR's point is to tell you there are several: East is *, not Chair.

Tableau

ATTR([Product])

Tempting SQL

SELECT Region, min(Product) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT
  Region,
  CASE
    WHEN count(DISTINCT Product) > 1 THEN '*'
    ELSE min(Product)
    END AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East*Chair*
Northnullnullnull
SouthChairChairChair
WestDeskDeskDesk

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

Common mistake

Why does count(DISTINCT x) = 1 disagree on North?

Testing count(DISTINCT Product) = 1 sends a group with no values to the * branch. ATTR ignores nulls and returns null for North.

Tableau

ATTR([Product])

Tempting SQL

SELECT
  Region,
  CASE
    WHEN count(DISTINCT Product) = 1 THEN min(Product)
    ELSE '*'
    END AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT
  Region,
  CASE
    WHEN count(DISTINCT Product) > 1 THEN '*'
    ELSE min(Product)
    END AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East***
Northnull*null
SouthChairChairChair
WestDeskDeskDesk

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

What does ATTR return when every value is null?

North's lines name no product. ATTR returns null there, not *. Each result column is one mark, by Region.

CaseTableau, then SQL (DuckDB)EastNorthSouthWest
all-null groupATTR([Product])Region, CASE WHEN count(DISTINCT Product) > 1 THEN '*' ELSE min(Product) END*nullChairDesk

On every row, each Tableau formula and its SQL return the same value.

How do I write it in Snowflake or BigQuery?

The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.

Snowflake

SELECT
  Region,
  CASE
    WHEN COUNT(DISTINCT Product) > 1 THEN '*'
    ELSE MIN(Product)
    END AS result
FROM input
GROUP BY Region
ORDER BY Region

Snowflake documentation

BigQuery

SELECT
  Region,
  CASE
    WHEN COUNT(DISTINCT Product) > 1 THEN '*'
    ELSE MIN(Product)
    END AS result
FROM input
GROUP BY Region
ORDER BY Region

BigQuery documentation

What does your own ATTR formula return?

Edit the formula and run it on the 12 sample rows. Fields: [id], [Region], [Product], [Sales], [Units].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about ATTR?

Why does my view show a *?

A field is aggregated with ATTR and the mark covers several of its values. Add the field to the view, or use MIN or MAX if any one value will do.

Does ATTR ignore nulls?

Yes. Tableau's help says null values are ignored, so a group with one product and some empty lines still shows that product. min() and max() skip nulls the same way.

What is the SQL for ATTR on a number?

The same CASE, with the value cast to text: CASE WHEN count(DISTINCT x) > 1 THEN '*' ELSE CAST(min(x) AS VARCHAR) END, since one column can't hold both numbers and text.

Where is this documented?