Tableau function Table calculation pattern

Tableau Running percent of total to SQL

RUNNING_SUM(SUM([Sales])) / WINDOW_SUM(SUM([Sales])) is the share of the region's sales reached by each month, ending at 1. In SQL the numerator is a running window and the denominator a whole-partition one: sum(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month) / sum(sum(Sales)) OVER (PARTITION BY Region).

Updated · Checked on the xmetric engine

What is the syntax?

RUNNING_SUM(SUM([Sales])) / WINDOW_SUM(SUM([Sales]))

Returns a number from 0 to 1: the running total divided by the 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 the region's sales has each month reached?

Tableau

RUNNING_SUM(SUM([Sales])) / WINDOW_SUM(SUM([Sales]))

Compute using Month, restarting for each Region

SQL (DuckDB)

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

Compute using Month: East reaches 0.25, 0.55, 0.70 and 1. West holds at 0.57 through its null month.

RegionMonthResult
East10.25
East20.55
East30.7
East41
West10.24
West20.57
West30.57
West41

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

Where does a straight translation go wrong?

Common mistake

Why is every month 1 in the SQL?

Using the same running window for both parts divides the running total by itself. WINDOW_SUM has no start and end, so it covers the whole region: drop ORDER BY from the denominator.

Tableau

RUNNING_SUM(SUM([Sales])) / WINDOW_SUM(SUM([Sales]))

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  sum(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month)
    / sum(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(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month)
    / sum(sum(Sales)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East10.2510.25
East20.5510.55
East30.710.7
East4111
West10.2410.24
West20.5710.57
West30.5710.57
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(SUM(Sales)) OVER (PARTITION BY Region ORDER BY Month)
    / 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(SUM(Sales)) OVER (PARTITION BY Region ORDER BY Month)
    / 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 Running percent of 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 Running percent of 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.

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.

Is this the Running Total, then Percent of Total quick calculation?

It gives the same numbers as a running total with a secondary percent-of-total calculation along the same direction.

Where is this documented?