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

View all the Fabric Data Days sessions on demand. View schedule

Reply
naelske_cronos
Advocate II
Advocate II

SAMEPERIODLASTYEAR - wrong result

Hello,

 

I know that when you use the function SAMEPERIODLASTYEAR(), you can compare the current period like in my picture with the sameperiod but for last year on the same row. 

Capture.PNG

This is my DAX measure to calculate the rolling months ending with the current date. I also work with a cumulative based on what the user clicks if he/she wants to calculate with or without cumulative.Capture1.PNG

This is the same measure but for the sameperiod but for last year. In my table above you see that it shows for the same period for last year but not on the same line.

Capture2.PNG

How am I getting this on the same line?

 

Kind regards

1 ACCEPTED SOLUTION
v-eachen-msft
Community Support
Community Support

Hi, @naelske_cronos 


I tried to understand what you mean and provide a solution. I used DATEADD() function to get the value of the same period last year.

 

Create a column to convert Year and Month to a date type value.

 

dateFormat =
test[Month] & "-" & test[Year]

 

After creating it, select the data type option to choose DATE type

 

Then edit relationship between your calendar table and your data table.

 

case07311.PNG

 

Then create the following measure:

rolling 12 Month PY =
VAR rollingMonths =
    CALCULATE ( SUM ( test[Sales] ), DATEADD ( 'Calendar'[Date], -12, MONTH ) )
RETURN
    rollingMonths

In the report, you need to choose these fields:1.png

 

3.PNG

In the Date fields, you just need Year and Month.2.png

Now, you can get the visual you want.case0731.PNG

Best Regards,

Eads

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.

View solution in original post

8 REPLIES 8
v-eachen-msft
Community Support
Community Support

Hi, @naelske_cronos 


I tried to understand what you mean and provide a solution. I used DATEADD() function to get the value of the same period last year.

 

Create a column to convert Year and Month to a date type value.

 

dateFormat =
test[Month] & "-" & test[Year]

 

After creating it, select the data type option to choose DATE type

 

Then edit relationship between your calendar table and your data table.

 

case07311.PNG

 

Then create the following measure:

rolling 12 Month PY =
VAR rollingMonths =
    CALCULATE ( SUM ( test[Sales] ), DATEADD ( 'Calendar'[Date], -12, MONTH ) )
RETURN
    rollingMonths

In the report, you need to choose these fields:1.png

 

3.PNG

In the Date fields, you just need Year and Month.2.png

Now, you can get the visual you want.case0731.PNG

Best Regards,

Eads

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.
Anonymous
Not applicable

Thank you so much!

I work with a fiscal calendar so date functions don't work well.

I was using SamePeriodLastYear(dateadd(date,1,day)) and it was giving me the correct daily results but wouldn't total correctly.

This solution works!  THANK YOU!!!

MattAllington
Community Champion
Community Champion

Your currentDate Variable is not correct. You should use =MAX(Calendar[date])

 

the objective is to find the last date for each row in the table, with the current table filters applied. You are taking today’s date - which is  a completely different thing



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

Did you try my suggestion?



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

@MattAllington 

 

I did try the solution but I don't know how it helps me to get the measure 12 Rolling Months PY for the SAMEPERIODLASTYEAR on the same line as 12 Rolling Months? The first measure works because it shows non-cumulative as cumulative so that's no problem.

 

Kind regards

Here is a generic formula for 12 months rolling total

 

=
VAR myLastDate =
MAX ( calendar[date] )
RETURN
CALCULATE (
[total sales],
DATESINPERIOD ( calendar[date], myLastDate, -1, YEAR )
)

 



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

Hello @MattAllington 

 

I guess you understand me completely wrong or it is just me whom asked the question wrong. There's nothing wrong about my first DAX measure (M) 12 Rolling Months. CurrentDate is 30th of July 2019. The VAR AllDates is 30th of July 2019 minus 12 months so 31th of July 2018. This is right. Even cumulative is right.

 

I want to create another measure based on this measure but for the sameperiod for last year so instead of 31th of July 2018 till 30th of July 2019, I want to calculate from 31th of July 2017 till 30th of July 2018 with SAMEPERIODLASTYEAR.

 

As you can see in the table in the first image, it shows the almost right result but I know with SAMEPERIODLASTYEAR it shows the same period for last year on the same row. For example you see for july 2018 the sum 88.359,20 euros but on the same line next to it, it has to show 172.740,07 and that is the sum from july 2017.

 

 

Kind regards

 

Yes, you are right - I don’t understand what you are doing. Maybe partly because I can’t read the headings in your table. 

 

First column is year, second is month. I assumed the third is something additive, like sales. The 4th column says it is a 12 month rolling total, but that is not what it is showing. It is showing the same number as column 3. Maybe this is what you want - I don’t know. But it is not a rolling 12 month total if it shows the total for the current month for each row of the table. A rolling 12 month total will show the previous 12 month total regardless of what time period you select - that’s what the “rolling” part means. 

 

If you want the last 12 months sales, then that’s is something different. 



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors