Tableau smallest customer revenue (INCLUDE) to SQL
MIN({ INCLUDE [Customer] : SUM([Revenue]) }) adds up each customer's revenue within the view's marks, then takes the MIN of those customer totals. In SQL that is two levels of grouping: sum per Region and Customer in a subquery, then min() per Region. Taking min(Revenue) straight from the rows answers a different question, about invoice lines.
Updated · Checked on the xmetric engine
What is the syntax?
MIN({ INCLUDE [Customer] : SUM([Revenue]) })
dimensions
what to add to the view's grain, here [Customer].
aggregate
an aggregate expression, such as SUM([Revenue]).
MIN
the outer aggregate Tableau applies across the customer totals; it defaults to SUM, so set MIN on the pill or in the formula.
Returns a number per mark: the MIN of the customer totals inside it.
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 does the smallest customer spend in each region?
Tableau
MIN({ INCLUDE [Customer] : SUM([Revenue]) })
SQL (DuckDB)
SELECT Region, min(total) AS result
FROM (
SELECT Region, Customer, sum(Revenue) AS total
FROM input
GROUPBY Region, Customer
)
GROUPBY Region
ORDERBY Region
North's customers spend 260, 300 and 90: the smallest is Clover's 90.
Region
Result
North
90
South
100
West
450
✓ The Tableau formula and the SQL return the same 3 rows.
Where does a straight translation go wrong?
Common mistake
Why does min(Revenue) give a different number?
Aggregating the raw rows gives the smallest invoice line, 60 for North. INCLUDE first adds up each customer's revenue, then takes the MIN of those totals.
Tableau
MIN({ INCLUDE [Customer] : SUM([Revenue]) })
Tempting SQL
SELECT Region, min(Revenue) AS result
FROM input
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, min(total) AS result
FROM (
SELECT Region, Customer, sum(Revenue) AS total
FROM input
GROUPBY Region, Customer
)
GROUPBY Region
ORDERBY 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
SELECT Region, MIN(total) AS result
FROM (
SELECT Region, Customer, SUM(Revenue) AS total
FROM input
GROUPBY Region, Customer
)
GROUPBY Region
ORDERBY Region
SELECT Region, MIN(total) AS result
FROM (
SELECT Region, Customer, SUM(Revenue) AS total
FROM input
GROUPBY Region, Customer
)
GROUPBY Region
ORDERBY Region
What do people ask about Smallest customer revenue?
Do filters change it?
Yes. INCLUDE is computed after dimension filters, so lines removed by a filter drop out of the totals before they are aggregated. A FIXED version would ignore the filter.
Why does the outer aggregate matter?
INCLUDE is finer than the view, so Tableau aggregates its values up to each mark. SUM gives back the region total; MIN gives the smallest customer.
How is this different from FIXED [Customer]?
FIXED counts a customer's revenue across the whole data source; INCLUDE only within the mark. They differ as soon as a customer spans marks, like Aster across two teams. See Fixed customer lifetime revenue.