Search code examples
mdxslicers

MDX - Calculated MTD measure returning null value


I have a Cube with a Date dimension hierarchy (Year,Semester,Quarter,Month,Day).

There are 2 measures available in the datawarehouse.

[AUD DLY] : Daily numbers
[AUD YTD] : Daily incremental YTD numbers

Hierarchy is as follows

Date - Dimension 
Financial - Hierarchy 

Year - [Date].[Financial].[Year]
Semester - [Date].[Financial].[Semester]
Quarter - [Date].[Financial].[Quarter] 
Month - [Date].[Financial].[Month] 
Day - [Date].[Financial].[Day]

Measures [AUD DLY]
Measures [AUD YTD]

I need to add a MTD field in the measures such that when the business select a particular date on their slicer in excel for e.g., 3 March 2016, MTD should be calculated as either of the following ways :

1) [AUD MTD] should be calculated by subtracting [AUD YTD] on 
last day of  previous month from the current selected date. 
So if we select 3 March 2016 then

[AUD MTD] = [AUD YTD] on 3 March 2016 - [AUD YTD] on 29 Feb 2016

OR

2) [AUD MTD] should be calculated by adding the [AUD DLY] from first day
of the current month until the selected date in that month. 
So if we select 3 March 2016 then

[AUD MTD] = SUM ([AUD DLY] from 1 March 2016 to 3 March 2016)

I created a New Calculated Member from Calculations tab in the Cube designer in the BIDS 2010. The MDX query is below. However when I try to browse the cube the [AUD MTD] values are only returning nulls.

Can someone please help what am I doing wrong ?

CREATE MEMBER CURRENTCUBE.[Measures].[AUD MTD]
AS Aggregate
(
    PeriodsToDate
        (
        [Date].[Financial].[Month]
        ,[Date].[Financial].CurrentMember
        )
        ,[Measures].[AUD DLY]
), 
FORMAT_STRING = "Currency", 
NON_EMPTY_BEHAVIOR = { [AUD DLY] }, 
VISIBLE = 1 ,  DISPLAY_FOLDER = 'AUD Values' ,  ASSOCIATED_MEASURE_GROUP = 'Measures'  ; 

Also the business would be using the new calculated measure in excel using the Slicer, they want to select any date and be able to view the MTD value for that month.

Also can someone please help with MDX query for both methods (1) and (2) ?

Your help much appreciated.


Solution

  • I think the problem with your quoted code (method 2) is that [Date].[Financial].CurrentMember must be at a level at or below the [Date].[Financial].[Month] level. Otherwise PeriodsToDate returns an empty set.

    So the problem is not in your calculated member definition, but somewhere in the query in which it's being used. In that query, [Date].[Financial].CurrentMember may be returning a member at a level above Months. Hard to see without seeing the query itself.

    Method (1) is more fiddly. You can get the last day of the previous month with

    Ancestor([Date].[Financial].CurrentMember,[Date].[Financial].[Month]).PrevMember.LastChild
    

    but you'd have to build in some logic for days in the first month of the financial year, which would otherwise subtract the value for a previous fin year from this year's YTD value. So I'd recommend method (2).

    As far as I know selecting a date (I mean a date, not a month) in the Excel slicer will make that date the .CurrentMember. I'm a bit hesitant because Excel does generate some deeply bizarre MDX sometimes.

    EDIT: Another possible problem is a hierarchy mismatch. You can select a perfectly good Day in a date hierarchy, but if it isn't in exactly the hierarchy you specify in your calculated member definition, you can get weird results. IMHO more recent versions of SSAS encourage a proliferation of attribute hierarchies and multiple "real" hierarchies, making this a real problem.