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,
CASEWHENsum(Sales) ISNOTNULLTHENcount(sum(Sales)) OVER (PARTITIONBY Region ORDERBYsum(Sales) DESCRANGEBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY 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.
Region
Month
Result
East
1
3
East
2
2
East
3
4
East
4
2
West
1
3
West
2
2
West
3
null
West
4
1
✓ 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.
Case
Tableau, then SQL (DuckDB)
East 1
East 2
East 3
East 4
West 1
West 2
West 3
West 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) END
2
4
1
4
1
2
null
3
✓ 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,
CASEWHENsum(Sales) ISNOTNULLTHENcount(sum(Sales)) OVER (PARTITIONBY Region ORDERBYsum(Sales) DESC, Month ROWSBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
CASEWHENsum(Sales) ISNOTNULLTHENcount(sum(Sales)) OVER (PARTITIONBY Region ORDERBYsum(Sales) DESCRANGEBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
3
3
3
East
2
2
1
2
East
3
4
4
4
East
4
2
2
2
West
1
3
3
3
West
2
2
2
2
West
3
null
null
null
West
4
1
1
1
✓ 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,
CASEWHENsum(Sales) ISNOTNULLTHENrank() OVER (PARTITIONBY Region ORDERBYsum(Sales) DESC)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
CASEWHENsum(Sales) ISNOTNULLTHENcount(sum(Sales)) OVER (PARTITIONBY Region ORDERBYsum(Sales) DESCRANGEBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
3
3
3
East
2
2
1
2
East
3
4
4
4
East
4
2
1
2
West
1
3
3
3
West
2
2
2
2
West
3
null
null
null
West
4
1
1
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) ISNOTNULLTHENCOUNT(SUM(Sales)) OVER (PARTITIONBY Region ORDERBYSUM(Sales) DESCRANGEBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
CASEWHENSUM(Sales) ISNOTNULLTHENCOUNT(SUM(Sales)) OVER (PARTITIONBY Region ORDERBYSUM(Sales) DESCRANGEBETWEENUNBOUNDEDPRECEDINGANDCURRENTROW)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
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.