Tableau function Level of detail

Tableau median customer revenue (INCLUDE) to SQL

MEDIAN({ INCLUDE [Customer] : SUM([Revenue]) }) adds up each customer's revenue within the view's marks, then takes the MEDIAN of those customer totals. In SQL that is two levels of grouping: sum per Region and Customer in a subquery, then median() per Region. Taking median(Revenue) straight from the rows answers a different question, about invoice lines.

Updated · Checked on the xmetric engine

What is the syntax?

MEDIAN({ INCLUDE [Customer] : SUM([Revenue]) })
dimensions
what to add to the view's grain, here [Customer].
aggregate
an aggregate expression, such as SUM([Revenue]).
MEDIAN
the outer aggregate Tableau applies across the customer totals; it defaults to SUM, so set MEDIAN on the pill or in the formula.

Returns a number per mark: the MEDIAN 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 median customer spend in each region?

Tableau

MEDIAN({ INCLUDE [Customer] : SUM([Revenue]) })

SQL (DuckDB)

SELECT Region, median(total) AS result
FROM (
  SELECT Region, Customer, sum(Revenue) AS total
  FROM input
  GROUP BY Region, Customer
)
GROUP BY Region
ORDER BY Region

North's customers spend 90, 260 and 300: the median is 260.

RegionResult
North260
South150
West450

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

Where does a straight translation go wrong?

Common mistake

Why does median(Revenue) give a different number?

Aggregating the raw rows gives the median invoice line, 90 for North. INCLUDE first adds up each customer's revenue, then takes the MEDIAN of those totals.

Tableau

MEDIAN({ INCLUDE [Customer] : SUM([Revenue]) })

Tempting SQL

SELECT Region, median(Revenue) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, median(total) AS result
FROM (
  SELECT Region, Customer, sum(Revenue) AS total
  FROM input
  GROUP BY Region, Customer
)
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
North26090260
South150100150
West450225450

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, MEDIAN(total) AS result
FROM (
  SELECT Region, Customer, SUM(Revenue) AS total
  FROM input
  GROUP BY Region, Customer
)
GROUP BY Region
ORDER BY Region

Snowflake documentation

BigQuery

SELECT
  DISTINCT Region,
  PERCENTILE_CONT(total, 0.5) OVER (PARTITION BY Region) AS result
FROM (
  SELECT Region, Customer, SUM(Revenue) AS total
  FROM input
  GROUP BY Region, Customer
)
ORDER BY Region

BigQuery documentation

What does your own Median customer revenue 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 Median 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; MEDIAN gives the median 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.

Where is this documented?