Tableau function Table calculation

Tableau INDEX to SQL

INDEX() numbers the marks along the Compute Using direction, starting at 1 and restarting for each partition. In SQL it is row_number() OVER (PARTITION BY Region ORDER BY Month): the dimensions it runs along go in ORDER BY, the rest of the view in PARTITION BY. A sort in Compute Using becomes the ORDER BY direction.

Updated · Checked on the xmetric engine

What is the syntax?

INDEX()

Returns an integer: the position of the current mark in its partition, starting at 1.

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 number the months within each region?

Tableau

INDEX()

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  row_number() OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Month: 1 to 4 for East, then 1 to 4 again for West.

RegionMonthResult
East11
East22
East33
East44
West11
West22
West33
West44

The Tableau formula and the SQL return the same 8 rows.

How do I number the regions within each month?

Tableau

INDEX()

Compute using Region, restarting for each Month

SQL (DuckDB)

SELECT
  Region,
  Month,
  row_number() OVER (PARTITION BY Month ORDER BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Region: the same formula now counts across the regions, so East is 1 and West is 2 in every month.

RegionMonthResult
East11
East21
East31
East41
West12
West22
West32
West42

The Tableau formula and the SQL return the same 8 rows.

How do I count from the latest month back?

Tableau

INDEX()

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  row_number() OVER (PARTITION BY Region ORDER BY Month DESC) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Month, sorted descending: month 4 becomes mark 1. The sort direction is the ORDER BY direction.

RegionMonthResult
East14
East23
East32
East41
West14
West23
West32
West41

The Tableau formula and the SQL return the same 8 rows.

Where does a straight translation go wrong?

Common mistake

Why does the SQL keep counting into West?

Compute Using Month restarts the index for each region. A row_number() with only ORDER BY numbers all eight marks in one run, so West starts at 5.

Tableau

INDEX()

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  row_number() OVER (ORDER BY Region, Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  row_number() OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1111
East2222
East3333
East4444
West1151
West2262
West3373
West4484

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

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  ROW_NUMBER() OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

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

Is INDEX() a rank?

No. It numbers marks in their sort order and ignores the measure, so ties get different numbers. For a rank by value, use RANK or RANK_UNIQUE.

How do I show only the top two months per region?

Sort the Compute Using by SUM([Sales]) descending and filter on INDEX() <= 2. In SQL, QUALIFY row_number() OVER (PARTITION BY Region ORDER BY sum(Sales) DESC) <= 2.

Does a null measure get an index?

Yes. West month 3 has no sales and is still mark 3: INDEX() counts marks, not values.

Where is this documented?