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.
SELECT Region,
Month,
(sum(Sales) -min(sum(Sales)) OVER (PARTITIONBY Region))
/ (max(sum(Sales)) OVER (PARTITIONBY Region) -min(sum(Sales)) OVER (PARTITIONBY Region)) AS result
FROM input
GROUPBY Region, Month
ORDERBY 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.
Region
Month
Result
East
1
0.67
East
2
1
East
3
0
East
4
1
West
1
0
West
2
0.5
West
3
null
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 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.
SELECT Region,
Month,
(sum(Sales) -min(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month))
/ (max(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) -min(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month)) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
(sum(Sales) -min(sum(Sales)) OVER (PARTITIONBY Region))
/ (max(sum(Sales)) OVER (PARTITIONBY Region) -min(sum(Sales)) OVER (PARTITIONBY Region)) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
0.67
NaN
0.67
East
2
1
1
1
East
3
0
0
0
East
4
1
1
1
West
1
0
NaN
0
West
2
0.5
1
0.5
West
3
null
null
null
West
4
1
1
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,
(SUM(Sales) -MIN(SUM(Sales)) OVER (PARTITIONBY Region))
/NULLIF(MAX(SUM(Sales)) OVER (PARTITIONBY Region) -MIN(SUM(Sales)) OVER (PARTITIONBY Region), 0) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
SAFE_DIVIDE(SUM(Sales) -MIN(SUM(Sales)) OVER (PARTITIONBY Region), MAX(SUM(Sales)) OVER (PARTITIONBY Region) -MIN(SUM(Sales)) OVER (PARTITIONBY Region)) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
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.