SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How do you filter a calculation by an existing hierarchy?


How do you filter a calculation by an existing hierarchy?

Author
Message
jason.freier
jason.freier
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 168
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!
sneumersky
sneumersky
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3208 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 blog--more specifically the PercentOfParent calculation:

http://sqldusty.wordpress.com/2012/02/28/ten-mdx-calculations-for-your-cube-part-2-2/
jason.freier
jason.freier
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 168
Thanks for the quick response. I'll start looking into those right now and hopefully be able to fix my errors Smile
sneumersky
sneumersky
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3208 Visits: 487
You caught me on a good day ;-)
jason.freier
jason.freier
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 168
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?
sneumersky
sneumersky
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3208 Visits: 487
In business terms, what are you trying to do? What is the rationale driving the calculation?
jason.freier
jason.freier
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 168
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 Smile

I tried using something along the lines of IIF(IsLeaf([Dimension].[TopHierarchy].CurrentMember),<do calculation>Wink,sum([Dimension].[TopHierarchy].CurrentMember.Children,[Measures].[MyNewMeasure].

That almost works, but as soon as I try using either Filter or Existing, I get #error :/
sneumersky
sneumersky
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3208 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.
jason.freier
jason.freier
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 168
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.
sneumersky
sneumersky
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3208 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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search