简体   繁体   中英

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 ie [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 eg 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.

It won't work:

[Term Record Creation].[Week]  <= 5

You compare Week hierarchy with number. It doesn't make sense. Try something like the following:

Select
[Measures].[Enrolments Count] on columns,                   
Non Empty 
    Filter(
        [Term Record Creation].[Week].[Week].Members,
        Right(
            [Term Record Creation].[Week].CurrentMember.Member_Caption,
            2
        )  <= "05"
    ) *
    [Term Record Creation].[Year].[Year].Members *    
    [Term Enrolments].[Term Year].Children on rows  
From [Enrolments]

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:

在此处输入图片说明

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