| Counting Consistent Customers in MDX |
| Written by Chris Webb | |||
| Sunday, 24 February 2013 19:44 | |||
|
Reposted from Chris Webb's blog with the author's permission. The post I wrote a few years ago on counting new and returning customers in MDX has proved to be one of the most popular here; it's certainly a topic I've returned to a number of times for DAX, and other people (most recently Gerhard Brueckl) seem equally interested in solving this problem. However I had a comment from Sohrab Niramwalla yesterday which raised an interesting question: how do you could the number of customers who have bought from you in every time period from the beginning of time until the current date? The Customer dimension in Adventure Works isn't actually very good for illustrating this because customers very rarely buy more than once, but if you look at Countries then you can recreate the scenario. Consider the following query: select From the screenshot you can see that on July 1st 2001 there were purchases in 4 out of 6 countries; of those 4 countries, only 2 (Australia and USA) had purchases on July 2nd; both of those had purchases on July 3rd; but by July 4th only USA had seen purchases on every day since the beginning of time. How then is is possible to get this count of the number of countries that have seen purchases since the beginning of time? I can think of a few approaches. One would involve using recursion and strtoset/settostr, but I generally like to avoid recursion and strtoset because performance can be unpredictable and bad. Probably the best way is to think of the problem like this: if a country has had sales on every day since the beginning of time, then count of days that had sales since the beginning of time will be the same as the total count of days since the beginning of time. Therefore, you can write a query something like this: with –count the number of non empty days for sales since the beginning of time –count the number of countries that have nonemptydaycount = daycount Mosha's technique for optimising count(filter()) calculations might also be useful here, although I have to admit I've not tested it to see if it does improve performance. However, for those of you who are fans of obscure MDX, here's another solution: with select {measures.[Consistent Customers]} on 0,
It uses the generate() function to loop over ever date from the beginning of time to the current time period, and then redefines an inline named set (called customerset) on every step of the iteration to find the non empty countries. It doesn't perform as well as the previous solution in this particular case, but if there were more countries that might change; a few tweaks to the code might also speed it up. I thought it was worth mentioning, though, for the novelty value.
|
Latest Author Articles
- Using Scoped Assignments To Show Calendar and Financial Year-To-Date Values In The Same Calculated Measure
- Comments And Descriptions In DAX
- UseRelationship() and Tabular Row Security
- SSAS on Windows Azure Virtual Machines
- UK/US Date Format Bug in PowerPivot and SSAS Tabular








