Search code examples
ssasaggregatemdxcube

Calculating the number of days in a time dimension node - with Grand Total


I need to know the number of days in a time dimension period for calculating weighted averages. I am using the following expression in a calculated measure to obtain the number of days in the current dimension member:

Count(
    Descendants(
        [Date].[Calendar].CurrentMember,
        [Date].[Calendar].[Date Key]
    )   
)

This works fine for all drill-down situations, but it does not work for the Grand Total when I have a filter. I suspect that CurrentMember does not work in this situation. It always returns the total number of days in my data. To illustrate, the measure with the above formula is aggregated in BIDS as follows

Browsing the cube in BIDS

because my fact data starts in 1984 and there are 11100 days in the time dimension. How can I change the formula so that the filter is accounted for in the aggregation? Users can drill down to the Day level. Here is the Excel Pivot table:

Browsing the cube in Excel


Solution

  • I believe that this is a classic case of the "Multiselect Issue with Calculated Members". I found a good summary here. I have a solution that works in Excel. It does not calculate the proper totals in BIDS, but that's not an issue in this case.

    I use the following dynamic set (this is the Script view on the Calculations tab in the cube editor):

    CALCULATE;     
    
    CREATE MEMBER CURRENTCUBE.[Measures].[Days In Period Count]
    AS Count(Existing [Filtered Date]), VISIBLE = 1  ; 
    
    CREATE DYNAMIC SET CURRENTCUBE.[Filtered Date]
    AS [Date].[Calendar].[Date Key] ; 
    

    My interpretation is that SSAS normally calculates the aggregate in advance and that [Date].[Calendar].CurrentMember is not meaningful at the Grand Total level outside of the context of a user's drill-down and filter action. By using a dynamic set, I am forcing SSAS to calculate the aggregate dynamically. There is a theoretical performance hit, but with my cube size I did not notice much of a difference.

    Then BIDS shows the following totals - no change from above yet:

    Browsing in BIDS

    But Excel has the correct totals that reflect only the filtered date coordinates (in this case, only Q1 of 2012 and the entire year of 2013):

    enter image description here