简体   繁体   中英

How to restructure MDX to group calculated measures under new row label

I the following MDX that queries a set of calculated measures for two metrics over a date range (months):

WITH
MEMBER [Measures].[Prior Visits Office New] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211].Lag(12) : [Date Post Transaction].[Calendar Month Period].&[201306].Lag(12)},[Measures].[Visits Office New]),format_string = '#,##0' 
MEMBER [Measures].[Current Visits Office New] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211] : [Date Post Transaction].[Calendar Month Period].&[201306]},[Measures].[Visits Office New]),format_string = '#,##0' 
MEMBER [Measures].[Diff Visits Office New] as ([Measures].[Current Visits Office New] - [Measures].[Prior Visits Office New]),format_string = '#,##0' 
MEMBER [Measures].[Percent Change Visits Office New] as ([Measures].[Diff Visits Office New] / [Measures].[Prior Visits Office New]),format_string = 'Percent'
MEMBER [Measures].[Prior Visits Office Established] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211].Lag(12) : [Date Post Transaction].[Calendar Month Period].&[201306].Lag(12)},[Measures].[Visits Office Established]),format_string = '#,##0' 
MEMBER [Measures].[Current Visits Office Established] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211] : [Date Post Transaction].[Calendar Month Period].&[201306]},[Measures].[Visits Office Established]),format_string = '#,##0' 
MEMBER [Measures].[Diff Visits Office Established] as ([Measures].[Current Visits Office Established] - [Measures].[Prior Visits Office Established]),format_string = '#,##0' 
MEMBER [Measures].[Percent Change Visits Office Established] as ([Measures].[Diff Visits Office Established] / [Measures].[Prior Visits Office Established]),format_string = 'Percent'
SELECT {[Measures].[Current Visits Office New],[Measures].[Prior Visits Office New],[Measures].[Diff Visits Office New],[Measures].[Percent Change Visits Office New],[Measures].[Current Visits Office Established],[Measures].[Prior Visits Office Established],[Measures].[Diff Visits Office Established],[Measures].[Percent Change Visits Office Established]}
ON COLUMNS , NON EMPTY Hierarchize(AddCalculatedMembers({DrilldownLevel({[Date Post Transaction].[Calendar Month Period].Children})}))  
ON ROWS FROM (SELECT ({[Date Post Transaction].[Calendar Month Period].&[201301]}) 
ON COLUMNS FROM [cube])
WHERE ([Report Group].[Report Group1].&[Group])

The result set looks like this:

+-----------------------------+-----------------------------+---------------------------+--------------------------+------------------------------------+-------------------------------------+-----------------------------------+---------------------------------+--------------------------------------------+
|                             |  Current Visits Office New  |  Prior Visits Office New  |  Diff Visits Office New  |  Percent Change Visits Office New  |  Current Visits Office Established  |  Prior Visits Office Established  |  Diff Visits Office Established |  Percent Change Visits Office Established  |
+-----------------------------+-----------------------------+---------------------------+--------------------------+------------------------------------+-------------------------------------+-----------------------------------+---------------------------------+--------------------------------------------+
|            201301           |            4,793            |           4,307           |             486          |               11.28%               |                 58,979              |                57,228             |               1,751             |                    3.06%                   |
+-----------------------------+-----------------------------+---------------------------+--------------------------+------------------------------------+-------------------------------------+-----------------------------------+---------------------------------+--------------------------------------------+

I just have the [Date Post Transaction].[Calendar Month Period].&[201301] in there, which displays the row 201301 , as a filler. It doesn't seem to return a result set without a row axis label there.

The format that I need to get the result set into looks like this:

+---------------------------------+----------------------+---------------------+-----------------------+----------------------+
|                                 |        Current       |        Prior        |          Diff         |    Percent Change    |  
+---------------------------------+----------------------+---------------------+-----------------------+----------------------+
|    Visits Office New            |         4,793        |        4,307        |           486         |        11.28%        |
+---------------------------------+----------------------+---------------------+-----------------------+----------------------+
|    Visits Office Established    |        58,979        |        57,228       |          1,751        |        3.06%         |
+---------------------------------+----------------------+---------------------+-----------------------+----------------------+

or even better:

+---------------------------------+----------------------+---------------------+-----------------------+----------------------+
|    Visits Office New            |         4,793        |        4,307        |           486         |        11.28%        |
+---------------------------------+----------------------+---------------------+-----------------------+----------------------+
|    Visits Office Established    |        58,979        |        57,228       |          1,751        |        3.06%         |
+---------------------------------+----------------------+---------------------+-----------------------+----------------------+

