

Grasshopper
Group: General Forum Members
Last Login: Friday, April 22, 2016 11:33 AM
Points: 16,
Visits: 167


I'm trying to figure out the proper MDX for a calculated member so that it only does the calculation on members of a specific hierarchy when it is a certain value. I thought I had it, but I am consistently getting the dreaded 1.#INF (divide by zero) even though I thought I trapped it properly in the beginning:
WITH MEMBER [Measures].[MyNewCalculatedMember] as IIF([Measures].[ExistingMeasure]=0, null , SUM( EXISTING ([Dimension].[HierarchyRoot].[HierarchyLeaf].MEMBERS, IIF([Dimension].[HierarchyRoot].CURRENTMEMBER is [Dimension].[HierarchyRoot].[HierarchyLeaf].&[2], [Measures].[OtherMeasure] / [Measures].[ExistingMeasure], null) , IIF([Dimension].[HierarchyRoot].CURRENTMEMBER.PARENT is [Dimension].[HierarchyRoot].[HierarchyLeaf].&[2], [Measures].[OtherMeasure] / [Measures].[ExistingMeasure], null)))* 1000)
I'm pretty sure I'm totally hosing this up as I'm a complete MDX noobie, but this seems to be close. If I filter down to the leaf level, I get what I expect with values or nulls if the denominator is null. When I roll it up to the parent of the hierarchy, I get the dreaded 1.#INF.
Isn't there an easier way to do this with something like Ancestor or something?
Any help would be greatly appreciated!




SSCrazy
Group: General Forum Members
Last Login: Monday, July 28, 2014 7:12 PM
Points: 2,148,
Visits: 487


You'll probably want to use a combination of the SCOPE assignment operator together with the LEAVES function and "THIS" statement instead of using the IF function and testing the coordinate's position.
Calculated members do NOT influence the base value of a parent member, but cell calculations using the SCOPE assignment operator can cascade upwards.
Also, SCOPE statements are usually better performing than IFs. Hope this helps.
EDIT: You must also check for the empty condition using ISEMPTY() to avoid division by zero. Check out this blogmore specifically the PercentOfParent calculation:
http://sqldusty.wordpress.com/2012/02/28/tenmdxcalculationsforyourcubepart22/




Grasshopper
Group: General Forum Members
Last Login: Friday, April 22, 2016 11:33 AM
Points: 16,
Visits: 167


Thanks for the quick response. I'll start looking into those right now and hopefully be able to fix my errors :)




SSCrazy
Group: General Forum Members
Last Login: Monday, July 28, 2014 7:12 PM
Points: 2,148,
Visits: 487


You caught me on a good day




Grasshopper
Group: General Forum Members
Last Login: Friday, April 22, 2016 11:33 AM
Points: 16,
Visits: 167


Yeah, this will take a while...seems like it might be overly cumbersome in terms of code as we are trying to keep our calculated members pretty concise as we have quite a few. Just looking at the blog sample, I'm not entirely sure how I can convert that over to a calculation that is taking two measures, the example is doing a calculation based off of a measure and the dimension?




SSCrazy
Group: General Forum Members
Last Login: Monday, July 28, 2014 7:12 PM
Points: 2,148,
Visits: 487


In business terms, what are you trying to do? What is the rationale driving the calculation?




Grasshopper
Group: General Forum Members
Last Login: Friday, April 22, 2016 11:33 AM
Points: 16,
Visits: 167


We are essentially trying to calculate how much revenue is generated per unit filtering it down to a specific branch of a product. Seems like this should be really simple, but ugh. Not really knowing MDX makes it a little difficult :)
I tried using something along the lines of IIF(IsLeaf([Dimension].[TopHierarchy].CurrentMember),<do calculation>),sum([Dimension].[TopHierarchy].CurrentMember.Children,[Measures].[MyNewMeasure].
That almost works, but as soon as I try using either Filter or Existing, I get #error :/




SSCrazy
Group: General Forum Members
Last Login: Monday, July 28, 2014 7:12 PM
Points: 2,148,
Visits: 487


Without seeing the data model, I am going to say try your original solution, but make sure you test for the member being empty as WELL AS being equal to zero.
Let's see if that works.
I am trying to give you a quick solution without having you rewrite the solution.




Grasshopper
Group: General Forum Members
Last Login: Friday, April 22, 2016 11:33 AM
Points: 16,
Visits: 167


Thanks...I have it almost figured out, but what's odd is putting in my filter for the value is what's throwing it off now. The IsLeaf is working, it's calculating, but with either Filter or Existing in there it's throwing back errors. I'm wondering if it's because I'm just checking if it's = 0....ugh. I'll try that!
 EDIT  This did not resolve the issue. My guess is that it's a fundamental misunderstanding that I have with how this should be working. I don't think what we're looking for is that difficult, but for the life of me I cannot get this to work. We already have calculated measures that work, unfortunately none of them filter on a specific dimension member to only calculate values for that.




SSCrazy
Group: General Forum Members
Last Login: Monday, July 28, 2014 7:12 PM
Points: 2,148,
Visits: 487


Did you get this resolved? I wish I could help you more, but I am really having trouble envisioning the solution. Forgetting the MDX for just a second, what would your pseudocode be?
You may be trying to do too much at once.
EDIT: Also, by just using IIF and NOT using SCOPE, your query performance will, more than likely, suffer. Check out Chris Webb's approach in this blog:
https://cwebbbi.wordpress.com/category/mdx/page/2/
I am hoping this triggers an idea for you.



