Annualization in MDX - mdx

Introduction
Currently I'm doing a maintenance job on a measure which annualizes another (YTD) measure. The granularity of measures is month. The annualization works fine on month level, but when we use parents in the date hierachy the calculation does not work.
It should work like this:
MeasureAnnualized = YTDMeasure * (CurrentMonth / 12)
CurrentMonth is implemented as
ClosingPeriod([Date].[DateHierarchy].[Month], [Date].[DateHierarchy]).MemberValue
On month level this works fine:
Month MeasureYTD MeasureAnnualized ClosingPeriod
july -50 -85,71 7
august -60 -90,00 8
september (null) (null) 9
But on a higher level in the DateHierarchy this does not work. For instance on Quarter level:
Quarter MeasureYTD MeasureAnnualized ClosingPeriod
3 -60 -80,00 (should be : -90,00) 9 (should be: 8)
As you can see, it picks the ClosingPeriod of quarter 3 (=9) whereas this should be the last "non-empty" ClosingPeriod (=8). The same applies to Year level:
Year MeasureYTD MeasureAnnualized ClosingPeriod
2016 -60 -60,00 (should be : -90,00) 12 (should be: 8)
The question therefore is: how can I get this ClosingPeriod to be the last Period in which the YTD measure is not (null)?

In the definition on msdn of ClosingPeriod it gives an equivalent nested version of the same logic:
Tail(Descendants(Member_Expression, Level_Expression), 1)
The below is similar to this with an added NonEmpty:
WITH
MEMBER Measures.[LastNonEmpty] AS
Tail
(
NonEmpty
(
(EXISTING
[Date].[Calendar].[Month])
,[Measures].[Internet Sales Amount]
)
,1
).Item(0).MemberValue
SELECT
Measures.[LastNonEmpty] ON 0
,[Date].[Calendar].[Calendar Year].MEMBERS ON 1
FROM [Adventure Works];
It returns the following:

Related

How to get Next available Date in MDX

I would like to get next available date for next month based on working day.
I am using Businessday = 1 as a working day.
Next available date for September is 09/03/2019 for running MDX on 08/31/2019.
I tried:
[Time].[Date].NextMember.MEMBER_VALUE
without any filter and it returns 09/1/2019.
If I do
[Time].[Date].LEAD(2).MEMBER_VALUE
then it give 09/02/2019 but this date is not marked as a business day according to my Businessday = 1
The result should be 09/03/2019
Here is the example that I made on Adventure Works cube - just instead of Businessday measure I am using [Measures].[Internet Order Count] measure.
So, in this example, for each month, calculated member [Measures].[NextWorkingDay] is showing first date in the next month that has Internet Order Count = 8 (you will just say Businessday = 1)
WITH MEMBER [Measures].[NextWorkingDay] AS
FILTER(Descendants([Date].[Calendar].Currentmember.Lead(1),,leaves)
, [Measures].[Internet Order Count]= 8).ITEM(0).NAME
SELECT { [Measures].[NextWorkingDay]
} ON 0,
[Date].[Calendar].[Month].MEMBERS ON 1
FROM [Adventure Works]

MDX Syntax to retrieve Specific Dates

I am building some reports in Pyramid Analytics. I created a custom set and tried to write a MDX query which will retrieve the first day of the first month for the first three quarters of the previous year, selected in the slicer. i.e.
If I choose in my slicer 2017, I expect to see the following in the body of the report: Jan 1st 2016, April 1st 2016 and July 1st 2016. These will change according to the selection in the slicer.
I only got as far as the syntax below, which only returns first day of the first month of the first quarter of the previous year i.e. Jan 1st 2016
[Policy - Inception Date].[Calendar Hierarchy].[!#NewPar#!].PREVMEMBER.FIRSTCHILD.FIRSTCHILD.FIRSTCHILD
Note:Newpar = Parameter
Can you please help with the correct syntax?
Thanks.
Without knowing the structure of your Date dimension I'm having to use some guess work but I've made the following in Pyramid and it works fine against our cube:
Generate
(
Head //<<<this will get the first 3 quarters
(
Descendants
(
{[Date].[Date - Calendar Month].[!#aYear#!].PrevMember} //<<<this will get the previous year
,[Date].[Date - Calendar Month].[Calendar Quarter]
)
,3
)
,Head //<<<this will get the first day of each of the quarters found in the first argument of the generate function
(
Descendants
(
[Date].[Date - Calendar Month].CurrentMember
,[Date].[Date - Calendar Month].[Calendar Day]
)
,1
)
)
Here is the result with the year parameter in the top left:

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 to display this year's sale along with the last year's sale

Here's my query. I want to display this year's data from week 12 to week 24 along with last year's data from week 12 to week 24. But I got the same data for both years even I had the "prevMember". Could you help me?
WITH MEMBER [Measures].[Net Sales This Year] AS ([Measures].[Ticket Net Sales],[Date].[Fiscal].[Fiscal Year].currentmember.prevmember)
SELECT {[Measures].[Ticket Net Sales],[Measures].[Net Sales Last Year]} on 0,
NON EMPTY {([Concepts].[Concept Name].[Concept Name],[Locations].[Department Name].[Department Name],
[Date].[Fiscal].[Fiscal Year].Members,[Date].[Week Of Year].&[12]:[Date].[Week Of Year].&[24])} on 1
FROM spbi
You can use the PARALLELPERIOD() function to achieve this.
See http://technet.microsoft.com/en-us/library/ms145500
Could you try this query:
WITH MEMBER [Measures].[Net Sales This Year] AS ([Measures].[Ticket Net Sales],[Date].[Fiscal].CurrentMember.PrevMember)
SELECT {[Measures].[Ticket Net Sales],[Measures].[Net Sales Last Year]} ON 0,
NON EMPTY [Date].[Fiscal].[Fiscal Year].Members * [Date].[Week Of Year].&[12]:[Date].[Week Of Year].&[24]) ON 1
FROM spbi

How to get the measure at specific day in that week using MDX

I am new to MDX,
I have a requirement, which is using the measure on that Saturday as the default of that week, I have a time dimension [CALENDAR],
but there are multiple hierachies, different hierachies have different week start, that means in [CALENDAR].[h1].[DATE], the week start may be Thursday(weekday in nature calendar), in the other may be Sunday(in nature calendar),
so I have to using [CALENDAR].[DATE], which is nature calendar, I can get the corresponding Saturday,
with member measures.[Weekday]
as datepart('w',[CALENDAR].[Fiscal].currentmember)
member measures.[SatDay]
as dateadd('d'
,7-datepart('w',[CALENDAR].[Fiscal].currentmember.MemberValue)
,[CALENDAR].[Fiscal].currentmember.MemberValue)
select
{
[Measures].[Plan Count]
,measures.[Weekday]
,measures.[SatDay]
} on 0,
[CALENDAR].[Fiscal].[Date] on 1
from [MyCube]
but how can I the measure on that day?
Try a sub-select using your date hierarchy containing week (Year-Week-Date).
with member [Measures].[WeekDate] as
dateadd('d' ,7 - datepart('w',[Dim Date].[Year - Week - Date].currentmember.MemberValue)
,[Dim Date].[Year - Week - Date].currentmember.MemberValue)
select {[Measures].[WeekDate], [Measures].[Plan Count]} on 0,
[Dim Date].[Year - Week - Date].Children on 1
from (
select ([Dim Date].[Year - Week - Date].[Week]) on 0
from [cube]
where (
//filters if necessary
)
)