Tableau function Level of detail

Tableau EXCLUDE with a dimension filter, in SQL

EXCLUDE is computed after dimension filters, so { EXCLUDE [Team] : SUM([Revenue]) } adds only the rows the filter keeps. With Team filtered to Cedar and Elm, North's region total is Cedar's 500, not 650. In SQL, filter first, then take the window over the filtered rows.

Updated · Checked on the xmetric engine

What is the syntax?

{ EXCLUDE [Team] : SUM([Revenue]) }
dimensions
the view dimensions to leave out, here [Team].
aggregate
an aggregate such as SUM([Revenue]).

Returns the aggregate at the view's level without the excluded dimension, over the rows the filters keep.

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 region total return with Team filtered to Cedar and Elm?

Tableau

{ EXCLUDE [Team] : SUM([Revenue]) }

Filter Team = Cedar, Elm, as a dimension filter

SQL (DuckDB)

WITH f AS (
  SELECT *
  FROM input
  WHERE Team IN ('Cedar', 'Elm')
)
SELECT
  Region,
  Team,
  sum(sum(Revenue)) OVER (PARTITION BY Region) AS result
FROM f
GROUP BY Region, Team
ORDER BY Region, Team

North's total is Cedar's 500 and South's is Elm's 300: the filtered-out teams Pine and Ash are not counted.

RegionTeamResult
NorthCedar500
SouthElm300

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

Where does a straight translation go wrong?

Common mistake

Why does a region-total subquery over the whole table give 650?

A per-region subquery over input behaves like FIXED: it reads every team. EXCLUDE only sees what the dimension filter kept, so filter before building the total.

Tableau

{ EXCLUDE [Team] : SUM([Revenue]) }

Filter Team = Cedar, Elm, as a dimension filter

Tempting SQL

WITH totals AS (
  SELECT Region, sum(Revenue) AS total
  FROM input
  GROUP BY Region
)
SELECT i.Region, i.Team, max(totals.total) AS result
FROM input i JOIN totals USING (Region)
WHERE i.Team IN ('Cedar', 'Elm')
GROUP BY i.Region, i.Team
ORDER BY i.Region, i.Team

SQL that matches

WITH f AS (
  SELECT *
  FROM input
  WHERE Team IN ('Cedar', 'Elm')
)
SELECT
  Region,
  Team,
  sum(sum(Revenue)) OVER (PARTITION BY Region) AS result
FROM f
GROUP BY Region, Team
ORDER BY Region, Team
RegionTeamTableauTempting SQLSQL that matches
NorthCedar500650500
SouthElm300550300

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

WITH f AS (
  SELECT *
  FROM input
  WHERE Team IN ('Cedar', 'Elm')
)
SELECT
  Region,
  Team,
  sum(sum(Revenue)) OVER (PARTITION BY Region) AS result
FROM f
GROUP BY Region, Team
ORDER BY Region, Team

Snowflake documentation

BigQuery

WITH f AS (
  SELECT *
  FROM input
  WHERE Team IN ('Cedar', 'Elm')
)
SELECT
  Region,
  Team,
  sum(sum(Revenue)) OVER (PARTITION BY Region) AS result
FROM f
GROUP BY Region, Team
ORDER BY Region, Team

BigQuery documentation

What does your own EXCLUDE with a dimension filter 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 EXCLUDE with a dimension filter?

How do I get the unfiltered region total instead?

Use { FIXED [Region] : SUM([Revenue]) }. FIXED runs before dimension filters, so it returns 650 for North with the same filter.

Does INCLUDE behave the same way?

Yes. INCLUDE and EXCLUDE both run after dimension filters; only FIXED runs before them.

Does a context filter change EXCLUDE too?

Yes. Context filters run before every level of detail expression, so EXCLUDE sees only the rows they keep.

Where is this documented?