RANK_UNIQUE(SUM([Sales])) ranks the marks from the highest value down and never repeats a number: tied marks get consecutive ranks in the order they sit in the partition. In SQL that is row_number() OVER (PARTITION BY Region ORDER BY sum(Sales) DESC, Month); the second sort key makes the tiebreak repeatable. Null marks get no rank.
Updated · Checked on the xmetric engine
What is the syntax?
RANK_UNIQUE(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: a rank that is unique within the 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 so no two share a number?
Tableau
RANK_UNIQUE(SUM([Sales]))
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
CASEWHENsum(Sales) ISNOTNULLTHENrow_number() OVER (PARTITIONBY Region ORDERBYsum(Sales) DESC, Month)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: East's two 120s become 1 (month 2) and 2 (month 4). West's null month gets no rank.
Region
Month
Result
East
1
3
East
2
1
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 unique 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_UNIQUE(SUM([Sales]), 'asc')Region, Month, CASE WHEN sum(Sales) IS NOT NULL THEN row_number() OVER (PARTITION BY Region ORDER BY sum(Sales), Month) END
2
3
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 still share rank 1?
rank() gives tied values the same number. RANK_UNIQUE splits them, which in SQL is row_number().
Tableau
RANK_UNIQUE(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) ISNOTNULLTHENrow_number() OVER (PARTITIONBY Region ORDERBYsum(Sales) DESC, Month)
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
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.
Common mistake
Why does the month with no sales get a rank?
row_number() numbers every row, nulls included; DuckDB sorts the null last, so West month 3 gets 4. Tableau skips it.
Tableau
RANK_UNIQUE(SUM([Sales]))
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
row_number() OVER (PARTITIONBY Region ORDERBYsum(Sales) DESC, Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
CASEWHENsum(Sales) ISNOTNULLTHENrow_number() OVER (PARTITIONBY Region ORDERBYsum(Sales) DESC, Month)
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
2
2
2
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) ISNOTNULLTHENROW_NUMBER() OVER (PARTITIONBY Region ORDERBYSUM(Sales) DESC NULLS LAST, Month)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
CASEWHENSUM(Sales) ISNOTNULLTHENROW_NUMBER() OVER (PARTITIONBY Region ORDERBYSUM(Sales) DESC NULLS LAST, Month)
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 tied month gets the lower number?
The one that comes first in the partition's order. Here the partition runs by Month, so East month 2 is 1 and month 4 is 2. In SQL, write that order as the second ORDER BY key; without it, row_number() may split the tie either way.
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.