MDX - rewrite a query using scope

2019-08-17 05:08发布

问题:

I have an MDX query

IIF
(
  IsLeaf([PnL].[PnL_A].CurrentMember)
 ,
  [Measures].[PnL - Plan] * [PnL].[Flag 5].CurrentMember.MemberValue
 ,Sum
  (
    [PnL].[PnL_A].CurrentMember.Children
   ,[Measures].[PnL- Plan (signed)]
  )
)

What it does: The whole thing represents profit and loss. Unfortunately, it is constructed in a way that there are two columns: value of a profit or loss, and flag in the other column.

So if the flag ([PnL].[Flag 5]) is set to -1, the value ([Measures].[PnL - Plan]) is a loss, if the flag is a 1 - the value is a profit. I can't change that.

The query finds leaves of the hierarchy (single deepest source of a profit or loss) and multiplies the flag with the value. For non-leaf members it just aggregates it's leaves.

My problem is that it works too slow - I wanted to rewrite this query using SCOPE but I have no idea how.

回答1:

Since I have absolutely no idea on your cube structure, let's say your member structure is

Pnl-->Pnl_A-->NonLeaf-->Leaf

You could define scope as below -

CREATE MEMBER CurrentCube.[Measures].[ProfitOrLossValue] AS NULL;

//Current member is leaf and Flag5 is 1
SCOPE
     (
      [Measures].[ProfitOrLossValue], 
      [PnL].[Flag 5].&[1], 
      [PnL].[PnL_A].[Leaf].[All].CHILDREN
     );

This = [Measures].[PnL - Plan];
END SCOPE;

SCOPE
     (
      [Measures].[ProfitOrLossValue], 
      [PnL].[Flag 5].&[-1], 
      [PnL].[PnL_A].[Leaf].[All].CHILDREN
     );

This = ([Measures].[PnL - Plan] * -1);
END SCOPE;

//Current member is non-leaf
SCOPE
     (
      [Measures].[ProfitOrLossValue], 
      [PnL].[PnL_A].[NonLeaf].[All].CHILDREN
     );

This = Sum
          (
           [PnL].[PnL_A].CurrentMember.Children, 
           [Measures].[PnL- Plan (signed)]
          );
END SCOPE;

Hope it helps.