Report Portal

MDX-How do I calculate sales for 12 Month to date?

Q: How do I calculate sales for 12 Month to date in MDX?

A: We have to assume that your date dimension last member represents last month. Then to get last 12 month we can use Lag function. Example:

SELECT [Measures].[Order Count] ON 0
, ClosingPeriod([Date].[Calendar].[Month], [Date].[Calendar].[All Periods]).Lag(12)
: ClosingPeriod([Date].[Calendar].[Month], [Date].[Calendar].[All Periods]) ON 1
FROM [Adventure Works]

Result:

 Order Count
August 20031,759
September 20031,783
October 20031,779
November 20031,888
December 20032,272
January 20041,944
February 20042,030
March 20042,108
April 20042,127
May 20042,385
June 20042,374
July 2004976
August 2004(null)

Now to get sum of sales query could be :

WITH MEMBER [Measures].[Last 12 Mth Order Count] AS
SUM(
ClosingPeriod([Date].[Calendar].[Month], [Date].[Calendar].[All Periods]).Lag(12)
: ClosingPeriod([Date].[Calendar].[Month], [Date].[Calendar].[All Periods])
, [Measures].[Order Count]
)
SELECT [Measures].[Last 12 Mth Order Count] ON 0
FROM [Adventure Works]

Result

Last 12 Mth Order Count
23,425

Tags: mdx, faq

 

2007-2015 VidasSoft Systems Inc.