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
GROUPBY Region
)
SELECTDISTINCT i.Region, i.Team, r.n AS result
FROM input i JOIN r USING (Region)
ORDERBY i.Region, i.Team
North has 3 customers: Aster, Birch and Clover. Both North teams show 3.
Region
Team
Result
North
Cedar
3
North
Pine
3
South
Ash
3
South
Elm
3
West
Oak
1
✓ 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
GROUPBY Region
ORDERBY Region
With Region alone in the view, a plain distinct count agrees.
Region
Result
North
3
South
3
West
1
✓ 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
GROUPBY Region, Team
ORDERBY Region, Team
SQL that matches
WITH r AS (
SELECT Region, count(DISTINCT Customer) AS n
FROM input
GROUPBY Region
)
SELECTDISTINCT i.Region, i.Team, r.n AS result
FROM input i JOIN r USING (Region)
ORDERBY i.Region, i.Team
Region
Team
Tableau
Tempting SQL
SQL that matches
North
Cedar
3
2
3
North
Pine
3
2
3
South
Ash
3
2
3
South
Elm
3
1
3
West
Oak
1
1
1
✓ 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
GROUPBY Region, Team
)
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, count(DISTINCT Customer) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
North
3
4
3
South
3
3
3
West
1
1
1
✓ 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
GROUPBY Region
)
SELECTDISTINCT i.Region, i.Team, r.n AS result
FROM input i JOIN r USING (Region)
ORDERBY i.Region, i.Team
WITH r AS (
SELECT Region, COUNT(DISTINCT Customer) AS n
FROM input
GROUPBY Region
)
SELECTDISTINCT i.Region, i.Team, r.n AS result
FROM input i JOIN r USING (Region)
ORDERBY i.Region, i.Team
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.