Tableau function Table calculation pattern

Tableau Regional mark position fraction to SQL

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

Compute using Month: four marks in each region, so 0.25 to 1. West's month with no sales still counts.

RegionMonthResult
East10.25
East20.5
East30.75
East41
West10.25
West20.5
West30.75
West41

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 (PARTITION BY Region ORDER BY Month)
    / count(sum(Sales)) OVER (PARTITION BY Region) 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)
    / count(*) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East10.250.250.25
East20.50.50.5
East30.750.750.75
East4111
West10.250.330.25
West20.50.670.5
West30.7510.75
West411.331

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

BigQuery documentation

What does your own Regional mark position fraction 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 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.

Where is this documented?