WINDOW_STDEVP: from Tableau to SQL

Calculates stdevp 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_STDEVP(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((stddev_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
Harbor11207042.4264
Harbor218010042.4264
Harbor318012042.4264
Harbor424014042.4264
Upland19050101.98
Upland2150110101.98
Upland3null90101.98
Upland4330190101.98

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((stddev_pop("Revenue") OVER ( ORDER BY "Period" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) AS DOUBLE) AS result FROM input ORDER BY "Region", "Period"
    RegionPeriodRevenueCostresult
    Harbor11207074.2307
    Harbor218010074.2307
    Harbor318012074.2307
    Harbor424014074.2307
    Upland1905074.2307
    Upland215011074.2307
    Upland3null9074.2307
    Upland433019074.2307

    Matches the calculation

    SELECT CAST((stddev_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
    Harbor11207042.4264
    Harbor218010042.4264
    Harbor318012042.4264
    Harbor424014042.4264
    Upland19050101.98
    Upland2150110101.98
    Upland3null90101.98
    Upland4330190101.98

Signature

WINDOW_STDEVP(expression, [start, end])