WINDOW_VARP: from Tableau to SQL

Calculates varp over all regional marks, excluding null revenues. Below, 8 sample rows go through the calculation and through the SQL, and both return the same numbers.

Calculated field

WINDOW_VARP(SUM([Revenue]))

Compute using: addressing Period; partitioning Region

Sample data

RegionPeriodRevenueCost
Harbor112070
Harbor2180100
Harbor3180120
Harbor4240140
Upland19050
Upland2150110
Upland3null90
Upland4330190

The same calculation as SQL

SELECT CAST((var_pop("Revenue") 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
Harbor1120701800
Harbor21801001800
Harbor31801201800
Harbor42401401800
Upland1905010400
Upland215011010400
Upland3null9010400
Upland433019010400

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((var_pop("Revenue") OVER ( ORDER BY "Period" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) AS DOUBLE) AS result FROM input ORDER BY "Region", "Period"
    RegionPeriodRevenueCostresult
    Harbor1120705510.2
    Harbor21801005510.2
    Harbor31801205510.2
    Harbor42401405510.2
    Upland190505510.2
    Upland21501105510.2
    Upland3null905510.2
    Upland43301905510.2

    Matches the calculation

    SELECT CAST((var_pop("Revenue") 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
    Harbor1120701800
    Harbor21801001800
    Harbor31801201800
    Harbor42401401800
    Upland1905010400
    Upland215011010400
    Upland3null9010400
    Upland433019010400

Signature

WINDOW_VARP(expression, [start, end])