Tableau function Table calculation pattern

Tableau Percent of regional total to SQL

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

Compute using Month: East month 1 is 100 / 400 = 0.25. West divides by 210, and its null month has no share.

RegionMonthResult
East10.25
East20.3
East30.15
East40.3
West10.24
West20.33
West3null
West40.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
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  sum(Sales)
    / sum(sum(Sales)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East10.250.160.25
East20.30.20.3
East30.150.10.15
East40.30.20.3
West10.240.080.24
West20.330.110.33
West3nullnullnull
West40.430.150.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 (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  SUM(Sales)
    / SUM(SUM(Sales)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own Percent of regional total 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 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).

Where is this documented?