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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
RubenIdx
Helper I
Helper I

Purchase price based on the date of the related line

Good day, I have a model with which I evaluate the cost of producing certain items I have the tables "Structures" that contains the detail of the production orders (produced units /inputs used) and the table "Purchasing" which contains the detail of the purchases of inputs (quantities / amounts / prices) and I require to obtain the price of the last purchase of each item according to the date of its production as shown in the images:

For example, the product AJON-011 for production of 03/12/2019 would take the price of 30/11/2019, for the production of 15/01/2020 the price corresponding to it is 14/01/2020 and so on with all products

I tried to use the following measure however I can't find the right logic to achieve the expected result.

Ult CompraFe = 
VAR __id = MAX (Compras[KeyProducto] )
VAR __date = CALCULATE ( MAX( Compras[Fecha] ), ALLSELECTED ( Compras ),  Compras[KeyProducto] = __id ) 
RETURN CALCULATE ( MAX ( Compras[Unit] ), VALUES ( Compras[KeyProducto] ), Compras[KeyProducto] = __id, Compras[Fecha] = __date )

I appreciate your help in advance.

image.pngimage.png

2 ACCEPTED SOLUTIONS

@RubenIdx somewhere your data t ype is not correct, check data type of all related columns used in this measure. I cannot tell from here where is the issue, but it is surely data type issue.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

Hi @RubenIdx ,

Please try to update the formula as below:

Ult CompraFe=
var kprod = CALCULATE(MAX('BDimensiones'[Kprod]), FILTER('BDimensiones','BDimensiones'[Prod]=SELECTEDVALUE ('DimEspejo'[Prod] )))
var Edate= SELECTEDVALUE('Estructuras'[Fecha])
var maxComDate = CALCULATE ( MAX ( 'Compras'[Fecha] ), Compras[KeyProducto] =kprod, 'Compras'[Fecha] <=Edate) 
RETURN 
CALCULATE ( MAX ( 'Compras'[Unit]), Compras[KeyProducto]= kprod, 'Compras'[Fecha] = maxComDate) 

Best Regards

Rena

Community Support Team _ Rena
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

8 REPLIES 8
parry2k
Super User
Super User

@RubenIdx can you paste the sample data instead of images.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@RubenIdx try this measure

 

Production Value = 
VAR __p = SELECTEDVALUE ( Production[Id] )
VAR __d = SELECTEDVALUE ( Production[Date] )
VAR __maxPurchaseDate = CALCULATE ( MAX ( Purchase[Date] ), Purchase[Id] = __p, Purchase[Date] <= __d ) 
RETURN 
CALCULATE ( MAX ( Purchase[Rate] ), Purchase[Id] = __p, Purchase[Date] = __maxPurchaseDate ) * SUM ( Production[Prod] )


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

I have tested the measurement and at the time of writing it does not mark me errors however when adding it to my visulation I send this message

Error Message:
MdxScript(Model) (8, 93) Calculation error in measure 'Structures'[Ult PurchaseFe]: DAX comparison operations do not support comparison values of type Text with values of type Date. Consider using the VALUE or FORMAT function to convert one of the values.

I have reviewed my tables and date columns if they are as date.

Hi @RubenIdx ,

Please try to update the formula as below:

Ult CompraFe=
var kprod = CALCULATE(MAX('BDimensiones'[Kprod]), FILTER('BDimensiones','BDimensiones'[Prod]=SELECTEDVALUE ('DimEspejo'[Prod] )))
var Edate= SELECTEDVALUE('Estructuras'[Fecha])
var maxComDate = CALCULATE ( MAX ( 'Compras'[Fecha] ), Compras[KeyProducto] =kprod, 'Compras'[Fecha] <=Edate) 
RETURN 
CALCULATE ( MAX ( 'Compras'[Unit]), Compras[KeyProducto]= kprod, 'Compras'[Fecha] = maxComDate) 

Best Regards

Rena

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

@RubenIdx somewhere your data t ype is not correct, check data type of all related columns used in this measure. I cannot tell from here where is the issue, but it is surely data type issue.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hello! I have modified the relationships of the tables and the measure you have provided me worked perfectly I share the model with the suggested measures already implemented.

https://1drv.ms/u/s!Aux4yKXbJDBchD_YJvKc8P-JOb2p?e=m5cavq

image.png

Hi @RubenIdx ,

If the problem has been resolved, could you please mark the helpful post as Answered? It will help other members in the community find the solution easily if they face the similar problem with you. Thank you.

Best Regards

Rena

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

Thank you! attached discharge link for the model.

https://1drv.ms/u/s!Aux4yKXbJDBchD5Ck4jQKVhSPFMq?e=0zqfN5

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.