Blog Post

MDX #28– Time functions will not work with subquery

,

I had a post last month, MDX #25 – Slicer or Sub-Cube?, verifying that the query context did not change with the subquery.

This can pose problems if you want to use the Time-series functions in MDX. Two functions come into mind, YTD() and PeriodsToDate().

Both functions need to know which member on the Date dimension we are operating on.

PeriodsToDate and YTD function will not work with a subquery

Let’s try this query, which tests the PeriodsToDate function with a subquery. It will not work with the created member Reseller Sales YTD. Created member [x] is here to show what the current member on the Datedimension is.

–PeriodsToDate will not work with the subquery
WITH
MEMBER [Measures].[Reseller Sales YTD] AS
   Aggregate(
        PeriodsToDate(
                [Date].[Calendar].[Calendar Year],
                [Date].[Calendar].CurrentMember
                ),
              [Measures].[Reseller Sales Amount]
            )
MEMBER [Measures].[x] AS
    membertostr([Date].[Calendar].CurrentMember)
SELECT
   { [Measures].[Reseller Sales Amount],
     [Measures].[Reseller Sales YTD],
     [Measures].[x] } ON 0
FROM
   ( select   
        { [Date].[Calendar].[Month].[March 2008]:
          [Date].[Calendar].[Month].[April 2008]
        } on 0
     from    [Adventure Works]
    )

image

PeriodsToDate and YTD function will work with modification

The fault is not the subquery, rather it’s because we did not provide the query context.

We can modify the above query to provide the context by adding a member expression [Date].[Calendar].[Month].MEMBERS to the ROWS axis.

– PeriodsToDate works with [Date].[Calendar].[Month].MEMBERS on the ROWS axis
WITH
MEMBER [Measures].[Reseller Sales YTD] AS
   Aggregate(
        PeriodsToDate(
                [Date].[Calendar].[Calendar Year],
                [Date].[Calendar].CurrentMember
                ),
              [Measures].[Reseller Sales Amount]
            )
MEMBER [Measures].[x] AS
    membertostr([Date].[Calendar].CurrentMember)
SELECT
   { [Measures].[Reseller Sales Amount],
     [Measures].[Reseller Sales YTD],
     [Measures].[x] } ON 0,
   { [Date].[Calendar].[Month].MEMBERS } ON 1
FROM
   ( select   
        { [Date].[Month Name].[Month Name].[March 2008]:
          [Date].[Month Name].[Month Name].[April 2008]
        } on 0
     from    [Adventure Works]
    )

Our result is showing both months on the ROWS axis, and with the correct YTD reseller sales for each month.

image

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating