Tableau function Number

Tableau LEAST to SQL

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

Updated · Checked on the xmetric engine

What is the syntax?

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

Returns the smallest 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 smaller, Value or Other?

Tableau

LEAST([Value], [Other])

SQL (DuckDB)

SELECT least(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.542.5
−2.5−3−3
0.130.13
−0.4−1−1
3.7583.75
22
00.50
161616

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

How do I cap values at 1?

Tableau

LEAST([Value], 1)

SQL (DuckDB)

SELECT least(Value, 1) AS result
FROM input
ORDER BY id

Everything above 1 becomes 1. Row 6 has no value, and the result is 1, the one argument left.

ValueResult
2.51
−2.5−2.5
0.130.13
−0.4−0.4
3.751
1
00
161

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. LEAST returns the value that is there, 0.125.

Tableau

LEAST([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 least(Value, Other) AS result
FROM input
ORDER BY id
ValueOtherTableauTempting SQLSQL that matches
2.542.52.52.5
−2.5−3−3−3−3
0.130.13null0.13
−0.4−1−1−1−1
3.7583.753.753.75
2222
00.5000
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 LEAST_IGNORE_NULLS(Value, Other) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

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

BigQuery documentation

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

Does LEAST ignore nulls?

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

How is LEAST different from MIN(a, b)?

Tableau's two-argument MIN returns null if either argument is null. LEAST skips the null.

Does LEAST work on strings?

Not in Tableau's LEAST; it needs numbers.

Where is this documented?