MDX基于维度值计算度量

时间:2014-08-26 06:44:55

标签: mdx measure

我是mdx和SSAS的新手,我们正在尝试将我们的商店程序转换为mdx查询。我有一个像下面这样的立方体:

事实表:

         [Target] (FK),
         [Date] (FK),
         [Action] (FK),
         [Amount] (Measure),

昏暗日期:

        [DateKey] (pk),
        [Date],
        [DayOfYear],
        [DayofWeek],
        [....]

Dim Action:有四种动作(添加,删除,连接,......)

        [ID] (PK)
        [Name] 

昏暗的目标:

        [ID] (PK)
        [Name] 

我想创建一个计算的度量,就像我们在t-sql中所做的那样

 SELECT ....,


   CASE 
         WHEN a.[Action]='added' 

         THEN DATEDIFF(DAY, a.[Date],GETUTCDATE()) 
         ELSE DATEDIFF(DAY, a.[Date],GETUTCDATE())*-1
         END


 AS 'Lifespan'

 FROM FilterData a 
 GROUP BY a.[Target]

问题:如何创建一个计算寿命的计算方法?

我试过但是失败了,[昏暗的行动类型]。[姓名] .CURRENTMEMBER总是“全部”;

With Member [Measures].[LifeSpan] as
CASE  
WHEN  [Dim  Action Types].[Name].CURRENTMEMBER IS [Dim  Action Types].[Name].&[added] THEN  1*'datediff("d",[Dim Date].[Date],Now()) '
WHEN [Dim  Action Types].[Name].CURRENTMEMBER IS  [Dim  Action Types].[Name].&[removed] THEN -1*'datediff("d",[Dim Date].[Date],Now()) '
ELSE  0
End 
select [Measures].[LifeSpan] on columns,
[Dim Action Targets].[Name].Children on rows
from [OLAP Prep]

我可以获得一些数据使用查询:


WITH MEMBER [Measures].[TimeSpan] as 
case
 WHEN [Action Types].[Name].CURRENTMEMBER IS [Action Types].[Name].&[added]       THEN datediff('D',[Date].[Date].CURRENTMEMBER.NAME,Now())
WHEN [Action Types].[Name].CURRENTMEMBER IS  [Action Types].[Name].&  [removed] THEN -datediff('D',[Date].[Date].CURRENTMEMBER.NAME,Now())
ELSE  0
END

SELECT [Measures].[TimeSpan] ON 0,
NONEMPTY([Date].[Date].Children*[Action Types].[NAME].Children)on 1
FROM [OLAP Prep]

获得这样的输出


                           TimeSpan
2014-07-23 00:00:00.000 added
  34
2014-07-23 00:00:00.000 connected
     0
2014-07-23 00:00:00.000 disconnected
     0
2014-07-23 00:00:00.000 removed
     -34
2014-07-25 00:00:00.000 added
  32
2014-07-25 00:00:00.000 connected
0
2014-07-25 00:00:00.000 disconnected
 0
 2014-08-04 00:00:00.000 added
   22

但它没有多大帮助,我希望将这个timepan值作为每个数据库行的“Amount”度量,然后我可以做总结...这很难解释。它应该是一个实际上是一个名为“timespan”(measure)的数据表的列,但它是按今天的日期计算的,不能在datatable中创建,后来我在dsv中使用GETDATE()创建了一个命名计算列。但是GETDATE()是处理日期,所以我必须在这里创建一个计算的度量...

我在t-sql中使用的查询:

 SELECT DATENAME(DAY,a.[Date])+'/'+ DATENAME(MONTH,a.[Date]) AS 'X',
 SUM(CASE WHEN a.[Action]='added' THEN DATEDIFF(DAY, a.[Date],GETDATE()) 
 ELSE                           DATEDIFF(DAY, a.[Date],GETDATE())*-1 END)
 AS 'TimeSpan',
 FROM FactData a
 GROUP BY a.[Date]

1 个答案:

答案 0 :(得分:0)

[Dim Action Types]也需要在行上,因为目前在[Dim Action Types] * [Dim Action Targets]上没有交叉 - 让我知道它是否有效。