Tableau function Level of detail

Tableau mean team revenue (INCLUDE) to SQL

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
  GROUP BY Region, Team
)
GROUP BY Region
ORDER BY Region

North's teams make 500 and 150: 325 on average. West has one team, so 450.

RegionResult
North325
South275
West450

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
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, avg(total) AS result
FROM (
  SELECT Region, Team, sum(Revenue) AS total
  FROM input
  GROUP BY Region, Team
)
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
North325130325
South275110275
West450225450

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
  GROUP BY Region, Team
)
GROUP BY Region
ORDER BY Region

Snowflake documentation

BigQuery

SELECT Region, AVG(total) AS result
FROM (
  SELECT Region, Team, SUM(Revenue) AS total
  FROM input
  GROUP BY Region, Team
)
GROUP BY Region
ORDER BY Region

BigQuery documentation

What does your own Mean team revenue formula return?

Edit the formula and run it on the 12 sample rows. Fields: [id], [Region], [Team], [Customer], [Invoice], [Revenue], [Date].

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

Which functions go with it?

What do people ask about Mean team revenue?

Do filters change it?

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.

Where is this documented?