MDX计算所有时间的测量总和

时间:2014-06-16 03:34:00

标签: sql reporting-services mdx

我为SSRS报告开发了一个MDX查询,该报告有一个名为CurrentMonthAndAllOfLastYear的计算度量,它应该将当前月份的净销售额与上一年的净销售额相加。不幸的是,计算出的衡量标准似乎总是为每个净销售额加总。 MDX是:

WITH 
SET [CurrMonth] AS [Sales Period].[Sales Period].CurrentMember --Month
SET [CurrYear] AS ANCESTORS([CurrMonth](0), 2)   --Year
SET [PriorYear] AS [CurrYear](0).LAG(1)   -- Gets prior Year

MEMBER [Measures].[CurrentMonthAndAllOfLastYearAmt] AS SUM({[CurrMonth], [PriorYear]}, Measures].[Net Sales])
MEMBER [Measures].[Debtors Collection Days] AS ([Measures].[Total Owing Amount] * 90) / [Measures].[CurrentMonthAndAllOfLastYearAmt]

SELECT  
{
[Measures].[CurrentMonthAndAllOfLastYearAmt],
[Measures].[Total Owing Amount],
[Measures].[Debtors Collection Days],
[Measures].[Net Sales] } ON COLUMNS,
{
[Sales Period].[Sales Month].[Sales Month].ALLMEMBERS } ON ROWS 
FROM ( SELECT ( YTD(STRTOMEMBER('[Sales Period].[Sales Period].[Sales Month].&[2013-05-31T00:00:00]')) ) ON COLUMNS 
FROM ( SELECT ( STRTOSET('[Sales Representative Mirror].[Sales Representative ID].&[261]') ) ON COLUMNS 
FROM [Sales])) 

MDX代码段产生以下内容:

Sales Month CurrentMonthAndAllOfLastYearAmt 
31/01/2013  $320,429.60 ... 
28/02/2013  $320,429.60 ... 
31/03/2013  $320,429.60 ... 
30/04/2013  $320,429.60 ... 
31/05/2013  $320,429.60 ... 

我需要以下输出(2012年没有任何销售):

Sales Month  CurrentMonthAndAllOfLastYearAmt
31/01/2013  $0.00 ...
28/02/2013  $0.00 ...
31/03/2013  $0.00 ...
30/04/2013  $18,931.10 ...
31/05/2013  $22,557.22 ...

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我不会使用set来执行此操作,而是使用ParallelPeriod MDX函数。

MEMBER [Measures].[CurrentMonthAndAllOfLastYearAmt] AS 
   ([Sales Period].[Sales Period].CurrentMember,[Measures].[Net Sales]) 
   + 
   ( ParallelPeriod( [CurrMonth](0), 1, [Sales Period].[Sales Period].CurrentMember), [Measures].[Net Sales] )

好的问题是每次使用时都会评估集合,或者每个MDX请求评估一次。之后你就是上下文(使用或不使用where和子查询上下文)。