Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Sherry Li's BI Corner

Always wanting to publish novels, but having the fortune to work in the data warehouse corner of technology, Sherry Li started to write the mysteries of the Microsoft Business Intelligence. She writes everything from T-SQL to MDX, ETL to Expressions to Scripting, Reporting to Cubes. You can find her writings at bisherryli.wordpress.com.

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


Comments

Leave a comment on the original post [bisherryli.com, opens in a new window]

Loading comments...