Tableau function Table calculation

Tableau RANK to SQL

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,
  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

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.

RegionMonthResult
East13
East21
East34
East41
West13
West22
West3null
West41

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.

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

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,
  CASE
    WHEN sum(Sales) IS NOT NULL THEN rank() OVER (PARTITION BY Region ORDER BY sum(Sales))
    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 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
East1323
East2131
East3414
East4131
West1313
West2222
West3nullnullnull
West4131

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 (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 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
East1333
East2111
East3444
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 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 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 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?

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 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.

How do the RANK variants treat ties?

For East's 120, 120, 100, 60: RANK gives 1, 1, 3, 4; RANK_DENSE 1, 1, 2, 3; RANK_MODIFIED 2, 2, 3, 4; RANK_UNIQUE 1, 2, 3, 4.

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?