Click here to Skip to main content
15,884,388 members
Articles / All Topics

Filters in MDX Queries

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
6 Jul 2015CPOL2 min read 34.7K   3   1
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!

This article was originally posted at http://sibeeshpassion.com/filters-in-mdx-queries

License

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


Written By
Software Developer
Germany Germany
I am Sibeesh Venu, an engineer by profession and writer by passion. I’m neither an expert nor a guru. I have been awarded Microsoft MVP 3 times, C# Corner MVP 5 times, DZone MVB. I always love to learn new technologies, and I strongly believe that the one who stops learning is old.

My Blog: Sibeesh Passion
My Website: Sibeesh Venu

Comments and Discussions

 
QuestionNon empty is not working for me. Pin
Member 141071073-Jan-19 4:08
Member 141071073-Jan-19 4:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.