使用MDX查询检查case语句中的多个条件

时间:2015-06-10 13:33:37

标签: ssas mdx

如何在mdx中编写以下case语句。我在事实表中移动了下面提到的列。

   sum(case when IsSummary = 1 and IsAudited  = 1 and FinalAuditFlag = 1 then 1 
else 0 end) AuditsCompleted,--Count - Completed Audits

我尝试了下面的MDX查询。

WITH 

    MEMBER [Measures].[count]
    AS
    (
     case  ([Measures].currentmember )
            when 0 then ([Measures].[Is Summary] )
                    else 1 
            end 
        )
    Select 
            {[Measures].[count]}    on 0,
            [Dim_table1].[TableId].members  on 1
    from    [Dsv]   

2 个答案:

答案 0 :(得分:1)

你所做的几乎是正确的。只需将.currentmember更改为多维数据集中的实际度量即可。目前,当您拥有以下内容时,它指的是自己,即currentmember的黑色箭头,它指的是黑色箭头所示的度量count ...

enter image description here

这是AdvWrks

SELECT 
  {[Measures].[Internet Sales Amount]} ON 0
 ,{[Product].[Product Categories].[Category]} ON 1
FROM [Adventure Works];

它返回:

enter image description here

如果我想替换组件的空单元格,我们可以使用CASE

WITH 
  MEMBER [Measures].[count] AS  
     CASE  [Measures].[Internet Sales Amount]
        WHEN 0 THEN "XXX"             
          ELSE [Measures].[Internet Sales Amount] 
      END  ,format_string = "#,###,##0"
SELECT 
  {
   [Measures].[count]
  } ON 0
 ,{[Product].[Product Categories].[Category]} ON 1
FROM [Adventure Works];

现在返回:

enter image description here

IIF中使用的{p> MDXCASE使用的更多 - IIF几乎总是更快。因此,使用IIF的上述等价物如下:

WITH 
  MEMBER [Measures].[count] AS 
    IIF
    (
      [Measures].[Internet Sales Amount] = 0
     ,"XXX"
     ,[Measures].[Internet Sales Amount]
    ) 
   ,format_string = "#,###,##0" 
SELECT 
  {[Measures].[count]} ON 0
 ,{[Product].[Product Categories].[Category]} ON 1
FROM [Adventure Works];

答案 1 :(得分:0)

我假设您的要求是找到[Dim Table1].[TableID]层次结构的所有成员的总和。

如果那么,那么下面的内容对您有用: -

WITH MEMBER Measures.[Count] AS
SUM(
    [Dim_table1].[TableId].CHILDREN, 
    [Measures].[Is Summary] * [Measures].[Is Audited] * [Measures].[Final Audited Flag] //If any is 0, it's 0, else 1
   )

SELECT Measures.[Count]
FROM [Dsv] 

如果需要添加更多切片器或者我的理解是不正确的,请告诉我。