Tableau function Aggregate

Tableau PERCENTILE to SQL

PERCENTILE(expression, number) returns the value at that fraction of the sorted non-null values: 0.25 is the first quartile, 0.5 the median. Tableau interpolates between neighbours, which is DuckDB's quantile_cont(x, 0.25). 0 returns the minimum and 1 the maximum.

Updated · Checked on the xmetric engine

What is the syntax?

PERCENTILE(expression, number)
expression
a numeric field or row-level calculation.
number
a numeric constant from 0 to 1, such as 0.9 for the 90th percentile. It can't be a field.

Returns a number: the interpolated value at that percentile. 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 first quartile of sales in each region?

Tableau

PERCENTILE([Sales], 0.25)

SQL (DuckDB)

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

West's sorted sales are 150, 200, 250, 400. A quarter of the way along lands between 150 and 200: 187.5.

RegionResult
East87.5
Northnull
South80
West188

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

Where does a straight translation go wrong?

Common mistake

Why does quantile_disc return a value from the data instead?

quantile_disc returns the first value at or past the fraction, 150 for West. Tableau interpolates and returns 187.5.

Tableau

PERCENTILE([Sales], 0.25)

Tempting SQL

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

SQL that matches

SELECT Region, quantile_cont(Sales, 0.25) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East87.55087.5
Northnullnullnull
South808080
West188150188

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

What do PERCENTILE 0, 1 and an all-null group return?

0 is the smallest value and 1 the largest. North has no sales values, so every percentile is null there. Each result column is one mark, by Region.

CaseTableau, then SQL (DuckDB)EastNorthSouthWest
all-null groupPERCENTILE([Sales], 0.5)Region, quantile_cont(Sales, 0.5)100null80225
percentile 0PERCENTILE([Sales], 0)Region, quantile_cont(Sales, 0)50null80150
percentile 1PERCENTILE([Sales], 1)Region, quantile_cont(Sales, 1)300null80400

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

Snowflake documentation

BigQuery

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

BigQuery documentation

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

Can the percentile be a parameter or a field?

A parameter works; a field does not. Tableau needs a constant for the second argument.

Is PERCENTILE(x, 0.5) the same as MEDIAN?

Yes. Both interpolate between the two middle values: 225 for West.

Which data sources support PERCENTILE?

Tableau's help lists the sources it runs on, such as text files and non-legacy Excel; on other sources, extract the data or use WINDOW_PERCENTILE.

Where is this documented?