1
with member test as
case 
  when ([All Products].[All Products].[Group 2].&[1],[Measures].[minus-prod-trx])>0
  then (ancestor([All Products].[All Products].[Group 2].&[1],2),[Measures].[minus-prod-trx])
   when ([All Products].[All Products].[Group 2].&[2],[Measures].[minus-prod-trx])>0
  then (ancestor([All Products].[All Products].[Group 2].&[2],2),[Measures].[minus-prod-trx])
end

Problem: How can I get both the values for if above both conditions satisfies? Now only one result returning though both condition satisfies. I tried with:

case 
  when [All Products].[Group 2].CURRENTMEMBER IS [All Products].[Group 2].&[1]
  then...

But it's not working.

halfer
  • 19,824
  • 17
  • 99
  • 186
Anil
  • 39
  • 2
  • A case statement will return the first expression that evaluates to true. That is simply [how case statements work](https://msdn.microsoft.com/en-us/library/ms144841.aspx). Depending on your hierarchies, I'm not sure you even need a case statement. Could you not do something with currentmember? – mmarie Nov 18 '15 at 18:38
  • I'd say your second attempt looks better. In the first attempt the initial `when` is probably always true. For the second snippet you say `But it's not working.` - can you expand on this - what is happening that means it is not working? – whytheq Nov 18 '15 at 21:20
  • I tried like below but showing "null" as result... with member [Measures].test as case when [All Products].[All Products].currentmember IS [All Products].[All Products].[Group 2].&[1] then (ancestor([All Products].[All Products].[Group 2].&[1],2),[Measures].[TRX]) end select [Measures].test on 0 from [JOI POC]; – Anil Nov 19 '15 at 08:08
  • how I am getting null values for [All Products].[All Products].CURRENTMEMBER.. ..:: with member [Measures].[Test] as [All Products].[All Products].CURRENTMEMBER select [Measures].[Test] on 0,[All Products].[Group 2].members on 1 from [cube]; – Anil Nov 19 '15 at 09:01
  • Did my answer help you find a solution to your problem? – whytheq Jan 02 '16 at 12:50

1 Answers1

0

Assuming this hierarchy is ON ROWS:

[All Products].[All Products]

Then something like the following:

WITH MEMBER [Measures].test AS
  CASE
    WHEN 
     [All Products].[All Products].CURRENTMEMBER 
         IS [All Products].[All Products].[Group 2].&[1]
       AND [Measures].[minus-prod-trx] > 0
         THEN
          (ancestor([All Products].[All Products].[Group 2].&[1],2),[Measures].[minus-prod-trx])
   WHEN
     ....

Here is an example of the above in use against MS's AdvWrks cube:

WITH 
  MEMBER [Measures].test AS 
    CASE 
      WHEN 
            [Product].[Product Categories].CurrentMember
          IS 
            [Product].[Product Categories].[Product].[Hitch Rack - 4-Bike]
        AND 
          [Measures].[Internet Sales Amount] > 0 
      THEN 
        (
          Ancestor
          (
            [Product].[Product Categories].[Product].[Hitch Rack - 4-Bike]
           ,2
          )
         ,[Measures].[Internet Sales Amount]
        )
      WHEN 
            [Product].[Product Categories].CurrentMember
          IS 
            [Product].[Product Categories].[Product].[Road Bottle Cage]
        AND 
          [Measures].[Internet Sales Amount] > 0 
      THEN 
        (
          Ancestor
          (
            [Product].[Product Categories].[Product].[Road Bottle Cage]
           ,3
          )
         ,[Measures].[Internet Sales Amount]
        )
    END 
SELECT 
  NON EMPTY 
    {
      [Measures].[Internet Sales Amount]
     ,[Measures].test
    } ON 0
 ,[Product].[Product Categories].[Product] ON 1
FROM [Adventure Works];

Results in the following:

enter image description here

In comments you put the following

with member [Measures].Test as 
case 
when [Dim Date].[Hierarchy].currentmember 
  IS [Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2] 
    then (ancestor([Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2],2),[Measures].[Amount]) end 
select {[Measures].Test} on 0 
from [My Adventure Works]; 

This code looks ok to me. [Dim Date].[Hierarchy].currentmember is not equal to the member you have specified in the above - currentmember looks at each row of the output and returns the current member but in the above you do not have this hierarchy on your rows ... so the currentmember is the All member so your WHEN condition is false.

Sourav has fixed the above for you by creating the context so that currentmember returns a member other than the All member of the hierarchy:

with member [Measures].Test as 
case 
when [Dim Date].[Hierarchy].currentmember 
  IS [Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2] 
    then (ancestor([Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2],2),[Measures].[Amount]) end 
select 
   {[Measures].Test} on 0, 
    [Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2] on 1 //<<including this means that the currentmember has something to work with!!
from [My Adventure Works]; 

Please have a read of the definition of the currentmember function: https://msdn.microsoft.com/en-us/library/ms144948.aspx

whytheq
  • 34,466
  • 65
  • 172
  • 267
  • @whytheq - There was an unneeded comma in your code. I edited and removed it from the code. – SouravA Nov 19 '15 at 09:52
  • @SouravA I'm currently replicating the behaviour in an AdvWrks script to try to understand things better - I will post shortly – whytheq Nov 19 '15 at 10:29
  • case1 fine where as case2 not working, returning null in output case1# select ancestor([Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2],2) on 0, [Measures].[Amount] on 1 from [My Adventure Works]; case2# with member [Measures].Test as case when [Dim Date].[Hierarchy].currentmember IS [Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2] then (ancestor([Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2],2),[Measures].[Amount]) end select {[Measures].Test} on 0 from [My Adventure Works]; – Anil Nov 19 '15 at 13:35
  • @Sourav, whytheq, Please help me, I am not sure what I am missing...please check above two cases... – Anil Nov 19 '15 at 13:36
  • 1
    In `case 2`, have `with member [Measures].Test as case when [Dim Date].[Hierarchy].currentmember IS [Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2] then (ancestor([Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2],2),[Measures].[Amount]) end select {[Measures].Test} on 0, [Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2] on 1 from [My Adventure Works];` – SouravA Nov 19 '15 at 15:32
  • @SouravA thank you - I have added the above to the answer so it is a little more readable. – whytheq Nov 19 '15 at 15:45
  • @SouravA ..Thank you for all your help..it's resolved... :) – Anil Nov 19 '15 at 17:28
  • @whytheq ..Thank you for all your help..it's resolved... :) – Anil Nov 19 '15 at 17:29
  • @Anil glad it is resolved (sometimes mdx is quite mysterious!) – whytheq Nov 19 '15 at 19:00
  • @Anil it is ok to mark questions as correct - or up answers if you think someone has tried to help you: don't be scared! – whytheq Dec 02 '15 at 12:51