MIN(expression) returns the smallest non-null value in the group; in SQL it is min(). With two arguments, MIN(a, b) compares them on each row, like SQL least(), except that Tableau returns null when either argument is null. DuckDB's least() skips nulls, so it needs a CASE.
Updated · Checked on the xmetric engine
What is the syntax?
MIN(expression) orMIN(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 smallest 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 smallest sale in each region?
Tableau
MIN([Sales])
SQL (DuckDB)
SELECT Region, min(Sales) AS result
FROM input
GROUPBY Region
ORDERBY Region
East's smallest sale is 50. North has no sales values: null.
Region
Result
East
50
North
null
South
80
West
150
✓ The Tableau formula and the SQL return the same 4 rows.
Which is smallest on each row, sales or units?
Tableau
MIN([Sales], [Units])
SQL (DuckDB)
SELECTCASEWHEN Sales ISNULLOR Units ISNULLTHENNULLELSEleast(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
2
3
null
300
5
5
50
1
1
100
2
2
200
4
4
250
null
150
3
3
400
6
6
1
null
2
null
80
2
2
✓ The Tableau formula and the SQL return the same 12 rows.
Where does a straight translation go wrong?
Common mistake
Why does least() return a number where Tableau returns null?
DuckDB's least() ignores null arguments, so row 2 gets its 3 units back. Tableau's two-argument MIN returns null if either side is null. Snowflake's and BigQuery's versions return null too, so this trap is DuckDB's.
Tableau
MIN([Sales], [Units])
Tempting SQL
SELECTleast(Sales, Units) AS result
FROM input
ORDERBY id
SQL that matches
SELECTCASEWHEN Sales ISNULLOR Units ISNULLTHENNULLELSEleast(Sales, Units)
ENDAS result
FROM input
ORDERBY id
Sales
Units
Tableau
Tempting SQL
SQL that matches
100
2
2
2
2
3
null
3
null
300
5
5
5
5
50
1
1
1
1
100
2
2
2
2
200
4
4
4
4
250
null
250
null
150
3
3
3
3
400
6
6
6
6
1
null
1
null
2
null
2
null
80
2
2
2
2
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
What does MIN 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)
Chair
Chair
Chair
Chair
Chair
Chair
Chair
Chair
Chair
Chair
all-null group
MIN([Sales])Region, min(Sales)
50
null
80
150
two strings
MIN([Product], 'Chair')CASE WHEN Product IS NULL OR 'Chair' IS NULL THEN NULL ELSE least(Product, 'Chair') END
Chair
Chair
Chair
Chair
Chair
Chair
Chair
Chair
Chair
null
null
Chair
two numbers
MIN([Sales], [Units])CASE WHEN Sales IS NULL OR Units IS NULL THEN NULL ELSE least(Sales, Units) END
2
null
5
1
2
4
null
3
6
null
null
2
✓ 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
SELECTLEAST(Sales, Units) AS result
FROM input
ORDERBY id