MDX如何计算过去36个月的平均值/总和

时间:2015-02-23 09:34:36

标签: sql-server mdx analysis

WITH 
  MEMBER AvgOfLast36Month AS 
    Sum
    (
      LastPeriods
      (2
       ,[Dim Date].[Month].CurrentMember
      )
     ,[Measures].[capacity]
    ) 
SELECT 
  {AvgOfLast36Month} ON 0
 ,{[Project].[Model].[Model]} ON 1
FROM [CubeCapacity]
WHERE 
  StrToSet('[Dim Date].[Date].&[2014-02-20T00:00:00]&[02]&[1]&[2014]');

我试图总结过去36个月的平均容量。我没有得到准确的总数。任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

我相信这个表达式会返回 Sum

  MEMBER AvgOfLast36Month AS 
    Sum
    (
      LastPeriods
      (2
       ,[Dim Date].[Month].CurrentMember
      )
     ,[Measures].[capacity]
    ) 

MDXAVG函数 - 最好尝试使用它:https://msdn.microsoft.com/en-us/library/ms146067.aspx

您脚本的这一部分正在过滤上下文并导致问题:

WHERE 
  StrToSet('[Dim Date].[Date].&[2014-02-20T00:00:00]&[02]&[1]&[2014]');

也许您可以将此动态strTo..代码段移动到WITH子句?这个advWrks脚本就是一个例子:

WITH 
  SET [x] AS 
    LastPeriods
    (36
     ,Exists
      (
        [Date].[Calendar].[Month].MEMBERS
       ,StrToMember('[Date].[Date].&[20070713]')
      ).Item(0).Item(0)
    ) 
  MEMBER [Measures].[avgOverSet] AS 
    Avg
    (
      [x]
     ,[Measures].[Internet Sales Amount]
    ) 
SELECT 
  {
    [Measures].[Internet Sales Amount]
   ,[Measures].[avgOverSet]
  } ON 0
 ,NON EMPTY 
    [x] ON 1
FROM [Adventure Works];

以下是行上的国家/地区:

WITH 
  SET [36mth] AS 
    LastPeriods
    (36
     ,Exists
      (
        [Date].[Calendar].[Month].MEMBERS
       ,StrToMember('[Date].[Date].&[20070713]')
      ).Item(0).Item(0)
    ) 
  MEMBER [Measures].[avgOver36mths] AS 
    Avg
    (
      [36mth]
     ,[Measures].[Internet Sales Amount]
    ) 
SELECT 
  {[Measures].[avgOver36mths]} ON 0
 ,[Customer].[Customer Geography].[Country] ON 1
FROM [Adventure Works];