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 (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: 1 to 4 for East, then 1 to 4 again for West.
Region
Month
Result
East
1
1
East
2
2
East
3
3
East
4
4
West
1
1
West
2
2
West
3
3
West
4
4
✓ 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 (PARTITIONBY Month ORDERBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Region: the same formula now counts across the regions, so East is 1 and West is 2 in every month.
Region
Month
Result
East
1
1
East
2
1
East
3
1
East
4
1
West
1
2
West
2
2
West
3
2
West
4
2
✓ 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 (PARTITIONBY Region ORDERBY Month DESC) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month, sorted descending: month 4 becomes mark 1. The sort direction is the ORDER BY direction.
Region
Month
Result
East
1
4
East
2
3
East
3
2
East
4
1
West
1
4
West
2
3
West
3
2
West
4
1
✓ 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 (ORDERBY Region, Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
row_number() OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
1
1
1
East
2
2
2
2
East
3
3
3
3
East
4
4
4
4
West
1
1
5
1
West
2
2
6
2
West
3
3
7
3
West
4
4
8
4
✓ 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 (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
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.