RANK_DENSE(SUM([Sales])) ranks the marks in each partition from the highest value down; tied marks share a rank and the next distinct value takes the next number, with no gap (1, 1, 2, 3). In SQL that is dense_rank() OVER (PARTITION BY Region ORDER BY sum(Sales) DESC). Add 'asc' to rank from the lowest. Null marks get no rank.
Updated · Checked on the xmetric engine
What is the syntax?
RANK_DENSE(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 dense 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.
How do I rank months without gaps after a tie?
Tableau
RANK_DENSE(SUM([Sales]))
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
CASEWHENsum(Sales) ISNOTNULLTHENdense_rank() OVER (PARTITIONBY Region ORDERBYsum(Sales) DESC)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: East's two 120s share rank 1 and 100 is rank 2, not 3. West's null month gets no rank.
Region
Month
Result
East
1
2
East
2
1
East
3
3
East
4
1
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 dense ranks?
The same marks ranked with 'asc'. 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_DENSE(SUM([Sales]), 'asc')Region, Month, CASE WHEN sum(Sales) IS NOT NULL THEN dense_rank() OVER (PARTITION BY Region ORDER BY sum(Sales)) END
2
3
1
3
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 does the month after a tie jump to rank 3?
rank() leaves a gap after tied values: two months share 1, so 100 becomes 3. RANK_DENSE counts distinct values, so 100 is 2.
Tableau
RANK_DENSE(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) ISNOTNULLTHENdense_rank() OVER (PARTITIONBY Region ORDERBYsum(Sales) DESC)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
2
3
2
East
2
1
1
1
East
3
3
4
3
East
4
1
1
1
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 the month with no sales get a rank?
SQL ranks the null too; DuckDB sorts it last, so West month 3 gets 4. Tableau leaves null marks unranked.
Tableau
RANK_DENSE(SUM([Sales]))
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
dense_rank() OVER (PARTITIONBY Region ORDERBYsum(Sales) DESC) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
CASEWHENsum(Sales) ISNOTNULLTHENdense_rank() OVER (PARTITIONBY Region ORDERBYsum(Sales) DESC)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
2
2
2
East
2
1
1
1
East
3
3
3
3
East
4
1
1
1
West
1
3
3
3
West
2
2
2
2
West
3
null
4
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) ISNOTNULLTHENDENSE_RANK() OVER (PARTITIONBY Region ORDERBYSUM(Sales) DESC NULLS LAST)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
CASEWHENSUM(Sales) ISNOTNULLTHENDENSE_RANK() OVER (PARTITIONBY Region ORDERBYSUM(Sales) DESC NULLS LAST)
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.
When should I use RANK_DENSE instead of RANK?
When you want to count distinct levels: "the second-best sales figure" is dense rank 2 even if two months tie for first. With RANK the same month is rank 3.
Why NULLS LAST in the Snowflake and BigQuery SQL?
Warehouses differ on where nulls sort. Snowflake puts them first in a descending sort, which would push the null month to the top and move every West rank down by one. NULLS LAST keeps the non-null ranks right on all three.