Monday, July 28, 2014

MDX using member

The member function in MDX is comparable with the MS-SQL CTE functionality. It allows you to define a member before you use it in your query and thus keeping your query more readable.
An example:
WITH MEMBER [Measures].[Prior Year Sales] AS
    SUM(     
        ParallelPeriod
            (
                [Date].[Calendar].[Calendar Year],
                1,
                [Date].[Calendar].CurrentMember
            )
            ,
            [Measures].[Sales Amount]
       )
This calculates the SUM of the Sales Amount for the previous year using the ParallelPeriod functionality in combination with CurrentMember.

Next use it in our query:
select
    {             
          [Measures].[Sales Amount]
        , [Measures].[Prior Year Sales]
       
    } ON COLUMNS
    ,
    NON EMPTY
    {
        (
         [Date].[Calendar Year].Children
         )
    } ON ROWS
FROM
[Adventure Works]
This will give us:
image
It automatically adjust it self for the date level we are using. Let’s add the quarters:
select
    {             
          [Measures].[Sales Amount]
        , [Measures].[Prior Year Sales]
       
    } ON COLUMNS
    ,
    NON EMPTY
    {
        (
         [Date].[Calendar Year].Children
        ,[Date].[Calendar Quarter of Year].children
         )
    } ON ROWS
FROM
[Adventure Works]
This will get us:
image
Have Fun And Till Next Time

No comments:

Post a Comment