单个“何时”具有多个条件的MDX案例声明

时间:2018-12-15 02:28:09

标签: mdx case-statement

我正在尝试尽可能整合以下MDX脚本,并且想知道是否有办法将四个季度(Q1,Q2,Q3,Q4)合并为一行?另外,如果您对如何简化此操作有任何进一步的想法,我非常高兴!

Case [Period].CurrentMember
  When [Q1] Then [Finance Charge and NSF Revenue] / ([CXO_Average Total A/R]+[Average Total A/R - Normalized Load])*4
  When [Q2] Then [Finance Charge and NSF Revenue] / ([CXO_Average Total A/R]+[Average Total A/R - Normalized Load])*4
  When [Q3] Then [Finance Charge and NSF Revenue] / ([CXO_Average Total A/R]+[Average Total A/R - Normalized Load])*4
  When [Q4] Then [Finance Charge and NSF Revenue] / ([CXO_Average Total A/R]+[Average Total A/R - Normalized Load])*4
  When [Total Year] Then [Finance Charge and NSF Revenue] / ([CXO_Average Total A/R]+[Average Total A/R - Normalized Load])        
Else 
  [Finance Charge and NSF Revenue] / ([CXO_Average Total A/R]+[Average Total A/R - Normalized Load])*12
End

2 个答案:

答案 0 :(得分:0)

这应该有效

Case 
  When [Period].CurrentMember =[Q1] or [Period].CurrentMember =[Q2] or [Period].CurrentMember =[Q3] or [Period].CurrentMember =[Q4]
  Then [Finance Charge and NSF Revenue] / ([CXO_Average Total A/R]+[Average Total A/R - Normalized Load])*4
   When [Period].CurrentMember =[Total Year] Then [Finance Charge and NSF Revenue] / ([CXO_Average Total A/R]+[Average Total A/R - Normalized Load])        
Else 
  [Finance Charge and NSF Revenue] / ([CXO_Average Total A/R]+[Average Total A/R - Normalized Load])*12
End

下面是另一个基于冒险作品的示例,可以提供帮助。

with member measures.test as 
case  
when 
[Product].[Subcategory].currentmember.Properties ("Member_Value",TYPED) ="caps" 
or [Product].[Subcategory].currentmember.Properties ("Member_Value",TYPED) ="chains" 
or [Product].[Subcategory].currentmember.Properties ("Member_Value",TYPED) ="gloves" 
then 1 
when [Product].[Subcategory].currentmember.Properties ("Member_Value",TYPED) ="Fenders" 
then 5
else 0 end 
select {[Measures].[Internet Sales Amount],measures.test} on columns ,
[Product].[Subcategory].[Subcategory] on rows 
from 
[Adventure Works]

enter image description here

答案 1 :(得分:0)

IS运算符在这里是您的朋友:我不确定=运算符是否还能工作并且使用....Properties ("Member_Value",TYPED) ="caps"的速度很慢。

我也尝试使用IIF中的MDX,它通常比CASE

IIF(
      ( [Period].CURRENTMEMBER IS [Period].[Period].[Q1] ) 
   OR ( [Period].CURRENTMEMBER IS [Period].[Period].[Q1] ) 
   OR ( [Period].CURRENTMEMBER IS [Period].[Period].[Q1] ) 
   OR ( [Period].CURRENTMEMBER IS [Period].[Period].[Q1] ) 
   , DIVIDE(
         [Finance Charge and NSF Revenue]
        ,( [CXO_Average Total A/R]+[Average Total A/R - Normalized Load] )*4
     )
   , IIF(
        ( [Period].CURRENTMEMBER IS [Period].[Period].[Total Year] )
        , DIVIDE(
             [Finance Charge and NSF Revenue]
            ,( [CXO_Average Total A/R]+[Average Total A/R - Normalized Load] )
          )
        , DIVIDE(
             [Finance Charge and NSF Revenue]
            ,( [CXO_Average Total A/R]+[Average Total A/R - Normalized Load] )*12
          )
)

编辑

在使用IIF / IS进行更复杂的查询时,速度会更快-这相当于MoazRubs AdvWrks脚本:

WITH 
MEMBER measures.test as 
IIF(   
  [Product].[Subcategory].currentmember IS  [Product].[Subcategory].[Caps]
  OR [Product].[Subcategory].currentmember IS  [Product].[Subcategory].[Chains] 
    OR [Product].[Subcategory].currentmember IS  [Product].[Subcategory].[Gloves] 
  , 1
    , IIF(
       [Product].[Subcategory].currentmember IS  [Product].[Subcategory].[Fenders] 
     ,5
         ,NULL
      )
) 
SELECT 
   {
        [Measures].[Internet Sales Amount],
            measures.test
        } ON 0,
[Product].[Subcategory].[Subcategory] ON 1 
FROM 
[Adventure Works]