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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
afaro
Helper III
Helper III

How to see the total for the same months last year?

I have a year slicer at the top from which specific year can be chosen. 
I also have a bar chart which has the month name on the x axis. Now, I want to see the total for every month but for the previous year to the year selected in the slicer.
 
I wrote the following DAX which is given me total sum for chosen year instead. Doesn't even filter the month. What is wrong in the logic here? 
1 total last year = CALCULATE
(sales[total], FILTER(ALLEXCEPT(datetable, datetable[months-name]), datetable[year] = MAX(datetable[year])-1))

I know I can write a more simple working solution like this: 
2 total last year = CALCULATE(sales[total], datetableyear[year] = MAX(datetable[year])-1))
But, for my understanding, I want to know why the 1  doesn't work? 
2 ACCEPTED SOLUTIONS
Joe_Barry
Super User
Super User

Hi @afaro 

 

Why not try the following few measures if you have a calander table in your data model,.

 

Base Sales Measure

Sales = SUM(Sales[Total])

 

YTD Sales

 

Sales - YTD = TOTALYTD([Sales], Calendar[Date])

 

PY Sales

Sales - PY =
CALCULATE([Sales - YTD], DATEADD(Calendar[Date], -1, YEAR))

 

Joe

 

If you found my answer helpful and it solved your issue, please accept as solution




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Learn about the Star Schema, it will solve many issues in Power BI!

Date tables help! Learn more



LinkedIn
Let's connect on LinkedIn


View solution in original post

Anonymous
Not applicable

Hi, @afaro 

The problem with the first formula is the use of functions. Retains filters for all columns except the specified columns. In this case, you keep the filter, but remove all other columns (including .which is why your formula isn't filtered by the previous year as expected.) Instead, it calculates the total sales for all years, but doesn't filter by month as you might expect.

The correct logic you're looking for is to calculate the total sales for the same month but the previous year relative to the year selected in the slicer. Your second formula is more in line with this goal

However, to understand why the first formula doesn't work as intended, it's crucial to grasp how things work in a function. In your scenario, you want to keep the month filter, but move to the previous year, which isn't effectively communicated in the first formula.

 

How to Get Your Question Answered Quickly 

Best Regards

Yongkang Hua

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

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi, @afaro 

The problem with the first formula is the use of functions. Retains filters for all columns except the specified columns. In this case, you keep the filter, but remove all other columns (including .which is why your formula isn't filtered by the previous year as expected.) Instead, it calculates the total sales for all years, but doesn't filter by month as you might expect.

The correct logic you're looking for is to calculate the total sales for the same month but the previous year relative to the year selected in the slicer. Your second formula is more in line with this goal

However, to understand why the first formula doesn't work as intended, it's crucial to grasp how things work in a function. In your scenario, you want to keep the month filter, but move to the previous year, which isn't effectively communicated in the first formula.

 

How to Get Your Question Answered Quickly 

Best Regards

Yongkang Hua

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

Joe_Barry
Super User
Super User

Hi @afaro 

 

Why not try the following few measures if you have a calander table in your data model,.

 

Base Sales Measure

Sales = SUM(Sales[Total])

 

YTD Sales

 

Sales - YTD = TOTALYTD([Sales], Calendar[Date])

 

PY Sales

Sales - PY =
CALCULATE([Sales - YTD], DATEADD(Calendar[Date], -1, YEAR))

 

Joe

 

If you found my answer helpful and it solved your issue, please accept as solution




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Learn about the Star Schema, it will solve many issues in Power BI!

Date tables help! Learn more



LinkedIn
Let's connect on LinkedIn


lbendlin
Super User
Super User

Have you considered using SAMEPERIODLASTYEAR instead?

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

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.