Tableau nested FIXED largest customer per region to SQL
{ FIXED [Region] : MAX({ FIXED [Customer] : SUM([Revenue]) }) } works inside out: the inner FIXED puts each customer's total revenue on every row, and the outer FIXED takes the MAX of those per region. In SQL: total per customer in a CTE, join it to the rows, then max() per Region. Because both levels are FIXED, the result ignores the view and its ordinary filters.
WITH c AS (
SELECT Customer, sum(Revenue) AS total
FROM input
GROUPBY Customer
)
SELECT i.Region, max(c.total) AS result
FROM input i JOIN c USING (Customer)
GROUPBY i.Region
ORDERBY i.Region
North's customers total 260, 300 and 90, so 300; South's largest is Dahlia's 300, West's Holly's 450.
Region
Result
North
300
South
300
West
450
✓ The Tableau formula and the SQL return the same 3 rows.
WITH c AS (
SELECT Customer, sum(Revenue) AS total
FROM input
GROUPBY Customer
), r AS (
SELECT i.Region, max(c.total) AS v
FROM input i JOIN c USING (Customer)
GROUPBY i.Region
)
SELECTDISTINCT i.Region, i.Team, r.v AS result
FROM input i JOIN r USING (Region)
ORDERBY i.Region, i.Team
The outer FIXED ignores Team, so both teams of a region show the region's value.
Region
Team
Result
North
Cedar
300
North
Pine
300
South
Ash
300
South
Elm
300
West
Oak
450
✓ The Tableau formula and the SQL return the same 5 rows.
Where does a straight translation go wrong?
Common mistake
Why does max(Revenue) give a different number?
It returns the largest invoice line: 200 for South. The nested expression compares customer totals, so it adds each customer's lines first.
SELECT Region, max(Revenue) AS result
FROM input
GROUPBY Region
ORDERBY Region
SQL that matches
WITH c AS (
SELECT Customer, sum(Revenue) AS total
FROM input
GROUPBY Customer
)
SELECT i.Region, max(c.total) AS result
FROM input i JOIN c USING (Customer)
GROUPBY i.Region
ORDERBY i.Region
Region
Tableau
Tempting SQL
SQL that matches
North
300
300
300
South
300
200
300
West
450
400
450
✓ 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 c AS (
SELECT Customer, SUM(Revenue) AS total
FROM input
GROUPBY Customer
)
SELECT i.Region, MAX(c.total) AS result
FROM input i JOIN c USING (Customer)
GROUPBY i.Region
ORDERBY i.Region
WITH c AS (
SELECT Customer, SUM(Revenue) AS total
FROM input
GROUPBY Customer
)
SELECT i.Region, MAX(c.total) AS result
FROM input i JOIN c USING (Customer)
GROUPBY i.Region
ORDERBY i.Region
What do people ask about Nested largest customer 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.
Is this the same as MAX({ INCLUDE [Customer] : SUM([Revenue]) })?
In a Region view, yes, here. The nested FIXED keeps the region value on every row and ignores the view; the INCLUDE version follows the view and dimension filters.
Can I flag the customer that holds the record?
Yes, row by row: { FIXED [Customer] : SUM([Revenue]) } = { FIXED [Region] : MAX({ FIXED [Customer] : SUM([Revenue]) }) } is true on that customer's rows.