Today I’m going to blog about a problem we recently solved in a client’s cube, an error in the Mdx script that’s very easy to make if you aren’t careful.
We’ll run a simple example in AdventureWorks (what else?) to demonstrate the issue.
The client had already added a calculation to their cube to show year-on-year growth. The formula is:
Create Member CurrentCube.[Measures].[Delta to PrevYear] as ( ([Measures].[Internet Sales Amount]) - ([Measures].[Internet Sales Amount], ParallelPeriod( [Date].[Calendar].[Calendar Year], 1, [Date].[Calendar].CurrentMember ) ) ) / ([Measures].[Internet Sales Amount], ParallelPeriod( [Date].[Calendar].[Calendar Year], 1, [Date].[Calendar].CurrentMember ) ) , Format_String = "0.00%";
(some error checking removed for clarity)
This screenshot shows a couple of simple XLCubed Grids showing the real value, and below the percentage change. I have added in an Excel calculation to show the results are as expected.
Later during the cube development, the client added a calculated member in their Product dimension, one that gives a total excluding one of the product categories.
To replicate this I’ll add a calculation for “All Ex Bikes”:
Create Member CurrentCube.[Product].[Product Model Categories].[All Products].[All Ex Bikes] as ( ([Product].[Product Model Categories].[All Products]) - ([Product].[Product Model Categories].[Category].&) );
And if we run the report again we get the following.
Notice the cell I’ve highlighted. The “All Ex Bikes” calculation works fine on the normal measure, but it gives totally the wrong number for the percentage calculation. What’s going on?
The problem is that in the cell highlighted Analysis Services has two calculations to think about when working out the result.
- Compare this year to last year
- Get the “Grand Total”, and subtract “Bikes”
As the number returned is 1.85% we can see that Analysis Services has chosen the second option, “Grand Total” – “Bikes”.
What we really want is for the calculation to be done by getting the subtotal, and then doing the percentage change based on that.
Fortunately the fix was a simple one. Analysis Services will run the calculations in the order they are found in the Mdx Script, so to fix the issue we simply moved the new “All Ex Bikes” definition up above the percentage calculation.
Pass/Solve Order can be a complex topic, so you may need to be careful.
In this case the number is totally wrong, so it was easy to spot, but some bugs will be much more subtle, so watch out!