Tableau nested FIXED smallest customer per region to SQL
{ FIXED [Region] : MIN({ 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 MIN of those per region. In SQL: total per customer in a CTE, join it to the rows, then min() 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, min(c.total) AS result
FROM input i JOIN c USING (Customer)
GROUPBY i.Region
ORDERBY i.Region
North's smallest customer is Clover at 90, South's Fern at 100.
Region
Result
North
90
South
100
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, min(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
90
North
Pine
90
South
Ash
100
South
Elm
100
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 min(Revenue) give a different number?
It returns the smallest invoice line: 60 for North. The nested expression compares customer totals, so it adds each customer's lines first.
SELECT Region, min(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, min(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
90
60
90
South
100
25
100
West
450
50
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, MIN(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, MIN(c.total) AS result
FROM input i JOIN c USING (Customer)
GROUPBY i.Region
ORDERBY i.Region
What do people ask about Nested smallest 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 MIN({ 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] : MIN({ FIXED [Customer] : SUM([Revenue]) }) } is true on that customer's rows.