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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
RubenIdx
Helper I
Helper I

Precio compra en función de la fecha del renglón relacionado

Buen día, tengo un modelo con el cual evaluo el costo de producir ciertos articulos tengo las tablas "Estructuras" que contiene el detalle de las ordenes de producción (Unidades producidas/insumos utilizados)  y la tabla "Compras" la cual contiene el detalle de las compras de insumos  (cantidades / importes / precios) y requiero obtener el precio de la ultima compra de cada elemento de acuerdo a la fecha de su producción como lo muestro en las imágenes:

Por ejemplo del producto AJON-011 para la producción del 03/12/2019 se tomaría el precio del 30/11/2019, para la producción del 15/01/2020 el precio que le corresponde es el del 14/01/2020 y asi sucesivamente con todos los productos

 

Intenté utilizar la siguiente medida sin embargo no puedo encontrar la lógica correcta para lograr el resultado esperado.

 

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 )

 

Agradezco de antemano su ayuda.

image.pngimage.png

3 ACCEPTED SOLUTIONS

@RubenIdx en algún lugar los datos no son correctos, compruebe el tipo de datos de todas las columnas relacionadas utilizadas en esta medida. No puedo decir desde aquí dónde está el problema, pero sin duda es un problema de tipo de datos.



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

Hola @RubenIdx ,

Por favor, intente actualizar la fórmula como se muestra a continuación:

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) 

Saludos

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

Hola ! He modificado las relaciones de las tablas y la medida que me ha proporcionado funcionó a la perfección les comparto el modelo con las medidas sugeridas ya implementadas.

 

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

 

image.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

View solution in original post

9 REPLIES 9
parry2k
Super User
Super User

@RubenIdx puede pegar los datos de muestra en lugar de las imágenes.



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 probar esta medida

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.

He probado la medida y al momento de escribirla no me marca errores sin embargo al agregarla a mi visulización me envia este mensaje

 

Error Message:
MdxScript(Model) (8, 93) Error de cálculo en la medida 'Estructuras'[Ult CompraFe]: Las operaciones de comparación DAX no admiten valores de comparación de tipo Text con valores de tipo Date. Considere la posibilidad de usar la función VALUE o FORMAT para convertir uno de los valores.

 

 

He revisado mis tablas y las columnas de fecha si están como fecha.

Hola @RubenIdx ,

Por favor, intente actualizar la fórmula como se muestra a continuación:

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) 

Saludos

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 en algún lugar los datos no son correctos, compruebe el tipo de datos de todas las columnas relacionadas utilizadas en esta medida. No puedo decir desde aquí dónde está el problema, pero sin duda es un problema de tipo de datos.



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.

Hola ! He modificado las relaciones de las tablas y la medida que me ha proporcionado funcionó a la perfección les comparto el modelo con las medidas sugeridas ya implementadas.

 

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

 

image.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Hola @RubenIdx ,

Si el problema se ha resuelto, ¿podría marcar el post útil como Respondido? Ayudará a otros miembros de la comunidad a encontrar la solución fácilmente si se enfrentan al problema similar con usted. Gracias.

Saludos

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.

Gracias! anexo vinculo de descarga para el modelo.

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

@RubenIdx probar esta medida, puede ajustarla según sus necesidades. En un escenario como este tener dimensión de producto que enlace tanto a la compra como al precio va a ser el mejor diseño.

Production Value = 
VAR __p = SELECTEDVALUE ( Production[Id] )
VAR __d = SELECTEDVALUE ( Production[Date] )
RETURN 
CALCULATE ( MAX ( Purchase[Rate] ), Purchase[Id] = __p, Purchase[Date] <= __d ) * 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.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors