MDX bucketizing the result set and assign frequency

773 views Asked by At

I have a scenario where a result set has to be bucketed and assign a frequency.

For example, the following MDX query:

WITH MEMBER [MEASURES].[PERC_1] AS
    AGGREGATE ( EXISTING  [DIM CUSTOMER].[CUSTOMER ID].[ALL].CHILDREN,[MEASURES].[AMOUNT])

SELECT
[MEASURES].[PERC_1] ON 0,
[DIM CUSTOMER].[CUSTOMER ID].CHILDREN,[DIM CUSTOMER].[NAME].CHILDREN        
FROM [ANALYSIS DW]
WHERE ([DIM CUSTOMER].[ADDRESS].[ALL])

should return this result:

     Perc1
 C1   10
 C2   0
 C3   20
 C4   30
 C5   40
 C6   50
 C7   50 
 C8   50 
 C9   90
 C10  100

Now, I want this result set to be divided into buckets. If my bucket size is 3, the buckets should be

  • 0-30
  • 31-60
  • 61-100

These buckets are calculated based on the maximum and minimum values of the perc_1 measure above; i.e., 0 is minimum and 100 is maximum. Buckets are calculated as (0+100)/3 -- (0-30, 31-60, 61-100).

Now the results after the frequency distribution on the above result set should look as below -

         frequency
 0-30      4
 31-60     4 
 61-100    2

I will not get the access to the OLTP design/SSAS Cube solution.

1

There are 1 answers

0
FrankPl On BEST ANSWER
WITH SET [0-30 set] AS
         Filter([DIM CUSTOMER].[CUSTOMER ID].CHILDREN,
                [MEASURES].[PERC_1] >= 0 AND [MEASURES].[PERC_1] <= 30
               )
     SET [31-60 set] AS
         Filter([DIM CUSTOMER].[CUSTOMER ID].CHILDREN,
                [MEASURES].[PERC_1] >= 31 AND [MEASURES].[PERC_1] <= 60
               )
     SET [61-100 set] AS
         Filter([DIM CUSTOMER].[CUSTOMER ID].CHILDREN,
                [MEASURES].[PERC_1] >= 61 AND [MEASURES].[PERC_1] <= 100
               )
     MEMBER [DIM CUSTOMER].[CUSTOMER ID].[0-30] AS
            NULL
     MEMBER [DIM CUSTOMER].[CUSTOMER ID].[31-60] AS
            NULL
     MEMBER [DIM CUSTOMER].[CUSTOMER ID].[61-100] AS
            NULL
     MEMBER [Measures].[frequency] AS
            CASE [DIM CUSTOMER].[CUSTOMER ID].CurrentMember
                 WHEN [DIM CUSTOMER].[CUSTOMER ID].[0-30] THEN
                      [0-30 set].Count
                 WHEN [DIM CUSTOMER].[CUSTOMER ID].[31-60] THEN
                      [31-60 set].Count
                 WHEN [DIM CUSTOMER].[CUSTOMER ID].[61-100] THEN
                      [61-100 set].Count
            END
SELECT { [Measures].[frequency] } ON 0,
       { 
         [DIM CUSTOMER].[CUSTOMER ID].[0-30],
         [DIM CUSTOMER].[CUSTOMER ID].[31-60],
         [DIM CUSTOMER].[CUSTOMER ID].[61-100]
       } ON 1
  FROM [ANALYSIS DW]
WHERE ([DIM CUSTOMER].[ADDRESS].[ALL])

I do not think you can do this completely within MDX in a way that the "3" is a parameter to the MDX query, as you need at least to have something like the member definitions for the buckets explicitly in the MDX statement. The WITH clause (or a session level CREATE statement) is the only place where you can create the members shown on the row axis, even if they are just used as placeholders and do not contain any logic.

You would need an external tool to generate a parameter specific MDX statement like that above.