Tableau function Table calculation

Tableau RANK_MODIFIED to SQL

RANK_MODIFIED(SUM([Sales])) is modified competition ranking: tied marks all take the last rank of their group, so two months tied for first are both 2. In SQL that is the number of marks at or above the current value: count(sum(Sales)) OVER (PARTITION BY Region ORDER BY sum(Sales) DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). The RANGE frame is what pulls in the tied rows.

Updated · Checked on the xmetric engine

What is the syntax?

RANK_MODIFIED(expression, ['asc' | 'desc'])
expression
an aggregate, such as SUM([Sales]).
order
'desc' (the default) ranks the largest value 1; 'asc' ranks the smallest 1.

Returns an integer: the modified competition rank of the current mark, 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.

How do I give tied months the lower rank?

Tableau

RANK_MODIFIED(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) DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Month: East's two 120s both count two marks at or above them, so both are 2. West has no ties: 1, 2, 3.

RegionMonthResult
East13
East22
East34
East42
West13
West22
West3null
West41

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

How does the ascending order change the modified ranks?

The same marks ranked with 'asc': East's two 120s are now the top of the order and both take 4. 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_MODIFIED(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) END241412null3

On every row, each Tableau formula and its SQL return the same value.

Where does a straight translation go wrong?

Common mistake

Why do the tied months get different ranks?

With a ROWS frame the count stops at the current row, so the first 120 counts 1 and the second 2. A RANGE frame includes every row with the same value, which is what RANK_MODIFIED does with ties.

Tableau

RANK_MODIFIED(SUM([Sales]))

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  CASE
    WHEN sum(Sales) IS NOT NULL THEN count(sum(Sales)) OVER (PARTITION BY Region ORDER BY sum(Sales) DESC, Month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
    END 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) DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1333
East2212
East3444
East4222
West1333
West2222
West3nullnullnull
West4111

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

Common mistake

Why does rank() give the tied months 1?

rank() gives ties the first rank of their group (standard competition). RANK_MODIFIED gives them the last one.

Tableau

RANK_MODIFIED(SUM([Sales]))

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  CASE
    WHEN sum(Sales) IS NOT NULL THEN rank() OVER (PARTITION BY Region ORDER BY sum(Sales) DESC)
    END 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) DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1333
East2212
East3444
East4212
West1333
West2222
West3nullnullnull
West4111

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) DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
    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) DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

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

What happens to a mark with no sales?

Tableau ignores nulls in ranking functions: West month 3 gets no rank, and West's other three months rank 1 to 3. SQL ranks nulls too (DuckDB sorts them last), so the CASE WHEN sum(Sales) IS NOT NULL wrapper blanks that row.

Is there a built-in SQL function for modified rank?

No. Besides the RANGE count, rank() OVER (...) + count(*) OVER (PARTITION BY Region, sum(Sales)) - 1 gives the same numbers: the competition rank plus the number of other marks tied with it.

How is RANK_MODIFIED different from RANK?

For East's 120, 120, 100, 60, RANK gives 1, 1, 3, 4 and RANK_MODIFIED gives 2, 2, 3, 4. Only the tied marks change.

Where is this documented?