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
GROUPBY Region
ORDERBY Region
West's sorted sales are 150, 200, 250, 400. A quarter of the way along lands between 150 and 200: 187.5.
Region
Result
East
87.5
North
null
South
80
West
188
✓ 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
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, quantile_cont(Sales, 0.25) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
87.5
50
87.5
North
null
null
null
South
80
80
80
West
188
150
188
✓ 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.