Case statement not working for all conditions

404 Views Asked by At
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.

1

There are 1 best solutions below

10
On

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