Tableau function Aggregate

Tableau MEDIAN to SQL

MEDIAN(expression) is the middle non-null value of the group. With an even count it is the average of the two middle values, which is what DuckDB's median() and quantile_cont(x, 0.5) return. quantile_disc picks one of the two instead.

Updated · Checked on the xmetric engine

What is the syntax?

MEDIAN(expression)
expression
a numeric field or row-level calculation.

Returns a number: the middle value, interpolated between the two middle values when the count is even. Null when the group has no values.

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 median sale in each region?

Tableau

MEDIAN([Sales])

SQL (DuckDB)

SELECT Region, median(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

West has 150, 200, 250 and 400: the median is halfway between 200 and 250, 225. East's four values put 100 and 100 in the middle.

RegionResult
East100
Northnull
South80
West225

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

Where does a straight translation go wrong?

Common mistake

Why does quantile_disc(x, 0.5) return 200 for West?

quantile_disc returns an actual value from the data, the lower middle one. Tableau interpolates, so West's median is 225.

Tableau

MEDIAN([Sales])

Tempting SQL

SELECT Region, quantile_disc(Sales, 0.5) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, median(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East100100100
Northnullnullnull
South808080
West225200225

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

What does MEDIAN return for a group with only nulls?

North has two order lines and no sales value on either. MEDIAN returns null there. Each result column is one mark, by Region.

CaseTableau, then SQL (DuckDB)EastNorthSouthWest
all-null groupMEDIAN([Sales])Region, median(Sales)100null80225

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 Region, MEDIAN(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

Snowflake documentation

BigQuery

SELECT
  DISTINCT Region,
  PERCENTILE_CONT(Sales, 0.5) OVER (PARTITION BY Region) AS result
FROM input
ORDER BY Region

BigQuery documentation

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

Does MEDIAN ignore nulls?

Yes. East's line without sales is left out, so its median is taken over four values.

Which data sources support MEDIAN?

Most, but not all: Tableau's help lists sources such as Access and Amazon Redshift where it is unavailable, and suggests an extract.

How do I get the median of customer totals?

Take the median of a level of detail expression: MEDIAN({ INCLUDE [Customer] : SUM([Revenue]) }). See Median customer revenue.

Where is this documented?