1

我有 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]

不幸的是,这个查询不能正常工作(返回 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])
) 

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

我的日期维度如下所示:

在此处输入图像描述

更新 2:

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

退货:全部

4

1 回答 1

1

你的代码看起来不错。我会在几个地方添加 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];

返回这个:

在此处输入图像描述

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

[Date].[Date] ?

[Date].[Month] ?

另外作为进一步的测试,这会返回什么?

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];

Ancestor 将在同一层次结构的不同级别之间运行,即:

在此处输入图像描述

这意味着它与祖先[Date].[Date]并没有真正相关。[Date].[Month]尝试这个:

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];

笔记

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

MEMBER [Measures].[Test] AS
(
(Ancestor 
    (   [Date].[Date].CurrentMember, 
        1 )
    ).Member_Caption
)
于 2016-04-25T08:41:06.480 回答