Search code examples
ssas

Ignore non-empty values when aggregating


I have a cube I've built with three separate measures: "TY Sales", "LY Sales", and "% Change", what I'm trying to do is have special behavior for the aggregate rows, basically not including any "LY Sales" values when summing the total if "TY Sales" is 0. So currently my cube works like below:

        LYSales  TYSales  %Change
Year 1   450      300     -33%
Week 1   100      125     +25%
Week 2   150      175     +14%
Week 3   200       0      +0%

The aggregate column "Year 1" in this example, is summing all values for each sales measure. What I want it to do instead, is only include values in LYSales if TYSales also has a non-zero value. So my ideal state would be below:

        LYSales  TYSales  %Change
Year 1   250      300     +20%
Week 1   100      125     +25%
Week 2   150      175     +14%
Week 3   200       0      +0%

I'm new to SSAS, so any guidance is appreciated. Thanks


Solution

  • An easy and reliable way to achieve that would be to change the source column of LYSales to be zero if TYSales is zero. This would be done in the fact table on which the measure is based. You could implement that

    • either in the ETL process, changing the LYSales column values to be zero when TYSales is zero,
    • or in a view based on the fact table that is then used in the Data Source View instead of the original table,
    • or as a Calculated Calculation of the fact table in the Data Source View.

    In the latter two cases, the calculation formula would be SQL like this:

    case when TYSales <> 0 then LYSales else 0 end
    

    Then switch the measure definition to use that column.