Forum Discussion
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 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
- d_gosbellSuper User
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 )) - lbendlinSuper User
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.
- FarhanAhmedCommunity 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]))
)- PowereportsHelper 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.
- FarhanAhmedCommunity 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 )))