SIZE: from Tableau to SQL

Returns the number of marks in each regional partition, including null measures. Below, 8 sample rows go through the calculation and through the SQL, and both return the same numbers.

Calculated field

SIZE()

Compute using: addressing Period; partitioning Region

Sample data

RegionPeriodRevenueCost
Harbor112070
Harbor2180100
Harbor3180120
Harbor4240140
Upland19050
Upland2150110
Upland3null90
Upland4330190

The same calculation as SQL

SELECT CAST((count(*) 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
Harbor1120704
Harbor21801004
Harbor31801204
Harbor42401404
Upland190504
Upland21501104
Upland3null904
Upland43301904

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((count(*) OVER ( ORDER BY "Period" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) AS DOUBLE) AS result FROM input ORDER BY "Region", "Period"
    RegionPeriodRevenueCostresult
    Harbor1120708
    Harbor21801008
    Harbor31801208
    Harbor42401408
    Upland190508
    Upland21501108
    Upland3null908
    Upland43301908

    Matches the calculation

    SELECT CAST((count(*) 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
    Harbor1120704
    Harbor21801004
    Harbor31801204
    Harbor42401404
    Upland190504
    Upland21501104
    Upland3null904
    Upland43301904

Signature

SIZE()