Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

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:

YTD = DATESYTD(DateTable[Date])
Returns dates between Jan 1 - Sep 10
 
YTD LY = DATEADD(DATESYTD(DateTable[Date]),-365,DAY)
Returns dates between Jan 1 - Sep 11 
 
In this example, I cant figure out how to align the dates, if I change the number of days, it just shift the error a different direction. Is there something I can do to get the dates to match?  

 

Example 2:

QTD = DATESQTD(DateTable[Date])
Returns dates between Jul 1 - Sep 10
 
QTD LY = DATEADD(DATESQTD(DateTable[Date]),-365,DAY)
Returns dates between Jul 2 - Sep 11
 
In this example however, I can align the dates by simply changing the number of days to -366
  • 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: " & thevalue 

    It'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

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

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      amitchandak TomMartens 

       

      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's avatar
        amitchandak
        Icon for Super User rankSuper 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