Forum Discussion
YTD Dates Inconsistent with YTD from Previous Year
I have a variety of measures that leverage the DATESYTD, DATESQTD and DATESMTD expressions, but I find that the date ranges are slightly inconsistent when comparing to the previous year.
Example 1:
Example 2:
Hey Anonymous ,
I recommend reading this article, pay attention to the notes: https://dax.guide/sameperiodlastyear/
For this reaseon I had to change the DAX statement of the DateTable to this:
DateTable = CALENDAR( //MIN(Orders[Date]),MAX(Orders[Date]) DATE( 2015 , 1 , 1 ) , DATE( 2020 , 12 , 31 ) )
Then I created these two measures:Tom Current Year 2020-01-01 - 09-10 = var _lastdate = DATE( 2020 , 9 , 10 ) var _firstDayOfTheYear = DATE( YEAR(_lastdate) , 1 , 1 ) var theDates = DATESBETWEEN( 'DateTable'[Date] , _firstDayOfTheYear , _lastdate ) return CALCULATE( SUM('Orders'[Sales]) , FILTER( ALL('DateTable') , 'DateTable'[Date] in theDates ) )And the more interesting one:
Tom Previous Year 2019-01-01 - 09-10 = var _lastdate = DATE( 2020 , 9 , 10 ) var _firstDayOfTheYear = DATE( YEAR(_lastdate) , 1 , 1 ) var theDates = SAMEPERIODLASTYEAR(DATESBETWEEN( 'DateTable'[Date] , _firstDayOfTheYear , _lastdate )) var theStartDate = MINX( theDates , 'DateTable'[Date] ) var theEndDate = MAXX( theDates , 'DateTable'[Date]) var noofday = COUNTROWS( theDates ) var thevalue = CALCULATE( SUM('Orders'[Sales]) , FILTER( ALL('DateTable') , 'DateTable'[Date] in theDates ) ) return "startdate: " & theStartDate & UNICHAR(10) & "enddate: " & theEndDate & UNICHAR(10) & "no of days: " & noofday & UNICHAR(10) & "value: " & thevalueIt's simply more wordy as I also use this to document what's going on.
Nevertheless, all this allows me to create this:Hopefully, this is what you are looking for.
Regards,Tom
15 Replies
- TomMartens
Super User
Hey Anonymous ,
you might consider using the SAMEPERIODLASTYEAR, https://dax.guide/sameperiodlastyear/
instead of DATEADD(... , -365 , Day)
Regards,
Tom
- amitchandak
Super User
Anonymous , Can try TomMartens solution or check tehse
CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))
or
DATEADD(DATESYTD(DateTable[Date]),-1,Year)
or
Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))
for
CALCULATE(SUM(Sales[Sales Amount]),DATESQTD(dateadd('Date'[Date],-1,Year)))
CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-4,QUARTER))
- AnonymousNot applicable
Thanks for the comments guys. The issue is that when you use SAMEPERIODLASTLEAR OR DATEADD(-x,Yr/Qtr/Mos) it includes the entire month of September. So rather than Jan 1 - Sep 11 with 1 day being incorrect, those other approaches all give me Jan 1 - Sep 30.
- amitchandak
Super User
Anonymous , I tried checking like
Measure 2 = CALCULATE(MAX('Date'[Date]),DATESYTD(DATEADD('Date'[Date],-1,Year))) Measure 3 = CALCULATE(Min('Date'[Date]),DATESYTD(DATEADD('Date'[Date],-1,Year)))dates seem correct to me. The way I suggested in last update