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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Power_BI_Dev9
Regular Visitor

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
Power_BI_Dev9
Regular Visitor

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
Power_BI_Dev9
Regular Visitor

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)

Hi Semja,

 

Unfortunatly both solutions give the same wrong result.

 

Regards

Michel

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors