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
GROUPBY Region
ORDERBY Region
East has five lines but only four sales values (row 2 is empty): 550 / 4 = 137.5.
Region
Result
East
138
North
null
South
80
West
250
✓ 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
GROUPBY Region
ORDERBY Region
West has one line without units (row 7), so its average is 13 / 3.
Region
Result
East
2.6
North
1.5
South
2
West
4.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
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, avg(Sales) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
138
110
138
North
null
null
null
South
80
80
80
West
250
250
250
✓ 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
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, avg(Sales) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
138
110
138
North
null
0
null
South
80
80
80
West
250
250
250
✓ 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.
Case
Tableau, then SQL (DuckDB)
East
North
South
West
all-null group
AVG([Sales])Region, avg(Sales)
138
null
80
250
✓ 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
GROUPBY Region
ORDERBY Region