Finding related date to Calculated Measure in MDX - 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!**

Related

SSAS DAX time intelligence previous year and calculation gives wrong value

We want to show current periods sales versus previous period sales.
To show the previous period we make use of a date dimenion table and the following calculation:
CALCULATE(SalesValueGross; DATEADD(Date[Date]; -1; YEAR))
Unfortunately somehow it shows minor (decimal) differences when comparing years.
The difference get's bigger when we slice to months.
Another issue we have is that this calculation does not seem to work when comparing (for example) week 1 - 2015 with week 1 - 2014.
Any help is greatly appreciated!
When I want to get prior calendar year sales, I use the a formula such as the following:
Cal Prior Yr Sales:=if(HASONEVALUE('Sale Date'[Calendar Year]),
Calculate([Total Sales],
PREVIOUSYEAR('Sale Date'[Date])),BLANK())
The HASONEVALUE just ensures that there is only one year selected so it will know the correct previous year to retrieve.
You can make a series of calculations that will let you use one calc that determines what level of the date hierarchy you are in (assuming you have the fields available in your date table). Here is something I've used in the past, with a fiscal calendar that was different from the normal calendar.
First, the base calculations:
Sales Same Week Prior Year:=
CALCULATE([Total Sales],Filter(All('Sale Date'),
'Sale Date'[Week Key] = max('Sale Date'[Same Week Last Year])))
Sales Same Month Prior Year:=CALCULATE([Total Sales], Filter(All('Sale Date'),
'Sale Date'[Month Seq] = max('Sale Date'[Month Seq])-12))
Sales Same Quarter Prior Year:=CALCULATE([Total Sales], Filter(All('Sale Date'),
'Sale Date'[Quarter Seq] = max('Sale Date'[Quarter Seq])-4))
Sales Prior Year:=CALCULATE([Total Sales], Filter(All('Sale Date'),
'Sale Date'[Fiscal Year] = max('Sale Date'[Fiscal Year])-1))
You can hide all of those calculations and then create one last calculation and leave it visible:
Sales Same Period Last Year:=
if(HASONEVALUE('Sale Date'[Week Key]), [Sales Same Week Prior Year],
if(HASONEVALUE('Sale Date'[Month Key]),[Sales Same Month Prior Year],
if(HASONEVALUE('Sale Date'[Quarter Key]),[Sales Same Quarter Prior Year],
if(HASONEVALUE('Sale Date'[Fiscal Year]), [Sales Prior Year], BLANK()))))
You may need to add a couple of calculated fields to your date table to make it work. I have fields for: [Same Week Last Year], [Month Seq], [Quarter Seq]. Same week last year is an integer field that is yyyyww. Month Seq and Quarter Seq are just autoincrementing integers in chronological order that do not repeat.
My formula for same week last year is
=if('Sale Date'[Week Nbr] = 53, (('Sale Date'[Fiscal Year]-1) * 100) + ([Week Nbr]-1),
(('Sale Date'[Fiscal Year]-1) * 100) + ([Week Nbr]))
I did the sequence numbers in my SQL view, which is the source for the date date. As an example, if my date table starts at 1/1/2010, the month seq for Jan 2010 is 1 and the month seq for Jan 2011 is 13. The quarter seq for Q1 2010 is 1 and the quarter seq for Q1 2012 is 9.
http://www.daxpatterns.com/time-patterns/ is a good read for this topic.

mdx first value of a period

I have a cube with a 'sales' measure, where we have the amount of sold units each day. We have a time dimension with 'Year > week > day'.
I need to have the sales for the first and the last day in a period with non empty sales for a calculated field. I tried with OPENINGPERIOD and CLOSINGPERIOD but they return for a week period just Monday and Sunday... the problem is that sometimes Monday is holiday and Sunday is always holiday and I get empty data (we have no sales on holidays)... Here is my example for last day of a period:
WITH
MEMBER
[Measures].[sales end] AS
'((CLOSINGPERIOD([Time.Weeks].[Day],[Time.Weeks].CurrentMember)
, [Measures].[sales]))'
SELECT
{[Measures].[sales], [Measures].[sales end]} ON COLUMNS,
NON EMPTY
({[Time.Weeks].[Week].Members}) ON ROWS
FROM [cubSales]
I need to get the last day with non empty sales in that period... it should be Friday, but if Friday is holiday, it should be Thursday... I mean the last day for the week with a value for the measure sales..
An easy solution could be to just omit holiday days from your time dimension.
Mondrian has no concept of holidays. I would implement my own MDX function with a built-in list of holidays. It's a flexible approach which can be extended to support different holidays from different countries.
Tested on our cube and seems to work ok (just doesn't say anything about holidays!)
SELECT
{
[Measures].[sales]
} ON COLUMNS
,NON EMPTY
Generate
(
[Time.Weeks].MEMBERS
,Tail
(
NonEmpty
(
[Time.Weeks].CurrentMember * [Time.Weeks].[Day].MEMBERS //<<I think .members is generally default but should do the same thing if we are explicit
,[Measures].[sales]
)
)
) ON ROWS
FROM [cubSales];

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]

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

Clients growth over time MDX

I want to calculate the clients growth over the time.
So every day i have the total clients per state and per product subscription, and i can calculate the total for every day.
If i want to calculate the growth every day i don't have problems because i use a calculated member with
[Date].CurrentMember-[Date].PrevMember
This works pretty fine, but now i want to calculate the growth on month. So i have to sum all day growths of the month to calculate the month growth, right?
But my problem is that i'm too newbie to MDX and i can't find a way to produce that result (I want to know how many clients i have more or less over the year).
My intuition says that i need to sum all day's growth in the agregate date.
Could you help me?
If your date hierarchy has a month level above the date level (eg. Year-Month-Day), your cube will already have pre-processed this value. I would use ANCESTOR and LAG to get the data for a given day's month:
WITH MEMBER [Date].[YMD].[Current Month] AS
ANCESTOR(
[Date].[YMD].CurrentMember,
[Date].[YMD].[Month Level]
)
MEMBER [Date].[YMD].[Growth this month] AS
(
[Date].[YMD].[Current Month]
-
[Date].[YMD].[Current Month].LAG(1)
)
This will, however, only get the data from a whole-month period.
If what you're after is all the data between a particular day and the same day in the previous month, then PARALLELPERIOD is your go-to function (sidenote: not a goto statement). PARALLELPERIOD(Level, N, Member) will look at the position of Member amongst its siblings, then go to its ancestor at Level, go N members prior to that, and traverse back down to a member in the same relative position as Member.
In other words, it looks up your date in a prior month, year or whatev'.
WITH MEMBER [Date].[YMD].[One Month Ago Today] AS
PARALLELPERIOD(
[Date].[YMD].[Calendar Month],
1,
[Date].[YMD].CurrentMember
)
MEMBER [Date].[YMD].[All data since today last month] AS
(
/* The [Member]:[Member] syntax here is a range */
[Date].[YMD].[One Month Ago Today] : [Date].[YMD].CurrentMember
)
MEMBER [Date].[YMD].[Two Months Ago Today] AS
PARALLELPERIOD(
[Date].[YMD].[Calendar Month],
2,
[Date].[YMD].CurrentMember
)
MEMBER [Date].[YMD].[All data between today last month and today in the previous month] AS
(
[Date].[YMD].[Two Months Ago Today] : [Date].[YMD].[One Month Ago Today]
)
MEMBER [Date].[YMD].[Growth in the last month since the previous month] AS
(
[Date].[YMD].[All data between today last month and today in the previous month]
-
[Date].[YMD].[All data since today last month]
)
Hope this helps.
<3