INDEX() / SIZE() is how far through its region each month sits: 0.25, 0.5, 0.75, 1 for four months. In SQL that is row_number() OVER (PARTITION BY Region ORDER BY Month) / count(*) OVER (PARTITION BY Region). Both count marks, whatever their values.
Updated · Checked on the xmetric engine
What is the syntax?
INDEX() /SIZE()
Returns a number from 0 to 1: the mark's 1-based position divided by the number of marks in its partition.
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 far through its region is each month?
Tableau
INDEX() /SIZE()
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
row_number() OVER (PARTITIONBY Region ORDERBY Month)
/count(*) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: four marks in each region, so 0.25 to 1. West's month with no sales still counts.
Region
Month
Result
East
1
0.25
East
2
0.5
East
3
0.75
East
4
1
West
1
0.25
West
2
0.5
West
3
0.75
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 West go past 1?
count(sum(Sales)) counts marks with sales, 3 for West, so West month 4 is 4 / 3. SIZE() counts every mark, as count(*) does.
Tableau
INDEX() /SIZE()
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
row_number() OVER (PARTITIONBY Region ORDERBY Month)
/count(sum(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
row_number() OVER (PARTITIONBY Region ORDERBY Month)
/count(*) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
0.25
0.25
0.25
East
2
0.5
0.5
0.5
East
3
0.75
0.75
0.75
East
4
1
1
1
West
1
0.25
0.33
0.25
West
2
0.5
0.67
0.5
West
3
0.75
1
0.75
West
4
1
1.33
1
✓ 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)
/COUNT(*) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
ROW_NUMBER() OVER (PARTITIONBY Region ORDERBY Month)
/COUNT(*) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
What do people ask about Regional mark position fraction?
Does integer division turn it into 0?
Not in Tableau, which divides integers as decimals, nor in DuckDB or BigQuery. If your warehouse keeps integer / integer as an integer, cast one side: CAST(row_number() OVER (...) AS DOUBLE).
How do I start at 0 instead?
Use (INDEX() - 1) / (SIZE() - 1), which runs from 0 on the first mark to 1 on the last; guard partitions with one mark.