Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The 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.

Reply
rudivs
Frequent Visitor

Last date of SAMEPERIODLASTYEAR() seems to be wrong

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?

 

1 ACCEPTED SOLUTION

@keajht31

 

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

 

View solution in original post

2 REPLIES 2
keajht31
Frequent Visitor

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?

@keajht31

 

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

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

Find out what's new and trending in the Fabric community.