MAX(expression) returns the largest non-null value in the group; in SQL it is max(). With two arguments, MAX(a, b) compares them on each row, like SQL greatest(), except that Tableau returns null when either argument is null. DuckDB's greatest() skips nulls, so it needs a CASE.
Updated · Checked on the xmetric engine
What is the syntax?
MAX(expression) orMAX(expression1, expression2)
expression
a number, string or date. Strings compare alphabetically, dates by time.
expression1, expression2
two values of the same type, compared row by row.
Returns the largest value. As an aggregate: null when every value is null. With two arguments: a row-level value, null if either 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.
What is the largest sale in each region?
Tableau
MAX([Sales])
SQL (DuckDB)
SELECT Region, max(Sales) AS result
FROM input
GROUPBY Region
ORDERBY Region
East's largest sale is 300. North has no sales values: null.
Region
Result
East
300
North
null
South
80
West
400
✓ The Tableau formula and the SQL return the same 4 rows.
Which is largest on each row, sales or units?
Tableau
MAX([Sales], [Units])
SQL (DuckDB)
SELECTCASEWHEN Sales ISNULLOR Units ISNULLTHENNULLELSEgreatest(Sales, Units)
ENDAS result
FROM input
ORDERBY id
Row-level: one result per line. Row 2 has no sales and row 7 no units, so both return null.
Sales
Units
Result
100
2
100
3
null
300
5
300
50
1
50
100
2
100
200
4
200
250
null
150
3
150
400
6
400
1
null
2
null
80
2
80
✓ The Tableau formula and the SQL return the same 12 rows.
Where does a straight translation go wrong?
Common mistake
Why does greatest() return a number where Tableau returns null?
DuckDB's greatest() ignores null arguments, so row 2 gets its 3 units back. Tableau's two-argument MAX returns null if either side is null. Snowflake's and BigQuery's versions return null too, so this trap is DuckDB's.
Tableau
MAX([Sales], [Units])
Tempting SQL
SELECTgreatest(Sales, Units) AS result
FROM input
ORDERBY id
SQL that matches
SELECTCASEWHEN Sales ISNULLOR Units ISNULLTHENNULLELSEgreatest(Sales, Units)
ENDAS result
FROM input
ORDERBY id
Sales
Units
Tableau
Tempting SQL
SQL that matches
100
2
100
100
100
3
null
3
null
300
5
300
300
300
50
1
50
50
50
100
2
100
100
100
200
4
200
200
200
250
null
250
null
150
3
150
150
150
400
6
400
400
400
1
null
1
null
2
null
2
null
80
2
80
80
80
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
What does MAX return for nulls, strings and two arguments?
North's sales are all null; the North lines also have no product, so the string comparison is null there. Each result column is one mark, by Region.
Case
Tableau, then SQL (DuckDB)
Desk
Desk
Desk
Lamp
Desk
Desk
Desk
Desk
Desk
Desk
all-null group
MAX([Sales])Region, max(Sales)
300
null
80
400
two strings
MAX([Product], 'Desk')CASE WHEN Product IS NULL OR 'Desk' IS NULL THEN NULL ELSE greatest(Product, 'Desk') END
Desk
Desk
Desk
Lamp
Desk
Desk
Desk
Desk
Desk
null
null
Desk
two numbers
MAX([Sales], [Units])CASE WHEN Sales IS NULL OR Units IS NULL THEN NULL ELSE greatest(Sales, Units) END
100
null
300
50
100
200
null
150
400
null
null
80
✓ 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
SELECTGREATEST(Sales, Units) AS result
FROM input
ORDERBY id