Tableau function Table calculation

Tableau RANK_UNIQUE to SQL

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,
  CASE
    WHEN sum(Sales) IS NOT NULL THEN row_number() OVER (PARTITION BY Region ORDER BY sum(Sales) DESC, Month)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Month: East's two 120s become 1 (month 2) and 2 (month 4). West's null month gets no rank.

RegionMonthResult
East13
East21
East34
East42
West13
West22
West3null
West41

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.

CaseTableau, then SQL (DuckDB)East 1East 2East 3East 4West 1West 2West 3West 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) END231412null3

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,
  CASE
    WHEN sum(Sales) IS NOT NULL THEN rank() OVER (PARTITION BY Region ORDER BY sum(Sales) DESC)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  CASE
    WHEN sum(Sales) IS NOT NULL THEN row_number() OVER (PARTITION BY Region ORDER BY sum(Sales) DESC, Month)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1333
East2111
East3444
East4212
West1333
West2222
West3nullnullnull
West4111

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 (PARTITION BY Region ORDER BY sum(Sales) DESC, Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  CASE
    WHEN sum(Sales) IS NOT NULL THEN row_number() OVER (PARTITION BY Region ORDER BY sum(Sales) DESC, Month)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1333
East2111
East3444
East4222
West1333
West2222
West3null4null
West4111

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,
  CASE
    WHEN SUM(Sales) IS NOT NULL THEN ROW_NUMBER() OVER (PARTITION BY Region ORDER BY SUM(Sales) DESC NULLS LAST, Month)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  CASE
    WHEN SUM(Sales) IS NOT NULL THEN ROW_NUMBER() OVER (PARTITION BY Region ORDER BY SUM(Sales) DESC NULLS LAST, Month)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own RANK_UNIQUE formula return?

Edit the formula and run it on the 9 sample rows. Fields: [id], [Region], [Month], [Sales], [Profit].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about RANK_UNIQUE?

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.

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.

Where is this documented?