SSAS MDX: Time Calculation with a date range - ssas

In my cube, I have a Scope statement for the Previous Year Same Period [PY SAME Period].
([Time Calculations].[PY Same Period]=
Aggregate(
CrossJoin
(
{[Time Calculations].[Current Period]},
PARALLELPERIOD(
case [Date].[Date Hierarchy].CurrentMember.LEVEL_NUMBER
when 4 then [Date].[Date Hierarchy].[Date]
when 3 then [Date].[Date Hierarchy].[Month]
when 2 then [Date].[Date Hierarchy].[Quarter]
when 1 then[Date].[Date Hierarchy].[Year]
end,
case [Date].[Date Hierarchy].CurrentMember.LEVEL_NUMBER
when 4 then 364
when 3 then 12
when 2 then 4
when 1 then 1
else null
end , [Date].[Date Hierarchy].CurrentMember )
))
);
This works fine for one date but doesn't work for a range of dates, which includes Begin Date and End date.
[Date].[Date Hierarchy].[Date].&[2020-01-20T00:00:00] - This works
{[Date].[Date Hierarchy].[Date].&[2020-01-01T00:00:00]:[Date].[Date Hierarchy].[Date].&[2020-01-20T00:00:00]} - doesn't work
How do I get the scope statement to work for both scenarios?

Related

To calculate the average of Latest 12 weeks

I figured out how to calculate the average of latest 12 weeks volume in my data. But lets say the current year does not have 12 weeks, it only has 8 weeks then I want to take the remaining 4 weeks from the previous year to make it 12?
This is how my avg mdx query looks like:
Avg(
LastPeriods(
12,
Tail(
NonEmpty(
[Time].[Week].[Week].Members,
[Measures].[Vol ( Cases)]
),
1
).Item(0)
),
[Measures].[Vol ( Cases)]
)
I'm wondering if you could simplify and use the following within your measure?:
...
...
Tail(
NonEmpty(
[Time].[Week].[Week].Members,
[Measures].[Vol ( Cases)]
),
12
)
...
...

How to get avg from date range?

I have a Date dimenstion with levels: Year, Month, Day. I am need to get average by month for range like this [Date].[2011].[1].[10]:[Date].[2011].[10].[20]
Something along these lines:
AVG(
EXISTS(
[Date].[Month].MEMBERS
,[Date].[2011].[1].[10]:[Date].[2011].[10].[20]
)
)
Or if you want the daily average:
AVG(
[Date].[2011].[1].[10]:[Date].[2011].[10].[20]
)
Or is you want the average by the count of number of days for the range you specified:
DIVIDE(
SUM([Date].[2011].[1].[10]:[Date].[2011].[10].[20])
,EXISTS(
[Date].[Month].MEMBERS
,[Date].[2011].[1].[10]:[Date].[2011].[10].[20]
).count
)

MTD without using the MTD function

This is our current MTD calculation:
MEMBER [Time Calculations].[Time Calculations].[Calculate MTD] AS
Sum
(
MTD([Date].[Date - Calendar Month].CurrentMember)
,[Time Calculations].[Time Calculations].[Current Value]
)
I've seen somewhere along the way a MTD calculation that uses the range operator :. The following will be a sum from the start of time:
MEMBER [Time Calculations].[Time Calculations].[Calculate MTD] AS
Sum
(
null: [Date].[Date - Calendar Month].CurrentMember
,[Time Calculations].[Time Calculations].[Current Value]
)
Can anyone remember how to adjust the above so that it is a MTD calculation?
If [Date - Calendar Month] is similar to Calendar hierarchy of Adventure Works, following might work for you:
MEMBER [Time Calculations].[Time Calculations].[Calculate MTD] AS
Sum
(
[Date].[Date - Calendar Month].CurrentMember.firstsibling
:
[Date].[Date - Calendar Month].CurrentMember
,[Time Calculations].[Time Calculations].[Current Value]
)

SSAS Last Year MTD Calculation Irregular Calnedar

