MDX YTD Calculated Member for current and parallel period - mdx

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.

Related

Dynamic performance % dax formula

Hope you all are doing well and safe!!
I have a question regarding Power BI dax. I want to create a dax measure for performance percentage in which numerator and denominator will be as per user filter.
Case 1: I want to create a dax measure for performance % (Current Month Sales / Dec'20 Sales) in which current month value will be depending upon user drop down filter and denominator will be fixed with Dec'20 sales value.
Case 2: I want to create a dax measure for performance % (Current Month Sales / Previous Month Sales) in which current month value will be depending upon user drop down filter and denominator will be as per previous month depending upon month selection done by user.
Thanks in advance :)
Try these measures
Case 1 =
DIVIDE (
[Measure],
CALCULATE (
[Measure],
Dates[Calendar Year Number] = 2020,
Dates[Month Number] = 12,
REMOVEFILTERS ( Dates )
)
)
Case 2 =
VAR SelectedMonth =
CALCULATE ( MAX ( Dates[Calendar Year Month Number] ), ALLSELECTED ( Dates ) )
VAR PreviousMonth =
CALCULATE (
MAX ( Dates[Calendar Year Month Number] ),
Dates[Calendar Year Month Number] < SelectedMonth,
REMOVEFILTERS ( Dates )
)
VAR Result =
DIVIDE (
[Measure],
CALCULATE (
[Measure],
Dates[Calendar Year Month Number] = PreviousMonth,
REMOVEFILTERS ( Dates )
)
)
RETURN
Result

Difference between two measure dax

I have a measure that calculates previous recorded sales now I would like to work out the difference between period sales against previous period sales, I tried simple subtraction but I get error message..
Any suggestions please..
Thanks
Sales Change:= sales[sales]-Previous Day Sales
Previous Day Sales :=
CALCULATE (
SUM ( Sales[Sales] ),
FILTER (
ALL ( Sales ),
Sales[Date]
= CALCULATE (
MAX ( Sales[Date] ),
FILTER (
ALL ( Sales ),
COUNTROWS ( FILTER ( Sales, EARLIER ( Sales[Date] ) < Sales[Date] ) )
)
)
)
)
If you are creating a measure you have to aggregate the Sales[Sales] using the SUM() function.
Sales Change := SUM(Sales[Sales])-[Previous Day Sales]
While creating measures you cannot refer to column values without an aggregation. Measures run in multiple contexts and the Sales[sales] column value cannot be calculated in a different context than the row context.

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.