RANK_PERCENTILE(SUM([Sales])) places each mark between 0 (lowest) and 1 (highest) in its partition, sorting ascending by default. Ties take the top of their group, and null marks are left out of both the rank and the count. In SQL it is (modified rank - 1) / (non-null marks - 1), with the modified rank counted by a RANGE window. DuckDB's percent_rank() looks like the same thing but is not.
Updated · Checked on the xmetric engine
What is the syntax?
RANK_PERCENTILE(expression, ['asc' | 'desc'])
expression
an aggregate, such as SUM([Sales]).
order
'asc' (the default) puts the smallest value at 0; 'desc' puts the largest at 0.
Returns a number from 0 to 1: the percentile rank of the current mark in its partition, or null when the expression is null.
What does it return, and what is the SQL?
Each example runs on the same 9 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
Where does each month sit between the region's lowest and highest?
Tableau
RANK_PERCENTILE(SUM([Sales]))
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
CASEWHENsum(Sales) ISNOTNULLTHEN (count(sum(Sales)) OVER (PARTITIONBY Region ORDERBYsum(Sales) RANGEBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW) -1) / (count(sum(Sales)) OVER (PARTITIONBY Region) -1)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: East's 60 is 0, 100 is 1/3 and both 120s are 1. West ranks its three non-null months 0, 0.5 and 1.
Region
Month
Result
East
1
0.33
East
2
1
East
3
0
East
4
1
West
1
0
West
2
0.5
West
3
null
West
4
1
✓ The Tableau formula and the SQL return the same 8 rows.
How do the order arguments change the percentiles?
'asc' is the default, so it matches the formula without an order. 'desc' flips the scale. Each result column is one mark, by Region and Month.
Case
Tableau, then SQL (DuckDB)
East 1
East 2
East 3
East 4
West 1
West 2
West 3
West 4
ascending ('asc')
RANK_PERCENTILE(SUM([Sales]), 'asc')Region, Month, CASE WHEN sum(Sales) IS NOT NULL THEN (count(sum(Sales)) OVER (PARTITION BY Region ORDER BY sum(Sales) RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) - 1) / (count(sum(Sales)) OVER (PARTITION BY Region) - 1) END
0.33
1
0
1
0
0.5
null
1
descending ('desc')
RANK_PERCENTILE(SUM([Sales]), 'desc')Region, Month, CASE WHEN sum(Sales) IS NOT NULL THEN (count(sum(Sales)) OVER (PARTITION BY Region ORDER BY sum(Sales) DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) - 1) / (count(sum(Sales)) OVER (PARTITION BY Region) - 1) END
0.67
0.33
1
0.33
1
0.5
null
0
✓ On every row, each Tableau formula and its SQL return the same value.
Where does a straight translation go wrong?
Common mistake
Why do percent_rank() values differ on ties and nulls?
percent_rank() gives tied marks the bottom of their group (East's 120s: 0.667) and counts the null month, so West is divided by 3 instead of 2. RANK_PERCENTILE gives ties the top of the group and skips nulls.
Tableau
RANK_PERCENTILE(SUM([Sales]))
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
percent_rank() OVER (PARTITIONBY Region ORDERBYsum(Sales)) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
CASEWHENsum(Sales) ISNOTNULLTHEN (count(sum(Sales)) OVER (PARTITIONBY Region ORDERBYsum(Sales) RANGEBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW) -1) / (count(sum(Sales)) OVER (PARTITIONBY Region) -1)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
0.33
0.33
0.33
East
2
1
0.67
1
East
3
0
0
0
East
4
1
0.67
1
West
1
0
0
0
West
2
0.5
0.33
0.5
West
3
null
1
null
West
4
1
0.67
1
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
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,
Month,
CASEWHENSUM(Sales) ISNOTNULLTHEN (COUNT(SUM(Sales)) OVER (PARTITIONBY Region ORDERBYSUM(Sales) RANGEBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW) -1) / (COUNT(SUM(Sales)) OVER (PARTITIONBY Region) -1)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
CASEWHENSUM(Sales) ISNOTNULLTHEN (COUNT(SUM(Sales)) OVER (PARTITIONBY Region ORDERBYSUM(Sales) RANGEBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW) -1) / (COUNT(SUM(Sales)) OVER (PARTITIONBY Region) -1)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
percent_rank() uses the competition rank, so ties take the bottom of their group, and it counts null rows. On East the two 120s come out 0.667 instead of 1, and every West value shifts because the null month counts as a fourth mark.
Which way does RANK_PERCENTILE sort?
Ascending by default, unlike RANK: the smallest value is 0. Pass 'desc' to make the largest 0.
What if a partition has one mark?
The SQL divides 0 by 0 there, which DuckDB returns as NaN. Guard it with CASE WHEN count(sum(Sales)) OVER (PARTITION BY Region) = 1 THEN 0 ELSE ... END if your data can have single-mark partitions.