How to select shifted time periods in MDX? - mdx

I am beginner in MDX. I am trying to calculate the increase in revenue from the previous year using the Adventure Works cube in a way that I will have a column that will show the difference in revenue between 2 years. Until now I was able to calculate the difference manually using:
(KpiValue("Revenue"), [Date].[Fiscal Year].&[2009]) - KpiValue("Revenue"),
[Date].[Fiscal Year].&[2008]
(KpiValue("Revenue"), [Date].[Fiscal Year].&[2008]) - KpiValue("Revenue"),
[Date].[Fiscal Year].&[2007]
(KpiValue("Revenue"), [Date].[Fiscal Year].&[2007]) - KpiValue("Revenue"),
[Date].[Fiscal Year].&[2006]
Can you suggest me more efficient way to calculate this. I think I can use the Parallel Processing function but I am struggling to understand how it works.
Thank you!
Edit:
Here is the code I use:
with member [diff] as
(KPIValue("Revenue"), [Date].[Fiscal Year].&[2008]) -
(KPIValue("Revenue"), [Date].[Fiscal Year].&[2007])
Select
{diff} on 0
FROM [Adventure works]

Try to use the PrevMember:
With
Member [Measures].[Diff] as
[Measures].[Revenue] - ([Date].[Fiscal Year].PrevMember,[Measures].[Revenue])
Select
[Measures].[Diff] on 0,
[Date].[Fiscal Year].[Year].Members on 1
From [Adventure Works]

Related

PY YTD calculated measure that can handle leap years MDX

