AVG({ INCLUDE [Team] : SUM([Revenue]) }) adds up each team's revenue within a region, then averages those team totals. In SQL: sum per Region and Team in a subquery, then avg() per Region. avg(Revenue) on the rows is the average invoice line instead.
Updated · Checked on the xmetric engine
What is the syntax?
AVG({ INCLUDE [Team] : SUM([Revenue]) })
dimensions
what to add to the view's grain, here [Team].
aggregate
an aggregate expression, such as SUM([Revenue]).
Returns a number per mark: the average of the team 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 team make in each region?
Tableau
AVG({ INCLUDE [Team] : SUM([Revenue]) })
SQL (DuckDB)
SELECT Region, avg(total) AS result
FROM (
SELECT Region, Team, sum(Revenue) AS total
FROM input
GROUPBY Region, Team
)
GROUPBY Region
ORDERBY Region
North's teams make 500 and 150: 325 on average. West has one team, so 450.
Region
Result
North
325
South
275
West
450
✓ The Tableau formula and the SQL return the same 3 rows.
Where does a straight translation go wrong?
Common mistake
Why does avg(Revenue) give 130 for North?
That is the average of North's five invoice lines. INCLUDE [Team] averages the two team totals, 500 and 150.
Tableau
AVG({ INCLUDE [Team] : 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, Team, sum(Revenue) AS total
FROM input
GROUPBY Region, Team
)
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
North
325
130
325
South
275
110
275
West
450
225
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, Team, SUM(Revenue) AS total
FROM input
GROUPBY Region, Team
)
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.
What happens with Team in the view too?
INCLUDE [Team] adds nothing new, so each mark is that team's own total.
Why set AVG explicitly?
Tableau aggregates an INCLUDE that is finer than the view with SUM by default, which gives back the region total. Write AVG in the formula or on the pill.