Hi guys,
Would someone mind checking my MDX for me, I just want to make sure it's as optimised as possible. I will give some info first so you can understand it easily.
So I have a fact table with cash margin and a date table which looks something like this.
![]()
A 1 in the working day column means it's a working day so doing a sum on 01/01/2018 - 05/01/2018 is 4 working days. As such this is a measure in my cube.
I then have an MDX which calculates the Prior Year Cash Margin:
CREATE DYNAMIC SET CURRENTCUBE.[Month Set]
AS [Date].[Calendar].[Month];
CREATE MEMBER CURRENTCUBE.[Measures].[Invoiced PY Cash Margin]
AS SUM
(
GENERATE (EXISTING [Month Set]
,{
PARALLELPERIOD
(
[Date].[Calendar].[Year]
,1
,[Date].[Calendar].CURRENTMEMBER
)
}
)
,[Measures].[Invoiced Cash Margin]
)
Please be aware that the Cash Margin is a calculated member (Sales-Cost).
Then i needed to adjust the previous year for the number of comparable days... (PY Cash Margin / PY Working Days * CY Working Days)
So I added these 2 measures:
CREATE MEMBER CURRENTCUBE.[Measures].[PY Working Days]
AS SUM
(
GENERATE (EXISTING [Month Set]
,{
PARALLELPERIOD
(
[Date].[Calendar].[Year]
,1
,[Date].[Calendar].CURRENTMEMBER
)
}
)
,[Measures].[Working Days]
)
CREATE MEMBER CURRENTCUBE.[Measures].[Invoiced PY Comparable Cash Margin]
AS DIVIDE([Measures].[Invoiced PY Cash Margin],[Measures].[PY Working Days],0)*[Measures].[Working Days]
However on a small set of sample data this is what was returned in Excel:
![]()
Notice that the PY Comparable Cash Margin does not add up... that's because the total is doing (58,000,000/60*62)
But it needs to do it on each month to calculate it properly... so actual result should be:
![]()
So to correct this and to force the calculation to always be done for every month I did this:
CREATE MEMBER CURRENTCUBE.[Measures].[Invoiced PY Comparable Cash Margin]
AS SUM
(
GENERATE (EXISTING [Month Set]
,{
PARALLELPERIOD
(
[Date].[Calendar].[Year]
,1
,[Date].[Calendar].CURRENTMEMBER
)
}
)
,DIVIDE([Measures].[Invoiced Cash Margin],[Measures].[Working Days])*
(PARALLELPERIOD
(
[Date].[Calendar].[Year]
,-1
,[Date].[Calendar].CURRENTMEMBER
),
[Measures].[Working Days]
)
)
My problem with it is that it doesn't leverage the Prior Year Measure and calculates the Prior Year from scratch so won't utilise a cache if one exists. Can anyone think of a better way?
I hope that all makes sense.
Thanks,
Joe