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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
aamoody81
Regular Visitor

SAMEPERIODLASTYEAR for only values in current year

Hi,

 

I'm trying to show the value from prior year for products. I only want the products that are present in the current year to show. 

 

 

What I would like:
Product SamePeriodSalesLastYear 1 $10 2 $12

What I am getting:
Product SamePeriodSalesLastYear
1 $10
2 $12
3 $8 --Product 3 has no sales in the current year

 

 

CALCULATE([Sum of ProductSales], SAMEPERIODLASTYEAR(D_DATE[DATE]))
OR
CALCULATE([Sum of ProductSales]), PARALLELPERIOD(D_DATE[DATE],-12,month))

 

Any help on this would be really appreciated. 

 

Thank you

 

 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

 

Try this

 

=IF([Sum of ProductSales]=0,BLANK(),CALCULATE([Sum of ProductSales], SAMEPERIODLASTYEAR(D_DATE[DATE])))

 

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

7 REPLIES 7
pdamac
New Member

Dear friends, unfortunately this function has no solution if there is an error. It is the worst function of Power BI, because it is necessary but only works in an unknown condition and does not work in others. It's a pity that this happens in an excellent solution like Power BI. There will be no solution. If it works, use it if you do not use any other function. That's the truth.

Ashish_Mathur
Super User
Super User

Hi,

 

Try this

 

=IF([Sum of ProductSales]=0,BLANK(),CALCULATE([Sum of ProductSales], SAMEPERIODLASTYEAR(D_DATE[DATE])))

 

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thank you! This removed the line I wanted removed!

 

=IF([Sum of ProductSales]=0,BLANK(),CALCULATE([Sum of ProductSales], SAMEPERIODLASTYEAR(D_DATE[DATE])))
AlbertoFerrari
Most Valuable Professional
Most Valuable Professional

Hi Aamoody,

 

If the formula works for you, then it's fine. Nevertheless, keep in mind that the code with IF is not computing what you asked at the beginning of the thread.

If you slice by product, then it is ok, every row will show sales in the previous year if there are sales in the current one. But on subtotals and at any grain over the product, the result might not be what you need.

If Products A and B belong to category Veggies, the code you are using will show the total of A and B in the last year, even if in the current one you sold only A.

The one with VALUES and CALCULATE shows only sales of A in the previous year, removing B because B has not been sold in the current year.

Might look like a pedantic difference, but it might be an important one.

 
Have fun with DAX!

Alberto Ferrari
http://www.sqlbi.com

Alberto Ferrari - SQLBI
AlbertoFerrari
Most Valuable Professional
Most Valuable Professional

This (or something similar, of course) should work:

 

=
CALCULATE ( 
    SUM ( Sales[Amount] ),
    SAMEPERIODLASTYEAR ( Date[Date] ),
    VALUES ( Sales[ProductKey] )
)

Basically, you move hte filter on the date, but you consolidate the filter over the products based on the current product keys.


Have fun with DAX!

Alberto Ferrari
http://www.sqlbi.com

Alberto Ferrari - SQLBI

I think this was really close. 

 

=
CALCULATE ( 
    SUM ( Sales[Amount] ),
    SAMEPERIODLASTYEAR ( Date[Date] ), 
Sales[ProductKey] IN VALUES ( Sales[ProductKey] ) )

 

I found this below. Unfortunatley for me the IN function isnt avaiable in my version. 

https://stackoverflow.com/questions/48301281/calculate-previous-year-based-on-this-year-condition 

 

Thank you for your respose. 

Greg_Deckler
Super User
Super User

Can't you just wrap that in an IF statement such that "if the sum of the sales in the current year is 0, then blank, otherwise, the calculation you mention"



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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