如何从 SSAS 多维立方体 (MDX) 查询每个日期的 3 个月 运行 平均值?

How to query 3 month running average from SSAS Multidimensional Cube (MDX) for each date?

我有 SSAS 多维立方体,我需要查询 3 个月 运行 日期范围内每一天的平均值

例如,对于 2016-04-25 我必须获取数据 从 2016-01-01 到 2016-03-31. 所以我不能使用这个查询(因为我不知道我必须滞后到上个月多少天):

WITH MEMBER [Measures].[SalesAmount 3m average] AS
(
SUM(
    ([Date].[Date].CurrentMember.Lag(90) :
    ([Date].[Date].CurrentMember.Lag(1),
    [Measures].[SalesAmount]
    )
)

我想我需要使用 Ancestor 函数来获取月份并使用延迟到月份的粒度。

好的,让我们试试这个:

WITH MEMBER [Measures].[SalesAmount 3m average] AS
(
SUM(
    (Ancestor ( [Date].[Date].CurrentMember, [Date].[Month] )).Lag(3) :
    (Ancestor ( [Date].[Date].CurrentMember, [Date].[Month] )).Lag(1),
    [Measures].[SalesAmount]
    )
)
SELECT { [Measures].[SalesAmount 3m average] } ON Columns,
{ [Date].[Date].&[2016-01-01T00:00:00] : [Date].[Date].&[2016-02-28T00:00:00]} On Rows
FROM [Cube]

很遗憾,此查询无法正常工作(returns null)。

如何解决这个问题?

更新: 好的,我尝试查询会员标题:

MEMBER [Measures].[Test] AS
(
(Ancestor 
    (   [Date].[Date].CurrentMember, 
        [Date].[Date].[Month] )
    ).Item(0).Member_Caption
)

我得到的不是月份标题,而是日期(日)- 与 [Date].[Date].CurrentMember 相同的日期。

然后我尝试了这个查询:

--First try
MEMBER [Measures].[Test] AS
(
IsAncestor([Date].[Date].CurrentMember, [Date].[Month].&[2016-05-01T00:00:00])
) 

--Second try
MEMBER [Measures].[Test] AS
(
IsAncestor([Date].[Date].CurrentMember, [Date].[Date].[Month].&[2016-05-01T00:00:00])
) 

--Third try
MEMBER [Measures].[Test] AS
(
IsAncestor([Date].[Date].CurrentMember, [Date].[Year -  Quarter -  Month -  Date].[Month].&[2016-05-01T00:00:00])
) 

所有查询的结果都是 "False"。那么,Month 成员不是 Date 成员的祖先???现在我真的很困惑。

我的日期维度如下所示:

更新 2:

MEMBER [Measures].[Test] AS
(
(Ancestor 
    (   [Date].[Date].CurrentMember, 
        1 )
    ).Member_Caption
)

Returns: 全部

你的代码看起来不错。我会在几个地方添加 item(0) 以使事情更明显,但这应该不会有太大变化

WITH 
  MEMBER [Measures].[SalesAmount 3m average] AS 
    Sum
    (
        Ancestor
        (
          [Date].[Calendar].CurrentMember
         ,[Date].[Calendar].[Month]
        ).Item(0).Lag(3)
      : 
        Ancestor
        (
          [Date].[Calendar].CurrentMember
         ,[Date].[Calendar].[Month]
        ).Item(0).Lag(1)
     ,[Measures].[Internet Sales Amount]
    ) 
SELECT 
  {[Measures].[SalesAmount 3m average]} ON COLUMNS
 ,{
      [Date].[Calendar].[Date].&[20060222]
    : 
      [Date].[Calendar].[Date].&[20060722]
  } ON ROWS
FROM [Adventure Works];

Returns这个:

检查你所有的关卡表达式是否正确:

[Date].[Date] ?

[Date].[Month] ?

也作为进一步测试,这return是什么?

WITH 
  MEMBER [Measures].[test1] AS 
    Ancestor
    (
      [Date].[Date].CurrentMember
     ,[Date].[Month]
    ).Item(0).Lag(3).Member_Caption 
  MEMBER [Measures].[test2] AS 
    Ancestor
    (
      [Date].[Date].CurrentMember
     ,[Date].[Month]
    ).Item(0).Lag(1).Member_Caption 
SELECT 
  {
    [Measures].[test1]
   ,[Measures].[test2]
  } ON COLUMNS
 ,{
      [Date].[Date].&[2016-01-01T00:00:00]
    : 
      [Date].[Date].&[2016-02-28T00:00:00]
  } ON ROWS
FROM [Cube];

祖先将在同一层次结构的不同级别之间发挥作用,即:

这意味着 [Date].[Date][Date].[Month] 并没有真正通过 Ancestor 相关。试试这个:

WITH 
MEMBER [Measures].[SalesAmount 3m average] AS
  (
    SUM(
      Ancestor ( 
         [Date].[Year -  Quarter -  Month -  Date].CurrentMember,
         [Date].[Year -  Quarter -  Month -  Date].[Month] ).Lag(3) 
       :
      Ancestor( 
         [Date].[Year -  Quarter -  Month -  Date].CurrentMember
        ,[Date].[Year -  Quarter -  Month -  Date].[Month] ).Lag(1)
      ,[Measures].[SalesAmount]
    )
  )
SELECT 
{ [Measures].[SalesAmount 3m average] } ON Columns,
{ 
  [Date].[Year -  Quarter -  Month -  Date].[Date].&[2016-01-01T00:00:00] 
: [Date].[Year -  Quarter -  Month -  Date].[Date].&[2016-02-28T00:00:00]
//I suspect the above 2 lines can be replaced by following:
//  [Date].[Date].&[2016-01-01T00:00:00]
//: [Date].[Date].&[2016-02-28T00:00:00]
}  
  On Rows
FROM [Cube];

注意

这应该 return All 因为层次结构 [Date].[Date] 只有两个级别...叶级别是在该级别之上有一个级别的日期 All:

MEMBER [Measures].[Test] AS
(
(Ancestor 
    (   [Date].[Date].CurrentMember, 
        1 )
    ).Member_Caption
)