RANK_DENSE(SUM([Sales])), computed using Month, ranks each region's months from highest to lowest sales with no gaps after ties. In SQL that is dense_rank() OVER (PARTITION BY Region ORDER BY sum(Sales) DESC), with null marks left unranked.
Updated · Checked on the xmetric engine
What is the syntax?
RANK_DENSE(SUM([Sales]))
Returns an integer: the dense rank of the month within its region, or null when the month has no sales.
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 each region's months without gaps?
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
East's months 2 and 4 tie at 120 for rank 1; 100 is 2 and 60 is 3. West ranks 90, 70, 50 as 1, 2, 3 and leaves month 3 blank.
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.
Where does a straight translation go wrong?
Common mistake
Why does West start at rank 2?
Without PARTITION BY Region, the rank runs over all eight marks, so West's 90 sits below East's 120 and 100. Compute Using Month ranks each region on its own.
Tableau
RANK_DENSE(SUM([Sales]))
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
CASEWHENsum(Sales) ISNOTNULLTHENdense_rank() OVER (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
2
2
East
2
1
1
1
East
3
3
5
3
East
4
1
1
1
West
1
3
6
3
West
2
2
4
2
West
3
null
null
null
West
4
1
3
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 like a value (DuckDB sorts it last, rank 4). Tableau leaves it 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
What do people ask about Dense rank within region?
Why does the expression need SUM inside it?
A table calculation runs on the marks in the view, so its argument must be an aggregate. A bare field such as [Sales] raises an error; see All fields must be aggregate or constant.
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.
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.