Tableau function Table calculation

Tableau WINDOW_MEDIAN to SQL

WINDOW_MEDIAN(SUM([Sales])) returns the median of the expression across the marks in the partition; with an even number of marks it averages the middle two. In SQL that is median(sum(Sales)) OVER (PARTITION BY Region) in the grouped query. A median over the raw rows is a different number.

Updated · Checked on the xmetric engine

What is the syntax?

WINDOW_MEDIAN(expression, [start, end])
expression
an aggregate, such as SUM([Sales]).
start, end
optional offsets from the current mark that bound the window; without them it is the whole partition. FIRST() and LAST() work here.

Returns a number: the median of the expression over the marks in the window.

What does it return, and what is the SQL?

Each example runs on the same 9 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

What is the median month of each region?

Tableau

WINDOW_MEDIAN(SUM([Sales]))

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  median(sum(Sales)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Month: East's marks are 60, 100, 120, 120, so the median is (100 + 120) / 2 = 110. West's is 70.

RegionMonthResult
East1110
East2110
East3110
East4110
West170
West270
West370
West470

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

How do I compute it across regions, within each month?

Tableau

WINDOW_MEDIAN(SUM([Sales]))

Compute using Region, restarting for each Month

SQL (DuckDB)

SELECT
  Region,
  Month,
  median(sum(Sales)) OVER (PARTITION BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Region: the window is now the two regions of each month.

RegionMonthResult
East175
East295
East360
East4105
West175
West295
West360
West4105

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

What does the bounded form return?

With start -1 and end 1, the window moves with the current mark and is clipped at the edges of each region. Each result column is one mark, by Region and Month.

CaseTableau, then SQL (DuckDB)East 1East 2East 3East 4West 1West 2West 3West 4
bounded (-1, 1)WINDOW_MEDIAN(SUM([Sales]), -1, 1)Region, Month, median(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)1101001209060608090

On every row, each Tableau formula and its SQL return the same value.

Where does a straight translation go wrong?

Common mistake

Why does the SQL median give East 80?

A median over the source rows sees East's five rows (100, 80, 40, 60, 120), whose middle value is 80. WINDOW_MEDIAN works on the four marks, where month 2 is one value of 120.

Tableau

WINDOW_MEDIAN(SUM([Sales]))

Compute using Month, restarting for each Region

Tempting SQL

SELECT Region, Month, any_value(m) AS result
FROM (
  SELECT
    Region,
    Month,
    median(Sales) OVER (PARTITION BY Region) AS m
  FROM input
)
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  median(sum(Sales)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East111080110
East211080110
East311080110
East411080110
West1707070
West2707070
West3707070
West4707070

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,
  Month,
  MEDIAN(s) OVER (PARTITION BY Region) AS result
FROM (
  SELECT Region, Month, SUM(Sales) AS s
  FROM input
  GROUP BY Region, Month
)
ORDER BY Region, Month

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  PERCENTILE_CONT(s, 0.5) OVER (PARTITION BY Region) AS result
FROM (
  SELECT Region, Month, SUM(Sales) AS s
  FROM input
  GROUP BY Region, Month
)
ORDER BY Region, Month

BigQuery documentation

What does your own WINDOW_MEDIAN formula return?

Edit the formula and run it on the 9 sample rows. Fields: [id], [Region], [Month], [Sales], [Profit].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about WINDOW_MEDIAN?

Why does the expression need SUM inside it?

A table calculation runs on the marks in the view, so its argument must be an aggregate. A bare field such as [Sales] raises an error; see All fields must be aggregate or constant.

Why no ORDER BY in the SQL?

Without start and end, a WINDOW_ function covers the whole partition. A SQL window with ORDER BY and no frame ends at the current row, so leave ORDER BY out, or write ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.

What do the start and end arguments mean?

They are offsets from the current mark: -1, 1 covers the previous, current and next marks, and FIRST(), 0 runs from the first mark to the current one. In SQL they become ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING. Use ROWS, not RANGE: Tableau counts marks, and RANGE counts values of the ORDER BY column.

Can Snowflake or BigQuery use start and end with a median?

No. Snowflake's MEDIAN and BigQuery's PERCENTILE_CONT accept only PARTITION BY as a window, no frame. For a moving median there, join each mark to its neighbours and take the median per group.

Where is this documented?