Tableau function Aggregate

Tableau MAX to SQL

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)  or  MAX(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
GROUP BY Region
ORDER BY Region

East's largest sale is 300. North has no sales values: null.

RegionResult
East300
Northnull
South80
West400

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)

SELECT
  CASE
    WHEN Sales IS NULL OR Units IS NULL THEN NULL
    ELSE greatest(Sales, Units)
    END AS result
FROM input
ORDER BY id

Row-level: one result per line. Row 2 has no sales and row 7 no units, so both return null.

SalesUnitsResult
1002100
3null
3005300
50150
1002100
2004200
250null
1503150
4006400
1null
2null
80280

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

SELECT greatest(Sales, Units) AS result
FROM input
ORDER BY id

SQL that matches

SELECT
  CASE
    WHEN Sales IS NULL OR Units IS NULL THEN NULL
    ELSE greatest(Sales, Units)
    END AS result
FROM input
ORDER BY id
SalesUnitsTableauTempting SQLSQL that matches
1002100100100
3null3null
3005300300300
501505050
1002100100100
2004200200200
250null250null
1503150150150
4006400400400
1null1null
2null2null
802808080

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.

CaseTableau, then SQL (DuckDB)DeskDeskDeskLampDeskDeskDeskDeskDeskDesk
all-null groupMAX([Sales])Region, max(Sales)300null80400
two stringsMAX([Product], 'Desk')CASE WHEN Product IS NULL OR 'Desk' IS NULL THEN NULL ELSE greatest(Product, 'Desk') ENDDeskDeskDeskLampDeskDeskDeskDeskDesknullnullDesk
two numbersMAX([Sales], [Units])CASE WHEN Sales IS NULL OR Units IS NULL THEN NULL ELSE greatest(Sales, Units) END100null30050100200null150400nullnull80

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 GREATEST(Sales, Units) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT GREATEST(Sales, Units) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own MAX 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 MAX?

Does MAX work on text?

Yes, alphabetically. MAX([Product]) per region gives the last product name; West sells only Desk; East's last product alphabetically is Lamp.

Is MAX(a, b) an aggregate?

No. With two arguments it is row-level, so it can sit inside SUM or AVG. To compare two aggregates, aggregate first: MAX(SUM([Sales]), SUM([Units])).

Do the two arguments need the same type?

Yes. Tableau raises an error for a number against a string; cast one side with STR() or FLOAT() first.

Where is this documented?