This might be a longshot, but maybe there is an ssas/mdx guru here that has stumpled upon the same problem regarding aggregated calculations and leap years. So what I am trying to do, is to create a PY YTD calculated measure in an ssas cube that can handle leap years. The issue I am facing is that the calculated measure becomes extremely slow based on the logic below (see code sample). Has anyone found a better way to handle leap years or has a document with best practise to share ? I am assume that if-statements and NonEmpty function could be performance deadly for a calculated measure. All tips are greatly appreciated (Doesn't have to be a solution).
-- YTD PRIOR YEAR
[Time Calculations].[YTD Pr Yr] = IIF([Calendar].[Year - Month - Day].CurrentMember IS [Calendar].[Year - Month - Day].[Month].&[201702]
,Aggregate(
NonEmptyCrossjoin({[Time Calculations].[Current Period]},
PeriodsToDate(
[Calendar].[Year - Month - Day].[Year],
[Calendar].[Year - Month - Day].[Day].&[2016-02-29 00:00:00.000])
))
,IIF([Calendar].[Year - Month - Day].CurrentMember IS [Calendar].[Year - Month - Day].[Month].&[201602]
,Aggregate(
NonEmptyCrossjoin({[Time Calculations].[Current Period]},
PeriodsToDate(
[Calendar].[Year - Month - Day].[Year],
[Calendar].[Year - Month - Day].[Day].&[2015-02-28 00:00:00.000])
))
,(Aggregate(
NonEmptyCrossjoin({[Time Calculations].[Current Period]},
PeriodsToDate(
[Calendar].[Year - Month - Day].[Year],
ParallelPeriod(
[Calendar].[Year - Month - Day].[Year],1,
TAIL(DESCENDANTS([Calendar].[Year - Month - Day].CurrentMember
, [Calendar].[Year - Month - Day].[Day]),1).ITEM(0)))
)
)
)
)
);
Best regards,
Rubrix
Try the following, some caveats follow:
CREATE MEMBER CURRENTCUBE.[Time Calculations].[YTD Prior Year] AS NULL;
/* Make sure the scope is for all days in all years in your calendar year */
Scope([Invoice Date].[Calendar Year].[Calendar Year].members, [Invoice Date].[Calendar Day].members);
// YTD PRIOR YEAR
([Time Calculations].[YTD Prior Year] =
iif(
/* Check to see if the prior year member is empty */
isempty(
ParallelPeriod(
[Invoice Date].[CY Hierarchy].[Calendar Year],
1,
[Invoice Date].[CY Hierarchy].CurrentMember
).MemberValue
),
/* If so, use the .LastChild */
Aggregate(
Crossjoin(
{[Time Calculations].[Current Period]},
PeriodsToDate(
[Invoice Date].[CY Hierarchy].[Calendar Year],
ParallelPeriod(
[Invoice Date].[CY Hierarchy].[Calendar Year],
1,
Ancestor(
[Invoice Date].[CY Hierarchy].CurrentMember,
[Invoice Date].[CY Hierarchy].[Calendar Month]
)
).LastChild
)
)
),
/* Otherwise just use the prior year */
Aggregate(
Crossjoin(
{[Time Calculations].[Current Period]},
PeriodsToDate(
[Invoice Date].[CY Hierarchy].[Calendar Year],
ParallelPeriod(
[Invoice Date].[CY Hierarchy].[Calendar Year],
1,
[Invoice Date].[CY Hierarchy].CurrentMember
)
)
)
)
)
);
End Scope;
One possible hitch is that if you have several days in a row where there are no transactions, .LastChild might not work accurately. I didn't have that situation when I originally developed this code. It was specifically developed to deal with this exact case of prior year-to-date and leap year nuances. It might need to be tweaked if that's the case.
This assumes that you have a proper time dimension and a time calculations dimension, which it looks like you do from the code sample you provided.
I would say that the performance of this solution is pretty great even on large cubes (hundreds of millions of rows).

Exclude few dates in a month in mdx query

Below is my query. My requirement here is I want to exclude a few dates sales in a particular month while I am looking for month wise data.
For example: I don't want to include Aug 15, 2008 date sales for the month of August, 2008 when I am looking 2008 year for the month of August, 2008 data for accessories product.
select
[Measures].[Internet Sales Amount] on 0,([Product].[Category].&[4],
[Date].[Calendar].[Month]
)on 1
from [Adventure Works]
where [Date].[Calendar Year].&[2008].
I would appropriate, if you could help in this regard.
You have to exclude the member within the Where clause:
select
[Measures].[Internet Sales Amount] on 0,
([Product].[Category].&[4],[Date].[Calendar].[Month]) on 1
from [Adventure Works]
where ([Date].[Calendar Year].&[2008],{[Date].[Day].[Day].Members - [Date].[Day].&[20080815]})

SUM of a YTD-value by Year for several selected years

I am trying to put together an SQL statement that returns the SUM of a value by Year for several selected years, but on a year to date basis. In other words, running the report by selecting years I need the value for the selected years on a year to date basis. This could be something like this: By selecting the years 2015, 2014 until 2010 the Report should give 2015 value ytd of 25.02.2015, 2014 ytd of 25.02.2014 and so on.
Yet I only figured out how to select the Dates for each year to date value manually., but clearly the I was thinking. However, I was hoping to have some of this work handled with my SQL Statement using the selected years Parameter and add month and day somehow.
Has anyone ever tackled this type of problem with an SQL statement, and if so, what is the trick that I am missing?
My current sql statement for ytd data is similar to the following:
SELECT NON EMPTY { [Measures].[Sales Turnover YTD], [Measures].[Order Intake YTD] } ON COLUMNS FROM ( SELECT ( STRTOSET(#DateDate, CONSTRAINED)
This works perfectly for the one year, but still I am seeking the solution for several years.
As described above my desired result would be: after choosing several fiscal years (e.g. 2010-2015) I will get following values:
2015 Sales Turnover YTD Order Intake YTD on date today
2014 Sales Turnover YTD Order Intake YTD on date today one year ago
and so on..
Here is a script against AdvWrks that calculates the equivalent YTD totals for each of the years ON ROWS:
WITH
SET [NonEmptyDates] AS
NonEmpty([Date].[Calendar].[Date].MEMBERS)
SET [LastNonEmptyDate] AS
Tail([NonEmptyDates])
SET [SetDaysInCurrentYear] AS
Descendants
(
Exists
(
[Date].[Calendar].[Calendar Year]
,[LastNonEmptyDate].Item(0).Item(0)
).Item(0)
,[Date].[Calendar].[Date]
)
MEMBER [Measures].[NumDaysInCurrentYear] AS
Rank
(
[LastNonEmptyDate].Item(0)
,[SetDaysInCurrentYear]
)
MEMBER [Measures].[EquivYTD] AS
Aggregate
(
Head
(
Descendants
(
[Date].[Calendar].CurrentMember
,[Date].[Calendar].[Date]
)
,[Measures].[NumDaysInCurrentYear]
)
,[Measures].[Internet Sales Amount]
)
SELECT
{
[Measures].[Internet Sales Amount]
,[Measures].[EquivYTD]
} ON 0
,[Date].[Calendar].[Calendar Year] ON 1
FROM [Adventure Works];

MDX query producing #Error as output

In my SSAS Cube, I have a measure called [Sales Total]. What I want to do is to create another measure that would give me the lowest sales figure in the last 6 months. I want this to be a moving minimum, calculated as the min of sales of every time period from the present month to 6 months back.
I wrote my MDX statement but it produces an error and I have hard time trying to figure out why. It is something to do with aggregating Date dimension members into a filter aggregate.
When i choose a single month member from the Date hierarchy, it returns the correct value. When I select multiple members from the hierarchy, as seen below, it errors out.
Any kind of help is appreciated.
WITH
MEMBER [Measures].[Min Sales Total Rolling 6 months] as '(MIN([Date].[Fiscal Month Hierarchy].currentmember.lag(6):[Date].[Fiscal Month Hierarchy].currentmember,[Measures].[Sales Total]))'
MEMBER [Date].[Fiscal Month Hierarchy].[FilterAggregate] as
'AGGREGATE({
[Date].[Fiscal Month Hierarchy].[Quarter].&[20141].&[201310],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20141].&[201311],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20141].&[201312],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20142].&[201401],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20142].&[201402],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20142].&[201403],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20143].&[201404],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20143].&[201405],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20143].&[201406],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20144].&[201407]
})'
SELECT {
[Measures].[Min Sales Total Rolling 6 months]} ON AXIS(0)
FROM [My Cube]
WHERE ([Date].[Fiscal Month Hierarchy].[FilterAggregate])
As explained in this blog by one of the SSAS developers already many years ago, multi select does not work with CurrentMember. You should use
MEMBER [Measures].[Min Sales Total Rolling 6 months] as
MIN(Tail(EXISTING [Date].[Fiscal Month Hierarchy].[Fiscal Month Hierarchy].Members).Item(0).Item(0).lag(6)
:
Tail(EXISTING [Date].[Fiscal Month Hierarchy].[Fiscal Month Hierarchy].Members).Item(0).Item(0),
[Measures].[Sales Total])
instead.
EXISTING gets the set of all selected members. This is needed as there is no single CurrentMember. Then Tail gets the set consisting of the last of these members, Item(0).Item(0) converts that single element set to a member.