I have a sales calendar where the calendars for a month change year to year. The example is that September 2015 is defined as September 1 - September 30 but last year it ran from September 3 - September 30.
So when I want to compare this year MTD on 9/1/2015 should be compared to 9/3/2014. I have all the dates in the time dimension, like LAST MTD Start and LAST MTD End, and put them in a hierarchy, DATE with LAST MTD START and LAST MTD END, and tried ancestor functions, like
WITH
MEMBER [Measures].LMTD AS
SUM(
ANCESTOR(
[Time].[LYMD_START].CURRENTMEMBER
,1
)
,[Measures].[Volume]
)
SELECT
[Measures].LMTD on 0
FROM [myCUBE]
WHERE [Time].[DATE].&[2015-09-01T00:00:00];
But I only get back the current month's aggregated volume.
Assuming you have a dimension with date, sales month, and sales year, the following should work. If you filter to 2015-09-01 (which is the first day of the 9th sales month) the this will step back one sales year and then find the first day of the 9th sales month (2014-09-03):
ParallelPeriod(
[Date].[Sales Calendar].[Sales Year],
1,
[Date].[Sales Calendar].CurrentMember
)
Then you want to sum up the days in that sales month through 2014-09-03:
Sum(
PeriodsToDate(
[Date].[Sales Calendar].[Sales Month],
ParallelPeriod(
[Date].[Sales Calendar].[Sales Year],
1,
[Date].[Sales Calendar].CurrentMember
)
),
[Measures].[Volume]
)
So the hard work is in building the dimension with the right sales months. The MDX can just navigate that hierarchy.

MDX YTD Calculated Member for current and parallel period

Trying to build a calculated member using our date hierarchy for use in Tableau.
Hierarchy is as follows:
Fiscal Year
Fiscal Season
Fiscal Quarter
Fiscal Month
Fiscal Week
Date
To help our users with their analysis on dashboards, we're looking to build a Tableau set including the following members for them to toggle through:
Yesterday, Week to Date, Month to Date, Season to Date, and Year to Date.
We need to be able to show Sales $ and Sales $ LY(measure is defined as parallel period) together and be able to toggle through the different "to date" values. The this year portion is fine, but creating a calculated member that works appropriately for the LY measure too has been problematic. We need to be able to pivot between the periods for dashboards rather than create individual measures for each period TY and LY.
Here are the current definitions we have out there:
Yesterday: [DATE].[Time].defaultmember
WTD: [DATE].[Time].defaultmember.parent
MTD: [DATE].[Time].defaultmember.parent.parent
STD: [DATE].[Time].defaultmember.parent.parent.parent
YTD: [DATE].[Time].defaultmember.parent.parent.parent.parent
Any thoughts on how to modify these so that the LY measure reflects the same "to date" period rather than the full period last year?
Count the number of days completed this year and then use the Head function to get the same set of dates from the previous year - aggregate that set together.
Here is an illustration against the AdvWrks cube:
WITH
SET [DaysThisYear] AS
Descendants
(
[Date].[Calendar].[Calendar Year].&[2007]
,[Date].[Calendar].[Date]
)
MEMBER [Measures].[posOfCurrentDate] AS
Rank
(
[Date].[Calendar].CurrentMember
,Descendants
(
[Date].[Calendar].Parent.Parent.Parent.Parent
,[Date].[Calendar].[Date]
)
)
MEMBER [Measures].[prevEquivMTD] AS
Sum
(
Head
(
Descendants
(
[Date].[Calendar].Parent.Parent.Parent.Parent.PrevMember
,[Date].[Calendar].[Date]
)
,[Measures].[posOfCurrentDate]
)
,[Measures].[Internet Sales Amount]
)
MEMBER [Measures].[posCurrentYear] AS
[Date].[Calendar].Parent.Parent.Parent.Parent.Member_Caption
SELECT
{
[Measures].[posCurrentYear]
,[Measures].[posOfCurrentDate]
,[Measures].[Internet Sales Amount]
,[Measures].[prevEquivMTD]
} ON 0
,
[Date].[Calendar].[Date].&[20050111]
:
[Date].[Calendar].[Date].&[20080611] ON 1
FROM [Adventure Works];
This is the above as a single measure:
WITH
MEMBER [Measures].[Internet Sales Amount prevEquivMTD] AS
Sum
(
Head
(
Descendants
(
[Date].[Calendar].Parent.Parent.Parent.Parent.PrevMember //previous year
,[Date].[Calendar].[Date]
)
,Rank
(
[Date].[Calendar].CurrentMember
,Descendants
(
[Date].[Calendar].Parent.Parent.Parent.Parent //current year
,[Date].[Calendar].[Date]
)
)
)
,[Measures].[Internet Sales Amount]
)
SELECT
{
[Measures].[Internet Sales Amount]
,[Measures].[Internet Sales Amount prevEquivMTD]
} ON 0
,
[Date].[Calendar].[Date].&[20050111]
:
[Date].[Calendar].[Date].&[20080611] ON 1
FROM [Adventure Works];
Unfortunately it is tied to the measure Internet Sales Amount so not very generic. Generic calcs such as prev equiv mtd or Year on Year growth are usually added to a helper Time Caculation helper dimension that is independent from the Date dimension.