Tableau function Table calculation

Tableau WINDOW_PERCENTILE to SQL

WINDOW_PERCENTILE(SUM([Sales]), 0.25) returns the 25th percentile of the expression across the marks in the partition, interpolating between marks. In SQL that is quantile_cont(sum(Sales), 0.25) OVER (PARTITION BY Region). The discrete version, quantile_disc (or PERCENTILE_DISC), picks an actual mark and gives a different number.

Updated · Checked on the xmetric engine

What is the syntax?

WINDOW_PERCENTILE(expression, number, [start, end])
expression
an aggregate, such as SUM([Sales]).
number
the percentile, from 0 to 1: 0.25 is the 25th percentile.

Returns a number: the given percentile of the expression over the marks in the window, interpolated.

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 25th percentile of each region's months?

Tableau

WINDOW_PERCENTILE(SUM([Sales]), 0.25)

Compute using Month, restarting for each Region

SQL (DuckDB)

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

Compute using Month: East's marks sorted are 60, 100, 120, 120; the 25th percentile sits three quarters of the way from 60 to 100, at 90. West's three marks give 60.

RegionMonthResult
East190
East290
East390
East490
West160
West260
West360
West460

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

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

Tableau

WINDOW_PERCENTILE(SUM([Sales]), 0.25)

Compute using Region, restarting for each Month

SQL (DuckDB)

SELECT
  Region,
  Month,
  quantile_cont(sum(Sales), 0.25) 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
East162.5
East282.5
East360
East497.5
West162.5
West282.5
West360
West497.5

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_PERCENTILE(SUM([Sales]), 0.25, -1, 1)Region, Month, quantile_cont(sum(Sales), 0.25) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)10580907555557590

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 return an actual month's value?

quantile_disc returns the first mark at or above the percentile: 60 for East. WINDOW_PERCENTILE interpolates between marks, which is quantile_cont.

Tableau

WINDOW_PERCENTILE(SUM([Sales]), 0.25)

Compute using Month, restarting for each Region

Tempting SQL

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

SQL that matches

SELECT
  Region,
  Month,
  quantile_cont(sum(Sales), 0.25) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1906090
East2906090
East3906090
East4906090
West1605060
West2605060
West3605060
West4605060

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,
  PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY 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.25) 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_PERCENTILE 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_PERCENTILE?

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 percentile?

No. As a window, their PERCENTILE_CONT takes only PARTITION BY. A moving percentile needs a self-join there.

Where is this documented?