Tableau function Aggregate

Tableau AVG to SQL

AVG(expression) is the mean of the non-null values in the group: their sum divided by how many there are. In SQL it is avg(). Nulls are left out of both the total and the count, so dividing by count(*) gives a smaller average whenever a row is missing a value.

Updated · Checked on the xmetric engine

What is the syntax?

AVG(expression)
expression
a numeric field or row-level calculation.

Returns a real number: the mean of the non-null values. Null when the group has none.

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 is the average sale per region?

Tableau

AVG([Sales])

SQL (DuckDB)

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

East has five lines but only four sales values (row 2 is empty): 550 / 4 = 137.5.

RegionResult
East138
Northnull
South80
West250

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

What is the average number of units per line?

Tableau

AVG([Units])

SQL (DuckDB)

SELECT Region, avg(Units) AS result
FROM input
GROUP BY Region
ORDER BY Region

West has one line without units (row 7), so its average is 13 / 3.

RegionResult
East2.6
North1.5
South2
West4.33

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

Where does a straight translation go wrong?

Common mistake

Why is sum(x) / count(*) lower than AVG?

count(*) counts rows, including the one with no sales. AVG divides by the number of values, so East is 550 / 4 = 137.5, not 550 / 5 = 110.

Tableau

AVG([Sales])

Tempting SQL

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

SQL that matches

SELECT Region, avg(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East138110138
Northnullnullnull
South808080
West250250250

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

Common mistake

Why does treating a missing sale as 0 change the average?

Filling nulls with coalesce(Sales, 0) adds a zero to the mean. Tableau ignores the empty line, so East stays 137.5 and North stays null.

Tableau

AVG([Sales])

Tempting SQL

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

SQL that matches

SELECT Region, avg(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East138110138
Northnull0null
South808080
West250250250

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

What does AVG return for a group with only nulls?

North has two order lines and no sales value on either. With no values to average, both return null. Each result column is one mark, by Region.

CaseTableau, then SQL (DuckDB)EastNorthSouthWest
all-null groupAVG([Sales])Region, avg(Sales)138null80250

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

Snowflake documentation

BigQuery

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

BigQuery documentation

What does your own AVG 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 AVG?

Does AVG count null rows?

No. Nulls are ignored, so they change neither the total nor the count. Use AVG(ZN([Sales])) if a missing value really means 0.

How do I average per customer instead of per row?

Average a level of detail expression: AVG({ INCLUDE [Customer] : SUM([Revenue]) }). See Mean customer revenue.

Does SQL avg() return an integer on integer columns?

Not in DuckDB, Snowflake or BigQuery: all three return a decimal or float, as Tableau does.

Where is this documented?