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
Anonymous
Not applicable

Find purchase price based on sales date (first price before date)

Good morning,

I’ve been struggling for one DAX solution I can’t figure out how to write it. 

 

I’m trying to find the latest purchase price based on the salesdate so I can calculate the Margin of a product on a given date.

I'm getting close with:

Purchase price calculated = calculate (
    MAX(Purchase_Table[Purchase_Price]),
    filter ( ALLEXCEPT(Purchase_Table , Purchase_Table[Product]),
    Purchase_Table[Date] <  min ( Sales_Table[Date])
    && Purchase_Table[Date] = LASTDATE(Purchase_Table[Date])))
 
I know that the last filter is incorrect, but I'm trying to get the last date in the selection. It now gives me the following result:
 
Power_BI_Dev9_0-1692613848663.png

 

As you see, the Purchase_Price_Calculated on 31-jan should be 49, however it's showing 56. It's not giving the last date in the filter. 
I'm missing the final piece in this puzzle.

 

Any help will be appreciated.

Michel

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I was able to solve it myself with the following code:

 

var maxdate = CALCULATE(LASTDATE(Purchase_Table[Date]), FILTER(ALLEXCEPT(Purchase_Table, Product_table[Product]),
Purchase_Table[Date] < MIN(Sales_Table[Date])))
var Purchaseprice =
calculate (
    MAX(Purchase_Table[Purchase_Price]),
    filter ( ALLEXCEPT(Purchase_Table , Purchase_Table[Product]),
    Purchase_Table[Date] <  min ( Sales_Table[Date])
    && Purchase_Table[Date] =  maxdate))

    return Purchaseprice

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

I was able to solve it myself with the following code:

 

var maxdate = CALCULATE(LASTDATE(Purchase_Table[Date]), FILTER(ALLEXCEPT(Purchase_Table, Product_table[Product]),
Purchase_Table[Date] < MIN(Sales_Table[Date])))
var Purchaseprice =
calculate (
    MAX(Purchase_Table[Purchase_Price]),
    filter ( ALLEXCEPT(Purchase_Table , Purchase_Table[Product]),
    Purchase_Table[Date] <  min ( Sales_Table[Date])
    && Purchase_Table[Date] =  maxdate))

    return Purchaseprice
Semaj06
Regular Visitor

Purchase price calculated = 
var maxdate=selectedvalue(Sales_Table[Date])
var searchproduct=selectedvalue(Sales_Table[Product])
Return
calculate (
    MAX(Purchase_Table[Purchase_Price]),
     Purchase_Table[Product]=searchproduct&&
    Purchase_Table[Date] <  maxdate)
Semaj06
Regular Visitor

HI

 

Try this

Purchase price calculated = 
var maxdate=selectedvalue(Sales_Table[Date])
Return
calculate (
    MAX(Purchase_Table[Purchase_Price]),
    filter ( ALLEXCEPT(Purchase_Table , Purchase_Table[Product]),
    Purchase_Table[Date] <  maxdate)
Anonymous
Not applicable

Hi Semja,

 

Unfortunatly both solutions give the same wrong result.

 

Regards

Michel

 

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.