Filters in MDX Queries

Introduction

This article explains filter conditions in MDX queries. There are so many conditions in MDX since we have SQL. I am explaining the most commonly used filter conditions in MDX. I hope you will like it.

Please see this article in my blog: Filters in MDX Query.

Background

For the past few days I have been working on the MDX queries. Since my application's data source was an ADOMD data source, it was a must to learn about MDX queries. If you are new to ADOMD, you can find out some tips here: ADOMD Tips.

MDX

Before beginning, we will see what MDX is. The following describes it.

  • MDX stands for Multidimensional Expression.
  • It is a query language for OLAP databases like SQL for relational databases.
  • It is also a calculation language.
  • Its syntax is similar to spreadsheet formulas.

If you are completely new to MDX, you can read the basics here.

Expressions and Equivalent in MDX

Now we will use this conditions in the MDX queries. I hope you are aware of MDX query basics now.

MDX Filter Expression Examples

To check whether a dimension value is empty

To check whether a dimension value is empty or not, you need to add a filter condition as follows. 

  1. {FILTER([My Dimension Group].[Dimension Name].[Dimension Name], Trim([My Dimension Group].[Dimension Name].CurrentMember.Name) = '')}  

To check whether a dimension value is not empty

To check whether a dimension value is not empty or not, you need to add a filter condition as follows. 
  1. {FILTER([My Dimension Group].[Dimension Name].[Dimension Name], Trim([My Dimension Group].[Dimension Name].CurrentMember.Name) <> '')}  

To check whether a dimension value contains a specific value

To check whether a dimension value contains a specific value, you need to add a filter condition as follows. 

  1. {FILTER([My Dimension Group].[Dimension Name].[Dimension Name], Instr([My Dimension Group].[Dimension Name].CurrentMember.Name, 'My String Value') > 0)}  
To check whether a dimension value does not contains a specific value

To check whether a dimension value does not contain a specific value, you need to add a filter condition as follows. 

  1. -{FILTER([My Dimension Group].[Dimension Name].[Dimension Name], Instr([My Dimension Group].[Dimension Name].CurrentMember.Name, 'My String Value') > 0)}  

To check whether a dimension value starts with a specific value

To check whether a dimension value starts with a specific value, you need to add a filter condition as follows. 
  1. {FILTER([My Dimension Group].[Dimension Name].[Dimension Name], Left([My Dimension Group].[Dimension Name].CurrentMember.Name, 5) = 'My String Value')}  
To check whether a dimension value ends with a specific value

To check whether a dimension value ends with a specific value, you need to add a filter condition as follows. 

  1. {FILTER([My Dimension Group].[Dimension Name].[Dimension Name], Right([My Dimension Group].[Dimension Name].CurrentMember.Name, 5) = 'My String Value')}  

To check whether a dimension value equal a specific value 

To check whether a dimension value is equal to a specific value, you need to add a filter condition as follows. 
  1. {FILTER([My Dimension Group].[Dimension Name].[Dimension Name], [My Dimension Group].[Dimension Name].CurrentMember.Name = 'My String Value Carolina')}  

To check whether a dimension value is null

To check whether a dimension value is null, you need to add a filter condition as follows.
  1. {FILTER([My Dimension Group].[Dimension Name].[Dimension Name], [Measures].[Mesure Name] = NULL)}  

To check whether a dimension value is not null

To check whether a dimension value is not null, you need to add a filter condition as follows.

  1. NON EMPTY([My Dimension Group].[Dimension Name].[Dimension Name])  

Examples

The following are the examples of queries that use the preceding expressions.

Query 1 

  1. SELECT {[Measures].[Mesure Name]} ON COLUMNS,  
  2. ({FILTER([My Dimension Group].[Dimension Name].[Dimension Name], Trim([My Dimension Group].[Dimension Name].CurrentMember.Name) = '')}) ON ROWS  
  3. FROM [My Cube Name]  

Query 2 

  1. SELECT {[Measures].[Mesure Name]} ON COLUMNS,  
  2. ({FILTER([My Dimension Group].[Dimension Name].[Dimension Name], Instr([My Dimension Group].[Dimension Name].CurrentMember.Name, 'My String Value') > 0)}) ON ROWS  
  3. FROM [My Cube Name]   
Query 3 
  1. SELECT {[Measures].[Mesure Name]} ON COLUMNS,  
  2. ({FILTER([My Dimension Group].[Dimension Name].[Dimension Name], Left([My Dimension Group].[Dimension Name].CurrentMember.Name, 5) = 'My String Value')}) ON ROWS  
  3. FROM [My Cube Name]     
Query 4 
  1. SELECT {[Measures].[Mesure Name]} ON COLUMNS,  
  2. ({FILTER([My Dimension Group].[Dimension Name].[Dimension Name], Right([My Dimension Group].[Dimension Name].CurrentMember.Name, 5) = 'My String Value')}) ON ROWS  
  3. FROM [My Cube Name]  
Query 5 
  1. SELECT {[Measures].[Mesure Name]} ON COLUMNS,  
  2. ({FILTER([My Dimension Group].[Dimension Name].[Dimension Name], [My Dimension Group].[Dimension Name].CurrentMember.Name = 'My String Value Carolina')}) ON ROWS  
  3. FROM [My Cube Name]   
Query 6 
  1. SELECT {[Measures].[Mesure Name]} ON COLUMNS,  
  2. ({FILTER([My Dimension Group].[Dimension Name].[Dimension Name], [Measures].[Mesure Name] = NULL)}) ON ROWS  
  3. FROM [My Cube Name] 
Query 7
  1. SELECT {[Measures].[Mesure Name]} ON COLUMNS,  
  2. NON EMPTY([My Dimension Group].[Dimension Name].[Dimension Name]) ON ROWS  
  3. FROM [My Cube Name]   

That is for now.

Conclusion

I hope someone found this article useful. Please share with me your valuable thoughts and comments. Your feedback is always welcomed.

Thanks in advance. Happy coding!
Kindest Regards

Sibeesh Venu


Up Next
    Ebook Download
    View all
    Learn
    View all