On occasion you may find that your MDX queries don’t quite give you the result that you expect, which sometimes may be due to the way in which a certain bit of MDX has changed the context of part of your query.
This can sometimes be tough to figure out, especially with functions like Sum() or Filter(), whereby we are looping over a set and then only returning the end product. For example, take the following query, where I’m trying to find out the count of UK male customers who have bought Road Bikes in both the current and the previous year:
WITH MEMBER [Measures].[Repeat Customer Count] AS SUM( [Customer].[Customer].[Customer].Members, IIF([Measures].[Internet Sales Amount] <> 0 AND ([Measures].[Internet Sales Amount], [Date].[Calendar Year].PrevMember) <> 0, 1, NULL) )
SELECT {[Measures].[Repeat Customer Count]} ON 0, [Date].[Calendar Year].[Calendar Year] ON 1 FROM [Adventure Works] --Road Bikes, UK, Male WHERE ([Product].[Subcategory].&[2], [Sales Territory].[Sales Territory Country].&[United Kingdom], [Customer].[Gender].&[M])
It returns the following result set, which I know to be incorrect:
Unlike debugging either of the axes, where of course we can see what gets returned, debugging examples such as the above might be difficult. The MDX Generate function can help here, as it works in a similar way to Sum(), by iterating over a set and then evaluating an argument once per iteration. The idea is that Generate will build us a new set, by using the looping context of the first set that you pass it. In this case, however, we can use it to return us a concatenated string based on our input set.
What I’ve done here is to use the CurrentMember of attributes that I think might be causing problems (Customer, Gender) and then just print them out as a string in order to see if the attributes have the context that I expect. Therefore I’ve replaced the ‘1’ in my original query with the string of:
" | " + [Customer].[Customer].CurrentMember.MEMBER_CAPTION + ", " + [Customer].[Gender].CurrentMember.MEMBER_CAPTION
The full query and results then become:
GENERATE( [Customer].[Customer].[Customer].Members, IIF([Measures].[Internet Sales Amount] <> 0 AND ([Measures].[Internet Sales Amount], [Date].[Calendar Year].PrevMember) <> 0, " | " + [Customer].[Customer].CurrentMember.MEMBER_CAPTION + ", " + [Customer].[Gender].CurrentMember.MEMBER_CAPTION, NULL) ) SELECT {[Measures].[Repeat Customer Count]} ON 0, [Date].[Calendar Year].[Calendar Year] ON 1 FROM [Adventure Works] --Road Bikes, UK, Male WHERE ([Product].[Subcategory].&[2], [Sales Territory].[Sales Territory Country].&[United Kingdom], [Customer].[Gender].&[M])
I can spot straight away that female customers have been returned in 2007, which is due to the fact that sets specified inside the WITH MEMBER section are not evaluated in the context of the slicer. In this case the problem can be solved by using the EXISTING keyword inside the Sum, so that the customers get sliced by the gender attribute:
SUM( EXISTING [Customer].[Customer].[Customer].Members, IIF([Measures].[Internet Sales Amount] <> 0 AND ([Measures].[Internet Sales Amount], [Date].[Calendar Year].PrevMember) <> 0, 1, NULL) )
This won’t be applicable in all MDX debugging scenarios, but hopefully it will help someone out.
Celebrating International Women’s Day: from Classroom to Code
As we celebrate International Women’s Day, I want to share my journey of breaking stereotypes
Mar
Pretty Power BI – Adding Pagination to Bar Charts
Good User Experience (UX) design is crucial in enabling stakeholders to maximise the insights that
Feb
Pretty Power BI – Creating Dynamic Histograms
Good User Experience (UX) design is crucial in enabling stakeholders to maximise the insights that
Feb
Top Tips to Pass the Databricks Certified Data Engineer Professional Exam
Having recently passed the Databricks Certified Data Engineer Professional exam, this blog post covers some
Jan
Python vs. PySpark Navigating Data Analytics in Databricks – Part 1
Introduction When it comes to conquering the data analytics landscape in Databricks, two heavyweights, Python
Jan
Impact of AI on Business Analysis
Artificial intelligence (AI) is rapidly transforming our world, and this blog post concentrates on the
Jan
Creating Clickbait Using Python
In 2023, about 5 billion people used the internet. With so many people contributing and
Dec
A Brief Overview of Security in Microsoft Fabric
Where Fabric Sits in the Hierarchy As you are probably aware, Microsoft Fabric is Microsoft’s
Dec