Search code examples
sql-serverssasmdxsql-server-2016

MDX Less Than Equal to Filter Expression Not Working


I have the following MDX query running on SQL Server 2016:

SELECT NON EMPTY(Measures.[Enrolments Count]) ON COLUMNS,                   
                    NON EMPTY FILTER ( [Term Record Creation].[Year].CHILDREN * [Term Record Creation].[Week].CHILDREN *
                        [Term Enrolments].[Term Year].CHILDREN ,
                        [Term Record Creation].[Week]  <= 5)
                     DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS 
        FROM [Enrolments]

I am trying to only take enrolments where a term record was created before week 5 of the year (where the week is an ISO week).

The Term Year is the year in which a term starts. It is a column in addition to the term record creation dimensions i.e. [Term Record Creation].[Year] and [Term Record Creation].[Week].

The problem I am having is that the filter is doing some sort of filtering (because my measure column is low), but I am still getting all of the weeks of the year with a number against them e.g. I have a row like the following in the result set:

Creation Year Creation Week Term Year Enrolments Count 2012 16 2013 4

I would really appreciate if someone could explain what is going wrong and provide a solution.

Thanks.


Solution

  • Maybe try just filtering the week:

    SELECT 
      NON EMPTY 
        Measures.[Enrolments Count] ON COLUMNS
     ,NON EMPTY 
          [Term Record Creation].[Year].Children*
          Filter
          (
            [Term Record Creation].[Week].Children
           ,
            [Term Record Creation].[Week] <= 5
          )*
          [Term Enrolments].[Term Year].Children ON ROWS
    FROM [Enrolments];
    

    Although I think Daylo will be correct in that you'll need to use the key:

    SELECT 
      NON EMPTY 
        Measures.[Enrolments Count] ON COLUMNS
     ,NON EMPTY 
          [Term Record Creation].[Year].Children*
          Filter
          (
            [Term Record Creation].[Week].Children
           ,
            [Term Record Creation].[Week].CURRENTMEMBER.MEMBER_KEY <= 5
          )*
          [Term Enrolments].[Term Year].Children ON ROWS
    FROM [Enrolments];
    

    In our cube a week's key is like this 201709 so this might be better:

    SELECT 
      NON EMPTY 
        Measures.[Enrolments Count] ON COLUMNS
     ,NON EMPTY 
          [Term Record Creation].[Year].Children*
          Filter
          (
            [Term Record Creation].[Week].Children
           ,
            Cint([Term Record Creation].[Week].CurrentMember.Member_Caption) <= 5
          )*
          [Term Enrolments].[Term Year].Children ON ROWS
    FROM [Enrolments];
    

    Ok - the last approach tests fine against a similar script against AdvWrks:

    SELECT 
      [Measures].[Internet Sales Amount] ON 0
     ,
        [Product].[Product Categories].[Subcategory]
      * 
        Filter
        (
          [Date].[Day of Month].[Day of Month]
         ,
          Cint([Date].[Day of Month].CurrentMember.Member_Caption) > 10
        ) ON 1
    FROM [Adventure Works];
    

    Results:

    enter image description here