RUNNING_STDEVP: from Tableau to SQL

Calculates stdevp over the current and preceding 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

RUNNING_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 CURRENT ROW)) AS DOUBLE) AS result FROM input ORDER BY "Region", "Period"

Runs the example above.

Result

RegionPeriodRevenueCostresult
Harbor1120700
Harbor218010030
Harbor318012028.2843
Harbor424014042.4264
Upland190500
Upland215011030
Upland3null9030
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 CURRENT ROW)) AS DOUBLE) AS result FROM input ORDER BY "Region", "Period"
    RegionPeriodRevenueCostresult
    Harbor1120700
    Harbor218010037.4166
    Harbor318012034.9857
    Harbor424014047.9583
    Upland1905015
    Upland215011033.541
    Upland3null9034.9857
    Upland433019074.2307

    Matches the calculation

    SELECT CAST((stddev_pop("Revenue") OVER (PARTITION BY "Region" ORDER BY "Period" ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS DOUBLE) AS result FROM input ORDER BY "Region", "Period"
    RegionPeriodRevenueCostresult
    Harbor1120700
    Harbor218010030
    Harbor318012028.2843
    Harbor424014042.4264
    Upland190500
    Upland215011030
    Upland3null9030
    Upland4330190101.98

Signature

RUNNING_STDEVP(expression)