SUM([Sales]) / TOTAL(SUM([Sales])), computed using Month, is each month's share of its region's sales. In SQL that is sum(Sales) / sum(sum(Sales)) OVER (PARTITION BY Region). Each region's shares add up to 1.
Updated · Checked on the xmetric engine
What is the syntax?
SUM([Sales]) /TOTAL(SUM([Sales]))
Returns a number from 0 to 1: the mark's share of its partition total.
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.
What share of each region's sales does each month make?
Tableau
SUM([Sales]) /TOTAL(SUM([Sales]))
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
sum(Sales)
/sum(sum(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: East month 1 is 100 / 400 = 0.25. West divides by 210, and its null month has no share.
Region
Month
Result
East
1
0.25
East
2
0.3
East
3
0.15
East
4
0.3
West
1
0.24
West
2
0.33
West
3
null
West
4
0.43
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why do the shares add up to less than 1 per region?
An empty OVER () divides by the total of both regions, 610. Compute Using Month partitions by Region, so East divides by 400 and West by 210.
Tableau
SUM([Sales]) /TOTAL(SUM([Sales]))
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
sum(Sales) /sum(sum(Sales)) OVER () AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
sum(Sales)
/sum(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.25
0.16
0.25
East
2
0.3
0.2
0.3
East
3
0.15
0.1
0.15
East
4
0.3
0.2
0.3
West
1
0.24
0.08
0.24
West
2
0.33
0.11
0.33
West
3
null
null
null
West
4
0.43
0.15
0.43
✓ 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)
/SUM(SUM(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
What do people ask about Percent of regional total?
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.
How do I get each region's share of a month instead?
Compute using Region: the partition becomes the month. In SQL, sum(Sales) / sum(sum(Sales)) OVER (PARTITION BY Month).
Will integer division round the shares to 0?
Not in DuckDB or BigQuery, where / returns a decimal. In engines where integer / integer stays an integer, cast one side with CAST(sum(Sales) AS DOUBLE).