Forum Discussion
Get previous month / previous year using calculation groups
- 2 years ago
I would go with Field Parameters and independent measures.
What is the business problem you are trying to solve?
- Anonymous2 years agoNot applicable
Hi lbendlin , the client wants to compare the values generated from Slicer A from Slicer B.
I was able to create a Calculation group with for Slicer A with the following measures:Calculation Group for Slicer ACalculation Group for Slicer A// 7 DaysVAR __Isdatesfiltered =CALCULATE ( ISFILTERED ( 'dim_date'[date] ), ALLSELECTED () )VAR __date =CALCULATE (MAX ( 'fact_table'[Date] ), REMOVEFILTERS () )VAR __Result =IF ( __Isdatesfiltered,SELECTEDMEASURE (), CALCULATE ( SELECTEDMEASURE (),
KEEPFILTERS (DATESINPERIOD ( 'dim_date'[date], __date - 1, -7, DAY ) ) ) )RETURN__Result// Last MonthVAR __Isdatesfiltered =CALCULATE ( ISFILTERED ( 'dim_date'[date] ), ALLSELECTED () )VAR _end = CALCULATE ( MAX ( 'fact_table'[Prev Month End Date] ), REMOVEFILTERS () )var _start = CALCULATE ( MAX ( 'fact_table'[Prev Month Start Date] ), REMOVEFILTERS () )VAR __Days = 7VAR __Result =IF ( __Isdatesfiltered, SELECTEDMEASURE (), CALCULATE ( SELECTEDMEASURE (),KEEPFILTERS ( FILTER ( ALL ( 'dim_date' ), 'dim_date'[date] >= _start && 'dim_date'[date] <= _end) )))RETURN__Result// mtdVAR __Isdatesfiltered = CALCULATE ( ISFILTERED ( 'dim_date'[date] ), ALLSELECTED () )VAR _todaymonthstart = EOMONTH ( TODAY (), -1 ) + 1VAR _todaymonthend = CALCULATE ( MAX ( 'fact_table'[Date] ), REMOVEFILTERS ())
VAR __Result =CALCULATE (selectedmeasure(), KEEPFILTERS( FILTER ( ALL ('al_pres_dimad dim_date' ),'al_pres_dimad dim_date'[date] >= _todaymonthstart
&& 'al_pres_dimad dim_date'[date] <= _todaymonthend - 1 )))RETURN__Result// fy to dateVAR _start = MAXX ( ALLSELECTED ( 'fact_table' ), 'fact_table'[FY Start Date] )VAR _end =CALCULATE ( MAX ( 'fact_table'[Date] ), REMOVEFILTERS () )VAR __Result =CALCULATE ( SELECTEDMEASURE (), KEEPFILTERS (FILTER ( ALL ( 'dim_date' ), 'dim_date'[date] >= _start && 'dim_date'[date] <= _end - 1) ) )RETURN__Result
and this is the measure I created for Slicer B//for Primary vs Month on Month calculations
var _last7DaysMoM = CALCULATE([Actual],DATEADD('dim_date'[date],-1,MONTH))
var _mtdMoM = CALCULATE([Actual], DATEADD('dim_date'[date],-1,MONTH))
var _lastMonthMoM = CALCULATE([Actual], PARALLELPERIOD('dim_date'[date],-1,MONTH))
var _ytdMoM = CALCULATE( [Actual], DATEADD('dim_date'[date],-1,MONTH))//for Primary vs Year on Year calculations
var _last7DaysYoY = CALCULATE([Actual], DATEADD('dim_date'[date],-1,YEAR))
var _mtdYoY = CALCULATE( [Actual ],DATEADD('dim_date'[date],-1,YEAR))
var _lastMonthYoY = CALCULATE( [Actual],PARALLELPERIOD('dim_date'[date],-1,YEAR))
var _ytdYoY = CALCULATE([Actual], DATEADD('dim_date'[date],-1,YEAR))
For the Slicer B, I was able to make it work for the Last 7 Days and FYTD and got the desired results. However, I am having issues with MTD and Last Month since it's returning blank values for the Slicer B.
My follow up question is, should I adjust the calculation I have in Slicer A for the MTD and Last Month? or should I change the DATEADD dax function I used for the Slicer B?- lbendlin2 years ago
Super User
I would go with Field Parameters and independent measures.
- Anonymous2 years agoNot applicable
Thank you! This works for my report.