Tableau function Aggregate

Tableau SUM to SQL

SUM(expression) adds every non-null value in the group. In SQL it is the same word: sum(Sales) with a GROUP BY for the view's dimensions. A group where every value is null returns null, not 0. The thing to get right is where SUM sits in a ratio: SUM([Sales]) / SUM([Units]) is not the sum of each row's ratio.

Updated · Checked on the xmetric engine

What is the syntax?

SUM(expression)
expression
a numeric field or row-level calculation, such as [Sales] or [Price] * [Quantity].

Returns a number: the total of the non-null values. Null when the group has no non-null value.

What does it return, and what is the SQL?

Each example runs on the same 12 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

What are total sales?

Tableau

SUM([Sales])

SQL (DuckDB)

SELECT sum(Sales) AS result
FROM input

With no dimension in the view there is one mark: 1,630. Line 2 and the two North lines have no sales and add nothing.

Result
1,630

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

What are total sales per region?

Tableau

SUM([Sales])

SQL (DuckDB)

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

One mark per region. North has two order lines and no sales value on either, so its total is null rather than 0.

RegionResult
East550
Northnull
South80
West1,000

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

What is the average price per unit in each region?

Tableau

SUM([Sales]) / SUM([Units])

SQL (DuckDB)

SELECT Region, sum(Sales) / sum(Units) AS result
FROM input
GROUP BY Region
ORDER BY Region

East sells 550 over 13 units: 42.31 a unit. Each SUM runs over the region first, then the division.

RegionResult
East42.31
Northnull
South40
West76.92

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

Where does a straight translation go wrong?

Common mistake

Why does summing each row's ratio give a different price?

Tableau evaluates SUM([Sales]) / SUM([Units]) per mark: two totals, then one division. Dividing on each row and summing adds up the per-line prices instead, so East gets 210 rather than 42.31.

Tableau

SUM([Sales]) / SUM([Units])

Tempting SQL

SELECT Region, sum(Sales / Units) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, sum(Sales) / sum(Units) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East42.3121042.31
Northnullnullnull
South404040
West76.9216776.92

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

Common mistake

Why does North show 0 in SQL but null in Tableau?

SUM ignores nulls, and a group with nothing left to add returns null. Wrapping the SQL in coalesce(..., 0) out of habit turns North into 0, which a chart then plots as a real zero. Use ZN(SUM([Sales])) in Tableau if you want the zero.

Tableau

SUM([Sales])

Tempting SQL

SELECT Region, coalesce(sum(Sales), 0) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, sum(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East550550550
Northnull0null
South808080
West1,0001,0001,000

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

What does SUM return for a group with only nulls?

North has two order lines and no sales value on either. Both Tableau and SQL return null there. Each result column is one mark, by Region.

CaseTableau, then SQL (DuckDB)EastNorthSouthWest
all-null groupSUM([Sales])Region, sum(Sales)550null801,000

On every row, each Tableau formula and its SQL return the same value.

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

Snowflake documentation

BigQuery

SELECT Region, SUM(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

BigQuery documentation

What does your own SUM formula return?

Edit the formula and run it on the 12 sample rows. Fields: [id], [Region], [Product], [Sales], [Units].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about SUM?

Does SUM count nulls as zero?

No, it skips them. East has a line with no sales (row 2); its total is 550 either way, but a group that is all null, like North, returns null.

Can SUM take a string or a date?

No. SUM works on numbers only. For text use MIN, MAX, ATTR or COUNTD.

How do I sum at a different level than the view?

Use a level of detail expression, such as { FIXED [Region] : SUM([Sales]) }. See Fixed region total.

Where is this documented?