Microsoft Fabric Community Conference 2025, March 31 - April 2, Las Vegas, Nevada. Use code FABINSIDER for a $400 discount.
Register nowThe Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.
I am trying to use SAMEPERIODLASTYEAR(Dates[Date]) to calculate the comparative nett financial position at the same time the previous year. So I have a measure `Nett Position = [Total Revenue] - [Total Expenses]`, and `Nett Position LY = CALCULATE([Nett Position], SAMEPERIODLASTYEAR(Dates[Date]))`.
The problem is that it seems to calculate to the end of the month, not to the same day last year. So LASTDATE(Dates[Date)) gives me 2018/11/07, but LASTDATE(SAMEPERIODLASTYEAR(Dates[Date])) gives me 2017/11/30, which is not the same period.
Any suggestions for how I can do a time comparison that does not include the extra days to the end of the month?
Solved! Go to Solution.
I think I figured it out from your suggestion to use datesbetween. If you use SAMEPERIODLASTYEAR(LASTDATE(Dates[Date])) you will get the actual equivalent date. So combining that with DATESBETWEEN, you can get the correct period without the issues with year end/start:
Nett Position LY = CALCULATE(
[Nett Position],
DATESBETWEEN(
Dates[Date],
SAMEPERIODLASTYEAR(FIRSTDATE(Dates[Date])),
SAMEPERIODLASTYEAR(LASTDATE(Dates[Date]))
)
)
I was facing similar question before and found no easy solution at all. Those DAX Time intelligence function is a joke.
Using SAMESPERIODLASTYEAR or DATESMTD/TOTALMTD combine with DATEADD will include the end of the month. A solution would be to use datebetween function with date(year(today()) - 1, month(today), day(today)). But it is not a clear solution since you need to cater the year end/start scenario where it give negative/zero. And of course the DATEADD function cannot be used on a date value so you cannot use it here. You could try add the coding to check the negative/zero value but it is just so silly that some simple thing easily doable in SQL has to be more complicate in Power BI. Not so POWER I guess?
I think I figured it out from your suggestion to use datesbetween. If you use SAMEPERIODLASTYEAR(LASTDATE(Dates[Date])) you will get the actual equivalent date. So combining that with DATESBETWEEN, you can get the correct period without the issues with year end/start:
Nett Position LY = CALCULATE(
[Nett Position],
DATESBETWEEN(
Dates[Date],
SAMEPERIODLASTYEAR(FIRSTDATE(Dates[Date])),
SAMEPERIODLASTYEAR(LASTDATE(Dates[Date]))
)
)
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Check out the February 2025 Power BI update to learn about new features.
User | Count |
---|---|
86 | |
80 | |
53 | |
39 | |
39 |
User | Count |
---|---|
104 | |
85 | |
47 | |
44 | |
43 |