RANK(SUM([Sales])) ranks the marks in each partition from the highest value down; tied marks share a rank and the next rank is skipped (competition ranking: 1, 1, 3, 4). In SQL that is 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(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 competition 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 the months within each region?
Tableau
RANK(SUM([Sales]))
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
CASEWHENsum(Sales) ISNOTNULLTHENrank() OVER (PARTITIONBY Region ORDERBYsum(Sales) DESC)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: East's months 2 and 4 tie at 120 and share rank 1, so the next rank is 3. West's null month gets no rank.
Region
Month
Result
East
1
3
East
2
1
East
3
4
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 ranks?
The same marks ranked with 'asc': the smallest value is 1. 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(SUM([Sales]), 'asc')Region, Month, CASE WHEN sum(Sales) IS NOT NULL THEN 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 is the biggest month ranked last?
RANK sorts descending by default, so 120 is rank 1. SQL's ORDER BY is ascending unless told otherwise, which ranks 60 first.
Tableau
RANK(SUM([Sales]))
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
CASEWHENsum(Sales) ISNOTNULLTHENrank() OVER (PARTITIONBY Region ORDERBYsum(Sales))
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
CASEWHENsum(Sales) ISNOTNULLTHENrank() 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
3
2
3
East
2
1
3
1
East
3
4
1
4
East
4
1
3
1
West
1
3
1
3
West
2
2
2
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 a null like any other value; DuckDB sorts it last, so West month 3 comes out as 4. Tableau leaves null marks unranked.
Tableau
RANK(SUM([Sales]))
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
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) ISNOTNULLTHENrank() 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
3
3
3
East
2
1
1
1
East
3
4
4
4
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) ISNOTNULLTHENRANK() OVER (PARTITIONBY Region ORDERBYSUM(Sales) DESC NULLS LAST)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
CASEWHENSUM(Sales) ISNOTNULLTHENRANK() 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.
Which way does RANK sort by default?
Descending: the biggest value is rank 1. SQL's ORDER BY is ascending unless you write DESC, which is the trap below.
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.