Forum Discussion

nok's avatar
nok
Icon for Advocate II rankAdvocate II
1 year ago
Solved

Find the nearest and highest value by ID

I have two tables that follow this structure:
Order

IDQty
1111000
2224500

 

Scale

ID      ScaleQty       
1111000
1112000
1115000
11110000
2223500
2226000
2229000

 

I'd like to create a new column in the Order table that, for each ID, checks the quantity ordered (in the Order[Qty] column) and finds the nearest and highest quantity in the Scale[ScaleQty] column. It can't be the largest Qty of them all (for example ID 111, it can't be 10000); so for ID 111 it needs to be the nearest and highest quantity to 1000, which in this case would be 2000.
The final result of this new column would be something like this:

IDQtyNewColumn
11110002000
22245006000


How can I do this?

  • Hi,

    This calcualated column formula in the Orders table will work

    =CALCULATE(MIN(Scale[ScaleQty]),FILTER(Scale,Scale[ID]=EARLIER('Order'[ID])&&[ScaleQty]>EARLIER('Order'[Qty])))

    Hope this helps.

     

2 Replies

  • Hi,

    This calcualated column formula in the Orders table will work

    =CALCULATE(MIN(Scale[ScaleQty]),FILTER(Scale,Scale[ID]=EARLIER('Order'[ID])&&[ScaleQty]>EARLIER('Order'[Qty])))

    Hope this helps.