Tableau function Table calculation pattern

Tableau Regional min-max normalization to SQL

This formula rescales each region's months to run from 0 (the lowest month) to 1 (the highest). In SQL the three windows are over the whole region: (sum(Sales) - min(...) OVER (PARTITION BY Region)) / (max(...) OVER (PARTITION BY Region) - min(...) OVER (PARTITION BY Region)). Keep ORDER BY out of those windows.

Updated · Checked on the xmetric engine

What is the syntax?

(SUM([Sales]) - WINDOW_MIN(SUM([Sales]))) / (WINDOW_MAX(SUM([Sales])) - WINDOW_MIN(SUM([Sales])))

Returns a number from 0 to 1: where the mark sits between the lowest and highest marks of 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 do I rescale each region's months from 0 to 1?

Tableau

(SUM([Sales]) - WINDOW_MIN(SUM([Sales]))) / (WINDOW_MAX(SUM([Sales])) - WINDOW_MIN(SUM([Sales])))

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  (sum(Sales) - min(sum(Sales)) OVER (PARTITION BY Region))
    / (max(sum(Sales)) OVER (PARTITION BY Region) - min(sum(Sales)) OVER (PARTITION BY Region)) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Month: East runs from 60 to 120, so 100 becomes 0.667. West runs from 50 to 90, and its null month stays null.

RegionMonthResult
East10.67
East21
East30
East41
West10
West20.5
West3null
West41

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

Where does a straight translation go wrong?

Common mistake

Why does the first month of each region come out NaN?

With ORDER BY Month the min and max windows only see the months so far. In month 1 both are the same value, so the SQL divides 0 by 0. WINDOW_MIN and WINDOW_MAX cover the whole region.

Tableau

(SUM([Sales]) - WINDOW_MIN(SUM([Sales]))) / (WINDOW_MAX(SUM([Sales])) - WINDOW_MIN(SUM([Sales])))

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  (sum(Sales) - min(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month))
    / (max(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month) - min(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month)) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  (sum(Sales) - min(sum(Sales)) OVER (PARTITION BY Region))
    / (max(sum(Sales)) OVER (PARTITION BY Region) - min(sum(Sales)) OVER (PARTITION BY Region)) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East10.67NaN0.67
East2111
East3000
East4111
West10NaN0
West20.510.5
West3nullnullnull
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,
  (SUM(Sales) - MIN(SUM(Sales)) OVER (PARTITION BY Region))
    / NULLIF(MAX(SUM(Sales)) OVER (PARTITION BY Region) - MIN(SUM(Sales)) OVER (PARTITION BY Region), 0) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  SAFE_DIVIDE(SUM(Sales) - MIN(SUM(Sales)) OVER (PARTITION BY Region), MAX(SUM(Sales)) OVER (PARTITION BY Region) - MIN(SUM(Sales)) OVER (PARTITION BY Region)) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own Regional min-max normalization 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 min-max normalization?

Why does the expression need SUM inside it?

A table calculation runs on the marks in the view, so its argument must be an aggregate. A bare field such as [Sales] raises an error; see All fields must be aggregate or constant.

Why no ORDER BY in the SQL?

Without start and end, a WINDOW_ function covers the whole partition. A SQL window with ORDER BY and no frame ends at the current row, so leave ORDER BY out, or write ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.

What if every month in a region has the same sales?

The divisor is 0. Guard it: IF WINDOW_MAX(...) = WINDOW_MIN(...) THEN 0 ELSE ... END, and nullif(max - min, 0) in SQL.

Where is this documented?