Forum Discussion

Powereports's avatar
Powereports
Helper I
5 years ago
Solved

MTD and YTD calculation

@ Power BI users,

 

When I try to use SAMEPERIODLASTYEAR to get MTD of previous year, it returns entire months billed hours sum, rather I was looking for only till current year date sum as in screenshot below. Also when i try to use Dateadd function Billed Hours previous Year, it gives me the entire year total

Example- I want to view this year MTD ie from Jan 1-Jan 19, 2021 Vs Jan1-Jan 19, 2020 (Screenshot below). Currently i am getting the entire month total highlighted in black but i want to get total till Jan 19, 2020 highlighted in blue.

 

Following are the measures i used

 

Billed Hours current Year = Sum(FACT_Monthly_Sales_By_business_Line[Duration Billed])
Billed Hours Last_Year_MTD = CALCULATE ( [Billed Hours], SAMEPERIODLASTYEAR(DATESMTD( 'Calendar'[Date])))
Billed Hours Last_Year_YTD  =  CALCULATE([Billed Hours], DATEADD('Calendar'[Date], -1, YEAR))
 
 
I tried using this link too but was unable to get the expected result.
  • FarhanAhmed's avatar
    FarhanAhmed
    5 years ago
    Billed Hours Last Year MTD = 
    var lastNonEmtpyDate = LASTNONBLANK(ALL(Calendar[Date]),[Billed Hours])
    return
    IF(HASONEVALUE('Calendar'[Date]),
    CALCULATE ( [Billed Hours], SAMEPERIODLASTYEAR(DATESMTD( (Calendar[Date]))),
    FILTER( ALL(Calendar[Date]), Calendar[Date]<=MAX( 'Billed Hours MTD dax'[Date]))
    ),
    CALCULATE([Billed Hours], DATEADD( FILTER(DATESMTD((Calendar[Date])),Calendar[Date]<= lastNonEmtpyDate ), -1, YEAR )))

13 Replies

  • If I understand your issue correctly you want the calculation to stop at the last date that has data. So something like the following might do the trick:

    Billed Hours Last Year MTD =
    var lastNonEmtpyDate = LASTNONBLANK(ALL(Calendar[Date]),[Bill Hours])
    return CALCULATE( [Billed Hours], DATEADD( FILTER(DATESMTD(Calendar[Date]), Calendar[Date] <= lastNonEmtpyDate ), -1, YEAR ))
  • You are nearly there. In addition to SAMEPERIODLASTYEAR you also need a filter from your dates table that includes all dates that are older than or equal to "Today minus one year". Depending on your dataset refresh frequency the easiest way to implement that is as a calculated column that gets updated during refresh.

  • FarhanAhmed's avatar
    FarhanAhmed
    Community Champion

    You need to add a filter statement at the end of your measure to limit dates

     

    FILTER(ALL( 'Calendar'[Date]), 'Calendar'[Date]<=MAX( FACT_Monthly_Sales_By_business_Line[Date]))

     

    your meausre will be look like this.

     

    Billed Hours Last_Year_MTD = CALCULATE ( [Billed Hours], SAMEPERIODLASTYEAR(DATESMTD( 'Calendar'[Date])),FILTER(ALL( 'Calendar'[Date]), 'Calendar'[Date]<=MAX( FACT_Monthly_Sales_By_business_Line[Date]))
    )

     

    • Powereports's avatar
      Powereports
      Helper I

      Hi FarhanAhmed,

      The measure did solve my problem to some extent but not completely. If you refere the screenshot below , you can see that though the measure restricts it to maxdate but the total for the previous year still shows the total for the entire month (721961) and not the current date for last year (426089). Screenshot below for reference.

      • FarhanAhmed's avatar
        FarhanAhmed
        Community Champion

        I think the solution suggest by d_gosbell  will work and you may want to do some tweaking by adding HASONEVALUE in the code to not return values for future dates.

         

        You may give it a try as well.

         

        Billed Hours Last Year MTD = 
        var lastNonEmtpyDate = LASTNONBLANK(ALL(ALL(Calendar[Date]),[Bill Hours])
        return 
        IF(HASONEVALUE(ALL(Calendar[Date]),
        
        CALCULATE ( [Bill Hours], SAMEPERIODLASTYEAR(DATESMTD( ALL(Calendar[Date])),FILTER(ALL( ALL(Calendar[Date]), ALL(Calendar[Date]<=MAX( Query1[Date]))
        )
        ,
        CALCULATE( [Bill Hours], DATEADD( FILTER(DATESMTD(ALL(Calendar[Date]),ALL(Calendar[Date] <= lastNonEmtpyDate ), -1, YEAR )))