Function table / Table calculations / WINDOW_PERCENTILE

WINDOW_PERCENTILE: from Tableau to SQL

Returns the interpolated 25th percentile of non-null regional mark revenues. Below, 8 sample rows go through the calculation and through the SQL, and both return the same numbers.

Calculated field

WINDOW_PERCENTILE(SUM([Revenue]), 0.25)

Compute using: addressing Period; partitioning Region

Sample data

RegionPeriodRevenueCost
Harbor112070
Harbor2180100
Harbor3180120
Harbor4240140
Upland19050
Upland2150110
Upland3null90
Upland4330190

The same calculation as SQL

SELECT CAST((quantile_cont("Revenue",0.25) OVER (PARTITION BY "Region" ORDER BY "Period" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) AS DOUBLE) AS result FROM input ORDER BY "Region", "Period"

Runs the example above.

Result

RegionPeriodRevenueCostresult
Harbor112070165
Harbor2180100165
Harbor3180120165
Harbor4240140165
Upland19050120
Upland2150110120
Upland3null90120
Upland4330190120

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

Where a naive translation goes wrong

  • Omitting the region partition allows a calculation to consume marks from other regions.

    Naive SQL

    SELECT CAST((quantile_cont("Revenue",0.25) OVER ( ORDER BY "Period" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) AS DOUBLE) AS result FROM input ORDER BY "Region", "Period"
    RegionPeriodRevenueCostresult
    Harbor112070135
    Harbor2180100135
    Harbor3180120135
    Harbor4240140135
    Upland19050135
    Upland2150110135
    Upland3null90135
    Upland4330190135

    Matches the calculation

    SELECT CAST((quantile_cont("Revenue",0.25) OVER (PARTITION BY "Region" ORDER BY "Period" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) AS DOUBLE) AS result FROM input ORDER BY "Region", "Period"
    RegionPeriodRevenueCostresult
    Harbor112070165
    Harbor2180100165
    Harbor3180120165
    Harbor4240140165
    Upland19050120
    Upland2150110120
    Upland3null90120
    Upland4330190120

Signature

WINDOW_PERCENTILE(expression, percentile, [start, end])