Search code examples
ssasmdxolap

Filtering hierarchies in MDX WITH clause


By using the answer of MDX on multiple hierarchical dimensions I have the following MDX query:

with
member L1Y1 as ([Dim Location].[Hierarchy].[Center].&[1],
        [Dim Date].[Calendar Date].[Year].&[2010],
        [Measures].[x])                 
select ([Dim Attribute].[Parent Code].&[10000]) on 0, 
       ({L1Y1}) on 1
from DS

Now the problem is how to filter the L1Y1 based on its children. For example suppose we want to filter it so that only season 2 and month 7 included in the query. The following query output is the same as above and the where clause has no effect:

with
member L1Y1 as ([Dim Location].[Hierarchy].[Center].&[1],
        [Dim Date].[Calendar Date].[Year].&[2010],
        [Measures].[x])                 
select ([Dim Attribute].[Parent Code].&[10000]) on 0, 
       ({L1Y1}) on 1
from DS
where [Dim Date].[Calendar Date].[Season].&[2] and 
      [Dim Date].[Calendar Date].[Month].&[7]

Solution

  • How about:

    with
    member L1Y1 as ([Dim Location].[Hierarchy].[Center].&[1],
            [Dim Date].[Calendar Date].[Year].&[2010],
            [Measures].[x])
    member S2 as ([Dim Location].[Hierarchy].[Center].&[1],
            [Dim Date].[Calendar Date].[Season].&[2],
            [Measures].[x])
    member M7 as ([Dim Location].[Hierarchy].[Center].&[1],
            [Dim Date].[Calendar Date].[Month].&[7],
            [Measures].[x])     
    select ([Dim Attribute].[Parent Code].&[10000]) on 0, 
           (L1Y1, S2, M7) on 1
    from DS
    

    I understand it is a somewhat more laborous approach, but you would get the result. If you otherwise want to get a single value only you can try keeping only the M7 measure.