过去24个月的移动平均值

时间:2015-06-19 08:34:58

标签: ssas mdx

我有这个计算成员,计算过去12个月的移动平均值:

iif(IsEmpty(Sum({[Time].[Month].CurrentMember:NULL},

[Measures].[Count])), NULL,
Avg
(
   [Time].[Month].CurrentMember.Lag(11) : 
   [Time].[Month].CurrentMember, 
   [Measures].[Count]
))

iif条件已经到位,因为我不想获得未来几个月的价值(没有价值),没有它我会得到它。

我想要做的是仅在过去24个月以来才采取此措施。

我尝试过Tail and Lag但没有运气(我会在这里发布我的尝试但经过多次尝试后我删除它们并且真的不知道从哪里开始)。

感谢@whytheq这是我使用的最终解决方案:

CREATE DYNAMIC SET CURRENTCUBE.[FirstEmptyMonth]
 AS { Tail
      (
        NonEmpty
        (
          [Time].[Month].MEMBERS
         ,[Measures].[Count]
        )
       ,1
      ).Item(0).NextMember };        
CREATE DYNAMIC SET CURRENTCUBE.[MonthsToIgnore]
 AS {[FirstEmptyMonth].Item(0) : NULL}
    + 
      {NULL : [FirstEmptyMonth].Item(0).Lag(25)} ;                
CREATE MEMBER CURRENTCUBE.[Measures].[Moving Average]
 AS IIF
    (
      Intersect({[Time].[Month].CurrentMember},[MonthsToIgnore]).Count = 1
     ,null
     ,Avg
  (
    [Time].[Month].CurrentMember.Lag(11) : [Time].[Month].CurrentMember
   ,[Measures].[Count]
  )
    );

1 个答案:

答案 0 :(得分:1)

AdvWrks中,我得到了这个:

WITH 
  SET [FutureMonthsWithNoData] AS 
    {
        Tail
        (
          NonEmpty
          (
            [Date].[Calendar].[Month].MEMBERS
           ,[Measures].[Internet Sales Amount]
          )
         ,1
        ).Item(0).NextMember
      : NULL
    } 
  MEMBER [Measures].[blah] AS 
    IIF
    (
        Intersect
        (
          {[Date].[Calendar].CurrentMember}
         ,[FutureMonthsWithNoData]
        ).Count
      = 1
     ,null
     ,1
    ) 
SELECT 
  {
    [Measures].[Internet Sales Amount]
   ,[Measures].[blah]
  } ON 0
 ,[Date].[Calendar].[Month].MEMBERS ON 1
FROM [Adventure Works];

它返回:

enter image description here

所以我要说的是你可以创建这个初始的FutureDatesWithNoData集合,然后使用该集合在你的脚本中创建一个条件。这个集合(我认为)在你的立方体中:

SET [FutureMonthsWithNoData] AS 
{
    Tail
    (
      NonEmpty
      (
        [Time].[Month].[Month].MEMBERS
       ,[Measures].[Count]
      )
     ,1
    ).Item(0).NextMember
  : NULL
} 

您的衡量标准如下:

IIF
(
    Intersect
    (
      {[Time].[Month].CurrentMember}
     ,[FutureMonthsWithNoData]
    ).Count
  = 1
 ,NULL
 ,Avg
  (
    [Time].[Month].CurrentMember.Lag(11) : [Time].[Month].CurrentMember
   ,[Measures].[Count]
  )
)

如果你想在24个月前排除几个月,那么这个脚本总结了逻辑:

WITH 
  SET [FistEmptyMonth] AS 
    {
      Tail
      (
        NonEmpty
        (
          [Date].[Calendar].[Month].MEMBERS
         ,[Measures].[Internet Sales Amount]
        )
       ,1
      ).Item(0).NextMember
    } 
  SET [MonthsToIgnore] AS 
      {[FistEmptyMonth].Item(0) : NULL}
    + 
      {NULL : [FistEmptyMonth].Item(0).Lag(24)} 
  MEMBER [Measures].[blah] AS 
    IIF
    (
      Intersect({[Date].[Calendar].CurrentMember},[MonthsToIgnore]).Count = 1
     ,null
     ,1
    ) 
SELECT 
  {[Measures].[Internet Sales Amount]} ON 0
 ,[Date].[Calendar].[Month].MEMBERS ON 1
FROM [Adventure Works];