Tableau function Level of detail

Tableau team share within region (EXCLUDE) to SQL

SUM([Revenue]) / SUM({ EXCLUDE [Team] : SUM([Revenue]) }) divides each team's revenue by its region's, so the teams of a region add up to 100%. In SQL: sum(Revenue) / sum(sum(Revenue)) OVER (PARTITION BY Region) over rows grouped by Region and Team.

Updated · Checked on the xmetric engine

What is the syntax?

SUM([Revenue]) / SUM({ EXCLUDE [Team] : SUM([Revenue]) })
numerator
SUM([Revenue]) at the view's grain, one team.
denominator
the same sum with Team excluded, the region total.

Returns a fraction per mark: the team's part of its region total.

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 share of its region does each team make?

Tableau

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

SQL (DuckDB)

SELECT
  Region,
  Team,
  sum(Revenue)
    / sum(sum(Revenue)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Team
ORDER BY Region, Team

Cedar makes 500 of North's 650: 76.9%. Pine makes the other 23.1%. West has one team, so Oak is 100%.

RegionTeamResult
NorthCedar0.77
NorthPine0.23
SouthAsh0.45
SouthElm0.55
WestOak1

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

Where does a straight translation go wrong?

Common mistake

Why do the shares add up to 100% across all regions instead?

An empty window OVER () divides by the grand total of 1,650. EXCLUDE [Team] keeps Region in the grain, so the window must partition by Region.

Tableau

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

Tempting SQL

SELECT
  Region,
  Team,
  sum(Revenue) / sum(sum(Revenue)) OVER () AS result
FROM input
GROUP BY Region, Team
ORDER BY Region, Team

SQL that matches

SELECT
  Region,
  Team,
  sum(Revenue)
    / sum(sum(Revenue)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Team
ORDER BY Region, Team
RegionTeamTableauTempting SQLSQL that matches
NorthCedar0.770.30.77
NorthPine0.230.090.23
SouthAsh0.450.150.45
SouthElm0.550.180.55
WestOak10.271

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,
  Team,
  SUM(Revenue)
    / SUM(SUM(Revenue)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Team
ORDER BY Region, Team

Snowflake documentation

BigQuery

SELECT
  Region,
  Team,
  SUM(Revenue)
    / SUM(SUM(Revenue)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Team
ORDER BY Region, Team

BigQuery documentation

What does your own Team share within region 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 Team share within region?

Do filters change it?

Yes. INCLUDE and EXCLUDE are computed after dimension filters, so filtering out a team removes its revenue from the region total too. That is the main practical difference from a FIXED region total.

Can I do this with a table calculation?

Yes: Percent of Total computed along Team gives the same numbers. The LOD version keeps working when you sort, hide or move Team.

Why wrap the EXCLUDE in SUM?

Tableau wraps a level of detail expression on a shelf in an aggregate. The region total is repeated on each team row, so SUM, MIN or MAX return the same value.

Where is this documented?