MDX TTM Calculated member - day level - mdx

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]

Related

Finding related date to Calculated Measure in MDX

BackStory
I am working in MDX and my measure groups use many calculations. Many of these are timeframes such as [Date].[Fiscal].[LY WTD] which stands for last year week to date.
To calculate this I was using the ParellelPeriods function. However as our calendar has 52 weeks for one year and 53 for the current year my calculations are off.
I have made my changes using
CREATE MEMBER CURRENTCUBE.[Date].[Fiscal].[LY WTD] as
Aggregate(
Exists(
[Date].[Date].[Date].members,
{
[Date].[F Day Of Week].[F Day Of Week].item(0) :
Exists([Date].[F Day Of Week].[F Day Of Week].members, [Date].[Last Complete Day].&[Y]).item(0)
}
* Exists([Date].[F Year].[F Year].members,[Date].[Last Complete Day].&[Y]).item(0).lag(1)
* Exists([Date].[F Week of Year].[F Week of Year].members, [Date].[Current Week].&[Current Week])
)
), VISIBLE = 0;
I now need to test this.
Problem
How can I determine what week my measure [Date].[Fiscal].[LY WTD] is pulling back?
Could you add a test member like this?
CREATE
MEMBER CURRENTCUBE.[Date].[Fiscal].[LY WTD - Caption] AS
Exists
(
[Date].[F Week of Year].[F Week of Year].MEMBERS
,
{
[Date].[F Day Of Week].[F Day Of Week].Item(0)
:
Exists
(
[Date].[F Day Of Week].[F Day Of Week].MEMBERS
,[Date].[Last Complete Day].&[Y]
).Item(0)
}*
Exists
(
[Date].[F Year].[F Year].MEMBERS
,[Date].[Last Complete Day].&[Y]
).Item(0).Lag(1)*
Exists
(
[Date].[F Week of Year].[F Week of Year].MEMBERS
,[Date].[Current Week].&[Current Week]
)
).Item(0).Item(0).Member_Caption
,VISIBLE = 1 ;
I think my problem is that as I am aggregating my calc script it is impossible to then see the individual original members. Here is what I ended up doing.
1. First I used the same script that I was using to make my calculation in a mdx query and ensured I got the dates I expected.
Select
{Measures.[Gross Units]} on 0,
Exists(
[Date].[Date].[Date].members,
{
[Date].[F Day Of Week].[F Day Of Week].item(0) :
Exists([Date].[F Day Of Week].[F Day Of Week].members, [Date].[Last Complete Day].&[Y]).item(0)
}
* Exists([Date].[F Year].[F Year].members,[Date].[Last Complete Day].&[Y]).item(0).lag(1)
* Exists([Date].[F Day of Year].[F Day of Year].members, [Date].[Current Week].&[Current Week])
)
on 1
From myips
Results:
Gross Units 2015/05/09 (null) 2015/05/10 13,069 2015/05/11 35,853
2015/05/12 104,617 2015/05/13 46,318
2. Then I looked for the results, using my calculation for last year week to date and comparing that to a query scoped calculated member aggregated of those days
With Member [Date].[Fiscal].[Custom] as
Aggregate([Date].[Fiscal].[Date].&[2015-05-09T00:00:00]:[Date].[Fiscal].[Date].&[2015-05-13T00:00:00])
Select
[Date].[Fiscal].[Custom]
//Date.Fiscal.[LY WTD]
on 0,
Measures.[Gross Units]
on 1
From myIPS
**I got the same results! Success!**

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.

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
)
)

Various Moving Annual Totals in an SSAS Cube

This is how I am creating my moving totals as columns in my cube:
MATTY (Moving Annual Total This Year)
SUM ( { [Time Period].[Month].CURRENTMEMBER.Lag(12) : [Time Period].[Month].CURRENTMEMBER }, [Measures].[MeasureColumnName])
MATLY (Moving Annual Total Last Year)
SUM ( { [Time Period].[Month].CURRENTMEMBER.Lag(24) : [Time Period].[Month].CURRENTMEMBER.Lag(12) }, [Measures].[MeasureColumnName])
How do I do the following?
YTDTY (Year To Date This Year)
YTDLY (Year To Date Last Year - up to same date as today)
I suggest you have a look at the PeriodsToDate and YTD MDX functions on MSDN. Is there something more than those that you need?