Tableau function Table calculation

Tableau RANK_DENSE to SQL

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

RegionMonthResult
East12
East21
East33
East41
West13
West22
West3null
West41

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.

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

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,
  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 dense_rank() OVER (PARTITION BY Region ORDER BY sum(Sales) DESC)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1232
East2111
East3343
East4111
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?

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 (PARTITION BY Region ORDER BY sum(Sales) DESC) 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 dense_rank() OVER (PARTITION BY Region ORDER BY sum(Sales) DESC)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1222
East2111
East3333
East4111
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 DENSE_RANK() OVER (PARTITION BY Region ORDER BY SUM(Sales) DESC NULLS LAST)
    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 DENSE_RANK() OVER (PARTITION BY Region ORDER BY SUM(Sales) DESC NULLS LAST)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own RANK_DENSE 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_DENSE?

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.

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.

Where is this documented?