Function table / Level of detail / Median customer revenue

Median customer revenue, from Tableau to SQL

Aggregates customer totals once within each region. Below, 8 sample rows go through the calculation and through the SQL, and both return the same numbers.

Calculated field

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

Sample data

RegionTeamCustomerRevenueSeatsDate
NorthCedarAster12042026-01-05
NorthCedarAster8022026-02-05
NorthCedarBirch30062026-01-12
NorthPineClover9032026-02-14
SouthElmDahlia20052026-01-08
SouthElmDahlia10022026-02-08
SouthAshFern7532026-01-20
SouthAshFern2512026-02-20

The same calculation as SQL

WITH customers AS (SELECT Region, Customer, SUM(Revenue) total FROM input GROUP BY Region, Customer) SELECT Region, MEDIAN(total) FROM customers GROUP BY Region ORDER BY Region

Runs the example above.

Result

Regionresult
North200
South200

Same rows from the calculation and from the SQL in DuckDB.

Where a naive translation goes wrong

  • MEDIAN of invoices differs from the median of summed customer totals.

    Naive SQL

    SELECT "Region", MEDIAN(Revenue) AS result FROM input  GROUP BY "Region" ORDER BY "Region"
    Regionresult
    North105
    South87.5

    Matches the calculation

    WITH customers AS (SELECT Region, Customer, SUM(Revenue) total FROM input GROUP BY Region, Customer) SELECT Region, MEDIAN(total) FROM customers GROUP BY Region ORDER BY Region
    Regionresult
    North200
    South200

Signature

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