Tableau function Table calculation

Tableau TOTAL to SQL

TOTAL(SUM([Sales])) returns the sum of sales over every mark in the partition, repeated on each mark. In SQL that is sum(sum(Sales)) OVER (PARTITION BY Region): no ORDER BY, so the window is the whole partition. Unlike WINDOW_SUM, TOTAL recomputes the aggregate from the underlying rows, which matters for averages and distinct counts.

Updated · Checked on the xmetric engine

What is the syntax?

TOTAL(expression)
expression
an aggregate, such as SUM([Sales]).

Returns the aggregate of the expression over the whole partition, on every mark of it.

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 show each region's total on every month?

Tableau

TOTAL(SUM([Sales]))

Compute using Month, restarting for each Region

SQL (DuckDB)

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

Compute using Month: every East mark shows 400, every West mark 210.

RegionMonthResult
East1400
East2400
East3400
East4400
West1210
West2210
West3210
West4210

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

How do I show each month's total across regions?

Tableau

TOTAL(SUM([Sales]))

Compute using Region, restarting for each Month

SQL (DuckDB)

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

Compute using Region: the partition is now the month, so month 1 shows 100 + 50 = 150 on both regions.

RegionMonthResult
East1150
East2190
East360
East4210
West1150
West2190
West360
West4210

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

Where does a straight translation go wrong?

Common mistake

Why is every row the grand total?

An empty OVER () sums all eight marks, 610. Compute Using Month partitions by Region, so TOTAL is 400 for East and 210 for West.

Tableau

TOTAL(SUM([Sales]))

Compute using Month, restarting for each Region

Tempting SQL

SELECT Region, Month, sum(sum(Sales)) OVER () 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) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1400610400
East2400610400
East3400610400
East4400610400
West1210610210
West2210610210
West3210610210
West4210610210

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

BigQuery documentation

What does your own 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 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 is TOTAL different from WINDOW_SUM?

For SUM they agree. For AVG, Tableau documents that TOTAL(AVG([Sales])) is the average of the underlying rows (East: 400 / 5 rows = 80), while WINDOW_AVG(AVG([Sales])) averages the marks. In SQL the row-level version is sum(sum(Sales)) OVER (PARTITION BY Region) / sum(count(Sales)) OVER (PARTITION BY Region).

What happens to a null mark?

It adds nothing. West's total is 50 + 70 + 90 = 210, and West month 3 still shows 210.

Where is this documented?