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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Get a corresponding price from another table using relationship through item table

Hi!

 

I have this pretty complex problem (at least for me) that I can't seem to solve by myself. So basically I have this table called "Latest purchase price", which has information about every items latest purchased. I also have an item table and then I have a trade agreements table that contains information about every item's trade agreements for different purchase quantities, thus the relationship between that table and item table is many-to-one. I'll post a picture here about the data model if that helps.  

taikuri_0-1669114017401.png

I want to compare latest purchase price to the price that we have in trade agreements thus I need to get the corresponding value from the trade agreements table for the latest purchase. I'll post a mockup data here also so that you can try it yourself. 

 

I have tried to do this using summarize columns and sumx. The logic is that once I have the virtual table with trade agreements for given item, I can use filter condition to filter it only contain values where purchased qty is equal or greater than fromqty in trade agreements table. Still it is ambiguous so using minx I want to just get one value which should be correct. But as you can see from picture bellow my dax query is not working...

taikuri_1-1669120493815.png

 

Trade agreement price = 
 var latest_qty = MAXX('Latest Purch Prices', 'Latest Purch Prices'[OrderedQty])
 var trade_agreement_qty = 
 FILTER(SUMMARIZECOLUMNS(
     vDimItem[ItemID], 
    TradeAgreements[AMOUNT], 
    TradeAgreements[QUANTITYAMOUNTFROM]),
    TradeAgreements[QUANTITYAMOUNTFROM] <= latest_qty)
 
 return
MINX(trade_agreement_qty, TradeAgreements[QUANTITYAMOUNTFROM])
Latest Purch Price Table:

ID	ITEM	PRICE	ORDEREDQTY
1	1234	1	1
2	1235	2	10
3	1236	11	3
vDimItem:

ID	ITEMID
1	1234
2	1235
3	1236
TradeAgreements:

ITEMRELATION	FROMQTY	AMOUNT
1234	0	1
1235	0	5
1235	10	2
1236	0	10
1236	5	5
1236	10	2
Goal/Result:

ID	ITEM	PRICE	ORDEREDQTY	TRADEAGREEMENT PRICE	Difference
1	1234	1	1	1	0
2	1235	2	10	2	0
3	1236	11	3	2	-1

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Sorry but that does not help since there is one to many relationship there so multiple values would be returned. But actually, I now managed to solve this problem and can share the resolution here if somebody else comes up with a similar problem. 

 

So I basically created a new measure that is the latest purchase qty and saved that to a variable. Then I calculate the corresponding quantity from the trade agreements table which is the biggest qty number that is still lower than the sales qty. Then just calculate the price for where the quantity is the same as we have in above explained variable and return the value.

Trade agreement price = 
VAR latest_qty = [Latest purch qty]
VAR corresp_qty =
    CALCULATE (
        MAXX ( TradeAgreements, TradeAgreements[QUANTITYAMOUNTFROM] ),
        TradeAgreements[QUANTITYAMOUNTFROM] <= latest_qty
    )
VAR corresp_price =
    CALCULATE (
        MINX ( TradeAgreements, TradeAgreements[Price Eur] ),
        TradeAgreements[QUANTITYAMOUNTFROM] = corresp_qty
    )
RETURN
    corresp_price

 

View solution in original post

2 REPLIES 2
Jayee
Responsive Resident
Responsive Resident

Hi @Anonymous 

 

Use the "Related" DAX function and get the value from one table to another table.

 

If this post helps, then please consider Accept it as the solution, Appreciate your Kudos!!

Anonymous
Not applicable

Sorry but that does not help since there is one to many relationship there so multiple values would be returned. But actually, I now managed to solve this problem and can share the resolution here if somebody else comes up with a similar problem. 

 

So I basically created a new measure that is the latest purchase qty and saved that to a variable. Then I calculate the corresponding quantity from the trade agreements table which is the biggest qty number that is still lower than the sales qty. Then just calculate the price for where the quantity is the same as we have in above explained variable and return the value.

Trade agreement price = 
VAR latest_qty = [Latest purch qty]
VAR corresp_qty =
    CALCULATE (
        MAXX ( TradeAgreements, TradeAgreements[QUANTITYAMOUNTFROM] ),
        TradeAgreements[QUANTITYAMOUNTFROM] <= latest_qty
    )
VAR corresp_price =
    CALCULATE (
        MINX ( TradeAgreements, TradeAgreements[Price Eur] ),
        TradeAgreements[QUANTITYAMOUNTFROM] = corresp_qty
    )
RETURN
    corresp_price

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.