Forum Discussion

PBSuper's avatar
PBSuper
Frequent Visitor
9 years ago

MTD Previous

I am using SAMEPERIODLASTYEAR for my MTD Last Year calculation, however it returns Sales for the entire month of the previous year. So if I am reporting MTD sales for 17-08-2017, the MTD Last Year I was expecting to be is for sales between 1-08-2016 to 17-08-2016. Whereas my dax is returning sales for 1-08-2016 to 31-08-2016.

 

My DAX is: MTD Sales (LY) = CALCULATE([MTD Sales],SAMEPERIODLASTYEAR(CALENDAR[CalendarKey]))

 

I also tried: PYMTD = totalmtd(sum(SALES[Sale Amount]),dateadd(FILTER(DATESMTD(CALENDAR[CalendarKey]),CALENDAR[CalendarKey]<TODAY()),-1,YEAR))

 

But this gives me sales up until the 16-08-2016, and skips sales for 17-08-2016.

 

I have seen many forums with this issue but I can't seem to get this going. Can someone please help?

 

Thanks

7 Replies

  • Two things.

     

    First, from a DAX perspective, I believe the formula you want is DATEADD. DateAdd will return partial periods whereas other functions return whole periods. Check this blog post for a good explanation: DateAdd

     

    Second, a best practice I always use is to have a "trimmed" dates table, meaning I don't have dates that go beyond where I have data in my fact table. For example, I do a lot of month end reporting, so I always trim my dates table to stop at the last day of the prior month. The great thing about Power Query/M Language is that you can code this to basically be automatic.

     

    So for your example, you would start by hardcoding that you want the date to be less than or equal to today's date. Once you do this, it will add a step to the query and you will see the M formula in the formula bar showing you what it's doing. Then, you can erase the hardcoded date in that formula bar and replace it with:

     

    <=Date.From(DateTime.LocalNow())

     

    This way, every time you refresh the data, your calendar table will always stop at todays date. There's a ton of date functions in M, so check out the Formula reference if there's other manipulations you need to make.

    • PBSuper's avatar
      PBSuper
      Frequent Visitor

      I have already trimmed my calendar dimension in my tabular to not include any future dates. My fact is not going to have future sales so I probably don't have to worry about that. Doing this has not fixed my problem.

      • TomMartens's avatar
        TomMartens
        Super User

        Hey,

         

        to answer similar questions I always use the following approach, maybe it seems a little complex, but I appreciate the degree of freedom it provides, the following measures uses the same number of dates in the previous month, for reasons of simplicity I skipped the treatment being on March 30 or March 31

         

        SameNumberOfPeriodsPrevMonth = 
        var currentDay = DAY(MAXX(VALUES('Calendar'[Date]),'Calendar'[Date]))
        var currentMonth = MONTH(MAXX(VALUES('Calendar'[Date]),'Calendar'[Date]))
        var currentYear = YEAR(MAXX(VALUES('Calendar'[Date]),'Calendar'[Date]))
        var startday = 1
        var endday = currentDay
        var startmonth = if(currentMonth = 1, 12, currentMonth-1)
        var endmonth = if(currentMonth = 1, 12, currentMonth-1)
        var startyear = if(currentMonth = 1, currentYear - 1, currentYear)
        var endyear = if(currentMonth = 1, currentYear - 1, currentYear)
        var startdate = DATE(startyear, startmonth, startday)
        var endDate = DATE(endyear, endmonth, endday)
        return
        IF(NOT(ISBLANK(CALCULATE(SUM('FactWithDates'[Amount]))))
        ,CALCULATE(
        	SUM('FactWithDates'[Amount])
        	,DATESBETWEEN('Calendar'[Date], startdate, endDate)
        )
        ,BLANK()
        )

        The measure creates this output

         

        Hope this helps

         

        Regards

  • v-jiascu-msft's avatar
    v-jiascu-msft
    Microsoft Employee

    Hi PBSuper,

     

    Could you please mark the proper answer as solution or share the solution if it's convenient for you? That will be a big help to the others.

     

    Best Regards!
    Dale

    • PBSuper's avatar
      PBSuper
      Frequent Visitor

      After trying various ideas to no avail, I had to resort to splitting my last year sales from this year sales in my tabular model. This seems to be working fine.

       

      Thank you all for your help again.