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
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:
{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:
{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:
{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:
-{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:
{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:
{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:
{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:
{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:
NON EMPTY([My Dimension Group].[Dimension Name].[Dimension Name])
Following are the examples of queries which use the above mentioned expressions.
Query 1
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
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
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
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
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
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
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!