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,
CASEWHENcount(DISTINCT Product) >1THEN'*'ELSEmin(Product)
ENDAS result
FROM input
GROUPBY Region
ORDERBY Region
West and South sell one product each, so ATTR shows it. East sells three, so ATTR shows *. North names none: null.
Region
Result
East
*
North
null
South
Chair
West
Desk
✓ 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
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region,
CASEWHENcount(DISTINCT Product) >1THEN'*'ELSEmin(Product)
ENDAS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
*
Chair
*
North
null
null
null
South
Chair
Chair
Chair
West
Desk
Desk
Desk
✓ 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,
CASEWHENcount(DISTINCT Product) =1THENmin(Product)
ELSE'*'ENDAS result
FROM input
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region,
CASEWHENcount(DISTINCT Product) >1THEN'*'ELSEmin(Product)
ENDAS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
*
*
*
North
null
*
null
South
Chair
Chair
Chair
West
Desk
Desk
Desk
✓ 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.
Case
Tableau, then SQL (DuckDB)
East
North
South
West
all-null group
ATTR([Product])Region, CASE WHEN count(DISTINCT Product) > 1 THEN '*' ELSE min(Product) END
*
null
Chair
Desk
✓ 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,
CASEWHENCOUNT(DISTINCT Product) >1THEN'*'ELSEMIN(Product)
ENDAS result
FROM input
GROUPBY Region
ORDERBY Region
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.