So, I was thinking about some logic that works to group the calculated measures like this:

WITH
MEMBER [Measures].[Visits Office New] as (
    MEMBER [Measures].[Prior Visits Office New] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211].Lag(12) : [Date Post Transaction].[Calendar Month Period].&[201306].Lag(12)},[Measures].[Visits Office New]),format_string = '#,##0' 
    MEMBER [Measures].[Current Visits Office New] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211] : [Date Post Transaction].[Calendar Month Period].&[201306]},[Measures].[Visits Office New]),format_string = '#,##0' 
    MEMBER [Measures].[Diff Visits Office New] as ([Measures].[Current Visits Office New] - [Measures].[Prior Visits Office New]),format_string = '#,##0' 
    MEMBER [Measures].[Percent Change Visits Office New] as ([Measures].[Diff Visits Office New] / [Measures].[Prior Visits Office New]),format_string = 'Percent' 
)
MEMBER [Measures].[Visits Office Established] as (
    MEMBER [Measures].[Prior Visits Office Established] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211].Lag(12) : [Date Post Transaction].[Calendar Month Period].&[201306].Lag(12)},[Measures].[Visits Office Established]),format_string = '#,##0'
    MEMBER [Measures].[Current Visits Office Established] as SUM({[Date Post Transaction].[Calendar Month Period].&[201211] : [Date Post Transaction].[Calendar Month Period].&[201306]},[Measures].[Visits Office Established]),format_string = '#,##0' 
    MEMBER [Measures].[Diff Visits Office Established] as ([Measures].[Current Visits Office Established] - [Measures].[Prior Visits Office Established]),format_string = '#,##0' 
    MEMBER [Measures].[Percent Change Visits Office Established] as ([Measures].[Diff Visits Office Established] / [Measures].[Prior Visits Office Established]),format_string = 'Percent'
)
SELECT {[Measures].[Current Visits Office New],[Measures].[Prior Visits Office New],[Measures].[Diff Visits Office New],[Measures].[Percent Change Visits Office New],[Measures].[Current Visits Office Established],[Measures].[Prior Visits Office Established],[Measures].[Diff Visits Office Established],[Measures].[Percent Change Visits Office Established]}
ON COLUMNS , NON EMPTY Hierarchize(AddCalculatedMembers({DrilldownLevel({[Date Post Transaction].[Calendar Month Period].Children})}))  
ON ROWS FROM (SELECT ({[Date Post Transaction].[Calendar Month Period].&[201301]}) 
ON COLUMNS FROM [cube])
WHERE ([Report Group].[Report Group1].&[Group])

But this doesn't work. Thoughts?

Using any hierarchy not used so far in your query (i will use [Dim1].[Util] in my example, as I do not know your cube), you would create members on this - as you can create calculated members on any hierarchy, not just on the Measures hierarchy:

WITH member [Dim1].[Util].[Prior] as
     SUM({[Date Post Transaction].[Calendar Month Period].&[201211].Lag(12) : [Date Post Transaction].[Calendar Month Period].&[201306].Lag(12)},
         [Measures].CurrentMember
        ),format_string = '#,##0'
     member [Dim1].[Util].[Current] as
     SUM({[Date Post Transaction].[Calendar Month Period].&[201211] : [Date Post Transaction].[Calendar Month Period].&[201306]},
         [Measures].CurrentMember
        ),format_string = '#,##0'
     member [Dim1].[Util].[Diff] as
     [Dim1].[Util].[Current] - member [Dim1].[Util].[Prior],format_string = '#,##0'
     member [Dim1].[Util].[Percent Change] as
     [Dim1].[Util].[Diff] / [Dim1].[Util].[Current],format_string = 'Percent'
SELECT {
       [Dim1].[Util].[Prior],
       [Dim1].[Util].[Current],
       [Dim1].[Util].[Diff],
       [Dim1].[Util].[Percent Change]
       }
ON COLUMNS ,
       {
       [Measures].[Visits Office New],
       [Measures].[Visits Office Established]
       }
ON ROWS
  FROM (SELECT ({[Date Post Transaction].[Calendar Month Period].&[201301]}) 
               ON COLUMNS
          FROM [cube])
WHERE ([Report Group].[Report Group1].&[Group])

There is no specific requirement on the hierarchy to use in the columns, except that it should not otherwise be used in your query. Some cube designers explicitly create one or even two utility dimensions in each cube, which contain just one dummy member in order to be able to easily write queries like this. These dimensions are then just used to create calculated members on them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM