Tableau function Number

Tableau GREATEST to SQL

GREATEST(expr1, expr2, ...) returns the largest of its arguments and skips nulls, so a row with one missing value still gets an answer. DuckDB's greatest() skips nulls the same way. Snowflake's and BigQuery's GREATEST return null if any argument is null: use GREATEST_IGNORE_NULLS in Snowflake and coalesce the arguments in BigQuery. For the null-propagating version in Tableau, see GREATESTPROPAGATENULL.

Updated · Checked on the xmetric engine

What is the syntax?

GREATEST(expr1, expr2, ...)
expr1, expr2, ...
two or more numbers or numeric expressions.

Returns the largest of the non-null arguments. Null only if every argument is null.

What does it return, and what is the SQL?

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

Which is larger, Value or Other?

Tableau

GREATEST([Value], [Other])

SQL (DuckDB)

SELECT greatest(Value, Other) AS result
FROM input
ORDER BY id

Row 3 has no Other, so it returns the value, 0.125; row 6 has no Value, so it returns Other, 2.

ValueOtherResult
2.544
−2.5−3−2.5
0.130.13
−0.4−1−0.4
3.7588
22
00.50.5
161616

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

How do I clamp values at zero?

Tableau

GREATEST([Value], 0)

SQL (DuckDB)

SELECT greatest(Value, 0) AS result
FROM input
ORDER BY id

Negatives become 0. Row 6 has no value, and the result is 0, the one argument left.

ValueResult
2.52.5
−2.50
0.130.13
−0.40
3.753.75
0
00
1616

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

Where does a straight translation go wrong?

Common mistake

Why does a hand-written CASE lose the value when one side is null?

CASE WHEN Value > Other THEN Value ELSE Other END compares with a null on row 3, the test is unknown, and it falls through to Other, which is null. GREATEST returns the value that is there, 0.125.

Tableau

GREATEST([Value], [Other])

Tempting SQL

SELECT
  CASE WHEN Value > Other THEN Value ELSE Other END AS result
FROM input
ORDER BY id

SQL that matches

SELECT greatest(Value, Other) AS result
FROM input
ORDER BY id
ValueOtherTableauTempting SQLSQL that matches
2.54444
−2.5−3−2.5−2.5−2.5
0.130.13null0.13
−0.4−1−0.4−0.4−0.4
3.758888
2222
00.50.50.50.5
1616161616

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

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_IGNORE_NULLS(Value, Other) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT
  GREATEST(COALESCE(Value, Other), COALESCE(Other, Value)) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own GREATEST formula return?

Edit the formula and run it on the 8 sample rows. Fields: [id], [Region], [Value], [Qty], [Ratio], [Other].

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

Which functions go with it?

What do people ask about GREATEST?

Does GREATEST ignore nulls?

Yes: row 3 has no Other and row 6 no Value, and GREATEST returns the value that is there. GREATESTPROPAGATENULL returns null instead.

How is GREATEST different from MAX(a, b)?

Tableau's two-argument MAX returns null if either argument is null. GREATEST skips the null.

Does GREATEST work on strings?

Not in Tableau's GREATEST; it needs numbers.

Where is this documented?