Tableau function Aggregate

Tableau MIN to SQL

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

East's smallest sale is 50. North has no sales values: null.

RegionResult
East50
Northnull
South80
West150

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)

SELECT
  CASE
    WHEN Sales IS NULL OR Units IS NULL THEN NULL
    ELSE least(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
10022
3null
30055
5011
10022
20044
250null
15033
40066
1null
2null
8022

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

SELECT least(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 least(Sales, Units)
    END AS result
FROM input
ORDER BY id
SalesUnitsTableauTempting SQLSQL that matches
1002222
3null3null
3005555
501111
1002222
2004444
250null250null
1503333
4006666
1null1null
2null2null
802222

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.

CaseTableau, then SQL (DuckDB)ChairChairChairChairChairChairChairChairChairChair
all-null groupMIN([Sales])Region, min(Sales)50null80150
two stringsMIN([Product], 'Chair')CASE WHEN Product IS NULL OR 'Chair' IS NULL THEN NULL ELSE least(Product, 'Chair') ENDChairChairChairChairChairChairChairChairChairnullnullChair
two numbersMIN([Sales], [Units])CASE WHEN Sales IS NULL OR Units IS NULL THEN NULL ELSE least(Sales, Units) END2null5124null36nullnull2

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

Snowflake documentation

BigQuery

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

BigQuery documentation

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

Does MIN work on text?

Yes, alphabetically. MIN([Product]) per region gives the first product name; East's first product alphabetically is Chair.

Is MIN(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: MIN(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?