AVG({ INCLUDE [Customer] : SUM([Revenue]) }) adds up each customer's revenue within the view's marks, then takes the AVG of those customer totals. In SQL that is two levels of grouping: sum per Region and Customer in a subquery, then avg() per Region. Taking avg(Revenue) straight from the rows answers a different question, about invoice lines.
Updated · Checked on the xmetric engine
What is the syntax?
AVG({ INCLUDE [Customer] : SUM([Revenue]) })
dimensions
what to add to the view's grain, here [Customer].
aggregate
an aggregate expression, such as SUM([Revenue]).
AVG
the outer aggregate Tableau applies across the customer totals; it defaults to SUM, so set AVG on the pill or in the formula.
Returns a number per mark: the AVG of the customer totals inside it.
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 does the average customer spend in each region?
Tableau
AVG({ INCLUDE [Customer] : SUM([Revenue]) })
SQL (DuckDB)
SELECT Region, avg(total) AS result
FROM (
SELECT Region, Customer, sum(Revenue) AS total
FROM input
GROUPBY Region, Customer
)
GROUPBY Region
ORDERBY Region
North's customers spend 260, 300 and 90: an average of 216.67.
Region
Result
North
217
South
183
West
450
✓ The Tableau formula and the SQL return the same 3 rows.
What does the average customer spend with each team?
Tableau
AVG({ INCLUDE [Customer] : SUM([Revenue]) })
SQL (DuckDB)
SELECT Region, Team, avg(total) AS result
FROM (
SELECT Region, Team, Customer, sum(Revenue) AS total
FROM input
GROUPBY Region, Team, Customer
)
GROUPBY Region, Team
ORDERBY Region, Team
INCLUDE adds Customer to whatever the view has. In Cedar, Aster counts 200 (its Cedar spend), not its lifetime 260: (200 + 300) / 2 = 250.
Region
Team
Result
North
Cedar
250
North
Pine
75
South
Ash
125
South
Elm
300
West
Oak
450
✓ The Tableau formula and the SQL return the same 5 rows.
Where does a straight translation go wrong?
Common mistake
Why does avg(Revenue) give a different number?
Aggregating the raw rows gives the average invoice line, 130 for North. INCLUDE first adds up each customer's revenue, then takes the AVG of those totals.
Tableau
AVG({ INCLUDE [Customer] : SUM([Revenue]) })
Tempting SQL
SELECT Region, avg(Revenue) AS result
FROM input
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, avg(total) AS result
FROM (
SELECT Region, Customer, sum(Revenue) AS total
FROM input
GROUPBY Region, Customer
)
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
North
217
130
217
South
183
110
183
West
450
225
450
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
Common mistake
Why does a FIXED customer total give Cedar a higher average?
A per-customer total over all teams brings Aster's Pine spend into Cedar: (260 + 300) / 2 = 280. INCLUDE respects the view, so Aster counts 200 in Cedar.
Tableau
AVG({ INCLUDE [Customer] : SUM([Revenue]) })
Tempting SQL
SELECT i.Region, i.Team, avg(c.total) AS result
FROM (
SELECTDISTINCT Region, Team, Customer
FROM input
) i JOIN (
SELECT Customer, sum(Revenue) AS total
FROM input
GROUPBY Customer
) c USING (Customer)
GROUPBY i.Region, i.Team
ORDERBY i.Region, i.Team
SQL that matches
SELECT Region, Team, avg(total) AS result
FROM (
SELECT Region, Team, Customer, sum(Revenue) AS total
FROM input
GROUPBY Region, Team, Customer
)
GROUPBY Region, Team
ORDERBY Region, Team
Region
Team
Tableau
Tempting SQL
SQL that matches
North
Cedar
250
280
250
North
Pine
75
175
75
South
Ash
125
125
125
South
Elm
300
300
300
West
Oak
450
450
450
✓ 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, AVG(total) AS result
FROM (
SELECT Region, Customer, SUM(Revenue) AS total
FROM input
GROUPBY Region, Customer
)
GROUPBY Region
ORDERBY Region
SELECT Region, AVG(total) AS result
FROM (
SELECT Region, Customer, SUM(Revenue) AS total
FROM input
GROUPBY Region, Customer
)
GROUPBY Region
ORDERBY Region
Yes. INCLUDE is computed after dimension filters, so lines removed by a filter drop out of the totals before they are aggregated. A FIXED version would ignore the filter.
Why does the outer aggregate matter?
INCLUDE is finer than the view, so Tableau aggregates its values up to each mark. SUM gives back the region total; AVG gives the average customer.
How is this different from FIXED [Customer]?
FIXED counts a customer's revenue across the whole data source; INCLUDE only within the mark. They differ as soon as a customer spans marks, like Aster across two teams. See Fixed customer lifetime revenue.