Tableau function Level of detail

Tableau FIXED distinct customers per region to SQL

{ FIXED [Region] : COUNTD([Customer]) } counts each region's different customers once, even when the view splits the region by team. In SQL, count distinct customers per region in a subquery and join it back. Distinct counts don't add up: a customer who buys from two teams is one customer.

Updated · Checked on the xmetric engine

What is the syntax?

{ FIXED [Region] : COUNTD([Customer]) }
dimensions
the fields the aggregate is grouped by, whatever the view shows.
aggregate
COUNTD([Customer]).

Returns an integer per row: how many different customers the region has.

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.

How many customers does each team's region have?

Tableau

{ FIXED [Region] : COUNTD([Customer]) }

SQL (DuckDB)

WITH r AS (
  SELECT Region, count(DISTINCT Customer) AS n
  FROM input
  GROUP BY Region
)
SELECT DISTINCT i.Region, i.Team, r.n AS result
FROM input i JOIN r USING (Region)
ORDER BY i.Region, i.Team

North has 3 customers: Aster, Birch and Clover. Both North teams show 3.

RegionTeamResult
NorthCedar3
NorthPine3
SouthAsh3
SouthElm3
WestOak1

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

How many customers does each region have?

Tableau

{ FIXED [Region] : COUNTD([Customer]) }

SQL (DuckDB)

SELECT Region, count(DISTINCT Customer) AS result
FROM input
GROUP BY Region
ORDER BY Region

With Region alone in the view, a plain distinct count agrees.

RegionResult
North3
South3
West1

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

Where does a straight translation go wrong?

Common mistake

Why does GROUP BY Region, Team give 2 customers per North team?

Grouping by the view counts customers per team: Cedar has Aster and Birch, Pine has Clover and Aster. FIXED counts the region: 3.

Tableau

{ FIXED [Region] : COUNTD([Customer]) }

Tempting SQL

SELECT Region, Team, count(DISTINCT Customer) AS result
FROM input
GROUP BY Region, Team
ORDER BY Region, Team

SQL that matches

WITH r AS (
  SELECT Region, count(DISTINCT Customer) AS n
  FROM input
  GROUP BY Region
)
SELECT DISTINCT i.Region, i.Team, r.n AS result
FROM input i JOIN r USING (Region)
ORDER BY i.Region, i.Team
RegionTeamTableauTempting SQLSQL that matches
NorthCedar323
NorthPine323
SouthAsh323
SouthElm313
WestOak111

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

Common mistake

Why can't I add up the team counts?

Aster buys from both North teams. Adding the per-team distinct counts gives North 4 customers; COUNTD over the region gives 3.

Tableau

{ FIXED [Region] : COUNTD([Customer]) }

Tempting SQL

SELECT Region, sum(n) AS result
FROM (
  SELECT Region, Team, count(DISTINCT Customer) AS n
  FROM input
  GROUP BY Region, Team
)
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, count(DISTINCT Customer) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
North343
South333
West111

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 r AS (
  SELECT Region, COUNT(DISTINCT Customer) AS n
  FROM input
  GROUP BY Region
)
SELECT DISTINCT i.Region, i.Team, r.n AS result
FROM input i JOIN r USING (Region)
ORDER BY i.Region, i.Team

Snowflake documentation

BigQuery

WITH r AS (
  SELECT Region, COUNT(DISTINCT Customer) AS n
  FROM input
  GROUP BY Region
)
SELECT DISTINCT i.Region, i.Team, r.n AS result
FROM input i JOIN r USING (Region)
ORDER BY i.Region, i.Team

BigQuery documentation

What does your own Distinct customers per 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 Distinct customers per region?

Do filters change it?

Ordinary dimension filters on the Filters shelf do not: FIXED is computed before them. Add a filter to context (or filter the data source) to make it count only the rows that survive. In SQL, a context filter is a WHERE inside the subquery or window; an ordinary filter is a WHERE that does not reach it.

Can I use a window for the distinct count?

Not portably. DuckDB, Snowflake and BigQuery don't all accept count(DISTINCT x) OVER (...); the subquery and join works everywhere.

What does SUM of this field return in a Region view?

The region count, 3 for North: the value is repeated per mark, not per row, so the aggregate doesn't multiply it.

Where is this documented?