MDX TTM Calculated member - day level

I need to create a calculated member that calculates revenue for TTM (Trailing Twelve Months) associated to selected date (day level).
I tried something like this:
SUM(
{
[Accounting Effective Date].[Date Hierarchy].CurrentMember.Lag(365)
: [Accounting Effective Date].[Date Hierarchy].CurrentMember
},
[Measures].[Revenue]
)
But this doesn't work with leap year, for example if I pick 2013-01-01 than it returns 2012-01-02.
I have also tried this but this one is also not good since it takes first day of the month:
SUM(
{
[Accounting Effective Date].[Date Hierarchy].CurrentMember.Parent.Lag(12).FirstChild
: [Accounting Effective Date].[Date Hierarchy].CurrentMember
},
[Measures].[Revenue]
)
Date hierarhy is following:
Year > Quarter > Month > Date
the following query can help, it uses cousin function to return last years date.
with member [Measures].[TestValue] as
(cousin([Date].[Calendar].currentmember,[Date].[Calendar].currentmember.parent.parent.lag(1)),[Measures].[Internet Sales Amount])
member [Measures].[TestDate] as
cousin([Date].[Calendar].currentmember,[Date].[Calendar].currentmember.parent.parent.lag(1)).item(0).name
select
{[Measures].[Internet Sales Amount],[Measures].[TestValue],[Measures].[TestDate]}
on columns,
{
[Date].[Calendar].[Date].&[20130922]
}
on rows from
[adventure works]