Filters in MDX Queries

Introduction

This article explains the 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 these conditions in the MDX queries. I hope you are aware of the 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.

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 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 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.

{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 contain a specific value

To check whether a dimension value does not contain a specific 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 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.

{
  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.

{
  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 equals a specific value

To check whether a dimension value is equal to a specific 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 a 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].[Measure 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.

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

Examples

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

Query 1

SELECT
  { [Measures].[Measure 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].[Measure 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].[Measure 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].[Measure 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].[Measure 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].[Measure Name] } ON COLUMNS,
  {
    FILTER(
      [My Dimension Group].[Dimension Name].[Dimension Name],
      [Measures].[Measure Name] = NULL
    )
  } ON ROWS
FROM
  [My Cube Name]

Query 7

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

That is for now.

Conclusion

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

Thanks in advance. Happy coding!

Kindest Regards

Sibeesh Venu


Similar Articles