Tableau function Table calculation

Tableau RANK_PERCENTILE to SQL

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,
  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 AS result
FROM input
GROUP BY Region, Month
ORDER BY 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.

RegionMonthResult
East10.33
East21
East30
East41
West10
West20.5
West3null
West41

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.

CaseTableau, then SQL (DuckDB)East 1East 2East 3East 4West 1West 2West 3West 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) END0.3310100.5null1
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) END0.670.3310.3310.5null0

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

SQL that matches

SELECT
  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 AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East10.330.330.33
East210.671
East3000
East410.671
West1000
West20.50.330.5
West3null1null
West410.671

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,
  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 AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Snowflake documentation

BigQuery

SELECT
  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 AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own RANK_PERCENTILE formula return?

Edit the formula and run it on the 9 sample rows. Fields: [id], [Region], [Month], [Sales], [Profit].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about RANK_PERCENTILE?

Why not percent_rank()?

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.

Where is this documented?