Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Filters in MDX Queries

5.00/5 (2 votes)
6 Jul 2015CPOL2 min read 35.5K  
Filters in MDX Queries

Introduction

Today, we will learn about filter conditions in MDX queries. There are so many conditions in MDX as we have SQL. I am going to discuss most used filter conditions in MDX. I hope you will like it.

Background

For the past few days, I am working on the MDX queries. Since my applications' data source were 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:

What is MDX?

Before we start, we will see what MDX is?

  • 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

Image 1

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

MDX Filter Expression Examples

To Check whether dimension value is empty

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

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

To Check whether dimension value is Not Empty

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

SQL
{FILTER([My Dimension Group].[Dimension Name].[Dimension Name], _
Trim([My Dimension Group].[Dimension Name].CurrentMember.Name) <> '')}

To Check whether dimension value Contains a particular value

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

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

To Check whether dimension value Does Not Contain a particular value

To check whether a dimension value Does Not Contain a particular value, you need to add a filter condition as follows:

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

To Check whether dimension value Starts With a particular value

To check whether a dimension value Starts With a particular value, you need to add a filter condition as follows:

SQL
{FILTER([My Dimension Group].[Dimension Name].[Dimension Name], _
Left([My Dimension Group].[Dimension Name].CurrentMember.Name, 5) = 'My String Value')}

To Check whether dimension value Ends With a particular value

To check whether a dimension value Ends With a particular value, you need to add a filter condition as follows:

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

To Check whether dimension value Equal a particular value

To check whether a dimension value Equals a particular value, you need to add a filter condition as follows:

SQL
{FILTER([My Dimension Group].[Dimension Name].[Dimension Name], _
[My Dimension Group].[Dimension Name].CurrentMember.Name = 'My String Value Carolina')}

To Check whether dimension value is NULL

To check whether a dimension value is NULL, you need to add a filter condition as follows:

SQL
{FILTER([My Dimension Group].[Dimension Name].[Dimension Name], [Measures].[Mesure Name] = NULL)}

To Check whether dimension value is NOT NULL

To check whether a dimension value is NOT NULL, you need to add a filter condition as follows:

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

Following are the examples of queries which use the above mentioned expressions.

Query 1

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

Query 2

SQL
SELECT {[Measures].[Mesure Name]} ON COLUMNS,
({FILTER([My Dimension Group].[Dimension Name].[Dimension Name], _
Instr([My Dimension Group].[Dimension Name].CurrentMember.Name, 'My String Value') > 0)}) ON ROWS
FROM [My Cube Name]

Query 3

SQL
SELECT {[Measures].[Mesure Name]} ON COLUMNS,
({FILTER([My Dimension Group].[Dimension Name].[Dimension Name], _
Left([My Dimension Group].[Dimension Name].CurrentMember.Name, 5) = 'My String Value')}) ON ROWS
FROM [My Cube Name]

Query 4

SQL
SELECT {[Measures].[Mesure Name]} ON COLUMNS,
({FILTER([My Dimension Group].[Dimension Name].[Dimension Name], _
Right([My Dimension Group].[Dimension Name].CurrentMember.Name, 5) = 'My String Value')}) ON ROWS
FROM [My Cube Name]

Query 5

SQL
SELECT {[Measures].[Mesure Name]} ON COLUMNS,
({FILTER([My Dimension Group].[Dimension Name].[Dimension Name], _
[My Dimension Group].[Dimension Name].CurrentMember.Name = 'My String Value Carolina')}) ON ROWS
FROM [My Cube Name]

Query 6

SQL
SELECT {[Measures].[Mesure Name]} ON COLUMNS,
({FILTER([My Dimension Group].[Dimension Name].[Dimension Name], _
[Measures].[Mesure Name] = NULL)}) ON ROWS
FROM [My Cube Name]

Query 7

SQL
SELECT {[Measures].[Mesure Name]} ON COLUMNS,
NON EMPTY([My Dimension Group].[Dimension Name].[Dimension Name]) ON ROWS
FROM [My Cube Name]

That is it for now. :)

Conclusion

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

Thanks in advance. Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)