Find next bigger value in two-way lookup

Skovgaard

Board Regular
Joined
Oct 18, 2013
Messages
197
Office Version
  1. 365
Platform
  1. Windows
Hello Experts,

Hope you can help.
Below table shows a running balance on a stock in quantities.
My goal is to find, when a certain order can be delivered.

Using two Xlookup-functions, I'm able to find the result, if there's enough pcs on stock.
But if there isn't enough items on stock, then I need to find when there will be.

Example:
- Requested delivery date from customer: 2021-33
- Item1
- 5 pcs

Correct result should be "2021-35", hence complete delivery can be fulfilled.

Hope you understand the challenge, otherwise don't hesitate to write your questions.

Running stock balance:
Item no.: / YYYY-Week2021-312021-322021-332021-35
Item 1100325
Item 25101512
Item 3001015


/Skovgaard
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Something like this?

Skovgaard.xlsm
ABCDE
1Running stock balance:
2Item no.: / YYYY-Week2021-312021-322021-332021-35
3Item 1100325
4Item 25101512
5Item 3001015
6
7
8
9Requested Delivery Date2021-332021-322021-312021-32
10ItemItem 1Item 3Item 2Item 2
11Qty512320
12Delivery2021-352021-352021-31#N/A
Sheet1
Cell Formulas
RangeFormula
B12:E12B12=LET(f,FILTER(FILTER($B2:$E5,$B2:$E2>=B9),($A2:$A5=B10)+($A2:$A5=$A2)),INDEX(INDEX(f,1,0),MATCH(TRUE,INDEX(f,2,0)>=B11,0)))
 
Upvote 0
Something like this?

Skovgaard.xlsm
ABCDE
1Running stock balance:
2Item no.: / YYYY-Week2021-312021-322021-332021-35
3Item 1100325
4Item 25101512
5Item 3001015
6
7
8
9Requested Delivery Date2021-332021-322021-312021-32
10ItemItem 1Item 3Item 2Item 2
11Qty512320
12Delivery2021-352021-352021-31#N/A
Sheet1
Cell Formulas
RangeFormula
B12:E12B12=LET(f,FILTER(FILTER($B2:$E5,$B2:$E2>=B9),($A2:$A5=B10)+($A2:$A5=$A2)),INDEX(INDEX(f,1,0),MATCH(TRUE,INDEX(f,2,0)>=B11,0)))
Hi Peter,

Thanks for your reply.
First time I see the function "let", seems pretty cool :)

I've testet your formula and I see one problem. My running stock balance is in a pivot table, so it seems that the filter function won't work on that.
Any way to solve this?

/Skovgaard
 
Upvote 0
It works for me running off a PT.
Here is my dummy sample data, PT & formula results.

Skovgaard.xlsm
ABCDEFGHIJ
1ItemNumYYY-WeekSum of NumYYY-Week
2Item 102021-32Item2021-312021-322021-332021-35Grand Total
3Item 102021-31Item 1203611
4Item 112021-33Item 2534315
5Item 242021-33Item 31861025
6Item 122021-31Grand Total811131951
7Item 212021-31
8Item 102021-35
9Item 212021-35
10Item 302021-33
11Item 302021-35
12Item 352021-33
13Item 202021-33
14Item 202021-33Requested Delivery Date2021-332021-322021-312021-32
15Item 302021-32ItemItem 1Item 3Item 2Item 2
16Item 312021-33Qty59320
17Item 122021-33Delivery2021-352021-352021-31#N/A
18Item 202021-32
19Item 232021-32
20Item 242021-31
21Item 142021-35
22Item 302021-32
23Item 312021-32
24Item 222021-35
25Item 372021-32
26Item 122021-35
27Item 312021-31
28Item 202021-32
29Item 202021-33
30Item 3102021-35
31
Sheet2
Cell Formulas
RangeFormula
F17:I17F17=LET(f,FILTER(FILTER($F2:$I5,$F2:$I2>=F14),($E2:$E5=F15)+($E2:$E5=$E2)),INDEX(INDEX(f,1,0),MATCH(TRUE,INDEX(f,2,0)>=F16,0)))
 
Upvote 0
It works for me running off a PT.
Here is my dummy sample data, PT & formula results.

Skovgaard.xlsm
ABCDEFGHIJ
1ItemNumYYY-WeekSum of NumYYY-Week
2Item 102021-32Item2021-312021-322021-332021-35Grand Total
3Item 102021-31Item 1203611
4Item 112021-33Item 2534315
5Item 242021-33Item 31861025
6Item 122021-31Grand Total811131951
7Item 212021-31
8Item 102021-35
9Item 212021-35
10Item 302021-33
11Item 302021-35
12Item 352021-33
13Item 202021-33
14Item 202021-33Requested Delivery Date2021-332021-322021-312021-32
15Item 302021-32ItemItem 1Item 3Item 2Item 2
16Item 312021-33Qty59320
17Item 122021-33Delivery2021-352021-352021-31#N/A
18Item 202021-32
19Item 232021-32
20Item 242021-31
21Item 142021-35
22Item 302021-32
23Item 312021-32
24Item 222021-35
25Item 372021-32
26Item 122021-35
27Item 312021-31
28Item 202021-32
29Item 202021-33
30Item 3102021-35
31
Sheet2
Cell Formulas
RangeFormula
F17:I17F17=LET(f,FILTER(FILTER($F2:$I5,$F2:$I2>=F14),($E2:$E5=F15)+($E2:$E5=$E2)),INDEX(INDEX(f,1,0),MATCH(TRUE,INDEX(f,2,0)>=F16,0)))

You're right, I misspelled a range, it also works for me too.

We miss one important thing in the formula, negative balance protection.
Each time we put in a delivery date, the running stock balance must be updated with this. I've already a macro doing this, that's not the challenge.

E.g. I've made small changes to your dummy sample item 2, see below.
The first order (qty=6) will be set to week 32 and the second to week 31, total 8 pcs of item 2.

Do you understand what I mean?
Is it possible to check for future negative balance, in order to prevent this?

PS. For each cell I do this formula, I'll paste the values and update the pivot, before I go to next cell.

1629879806795.png


/Skovgaard
 
Upvote 0

I guessed it wasn't that good explained, I'll try again with below example :)
This is just how I imagine it could work. Other possible solutions are welcome.

Start:
1. One table showing in (and out) transactions on stock
a. Pivot table showing running balance of stock

1629888260892.png


Open Orders:
2. Check of availability week in below column F (your original formula)
a. Formula is typed in F2 - Result week 2021-17 (order Qty=7)
b. The 7 pcs will be stored in stock transaction table (row 6 in this example - Not showed)
c. PT will be updated with new running balance - Balance in week 2021-17 is now 3
d. Point a-c is repeated for F3+F4
c. Balance in week 2021-12 is now 0. For 2021-17 it is -2, which isn't possible (negative balance)

1629888546515.png


So I was wondering if there was a way to prevent, future balances go in minus?

/Skovgaard
 
Upvote 0
I've tried to implement Peters "LET" function in VBA as below, but it doesn't seems to work.

PivotData, PivotWeek and PivotItem are names for defined variable areas in the workbook.
Is it not possible to do it like this, or should I define the names in the sub instead?

VBA Code:
For Each cell In rng
        If cell.Value = "" Then
            cell.FormulaR1C1 = _
                                "=Let(f,Filter(PivotData,PivotWeek>=RC[-1]),(PivotItem=RC[-23])+(PivotItem=Lager!R11C[-17])),Index(Index(f,1,0),Match(TRUE,Index(f,2,0)>RC[-2],0)))"


/Skovgaard
 
Upvote 0
Pretty hard to tell without all the details, sample data, layout, names etc, but the formula does not look a lot like what I suggested.
For a start:
Mine has two 'Filter's, yours has one
The two blue items are identical for me, not so for you.

=Let(f,Filter(PivotData,PivotWeek>=RC[-1]), ....
=LET(f,FILTER(FILTER($F2:$I5,$F2:$I2>=F14), ...
 
Upvote 0
Pretty hard to tell without all the details, sample data, layout, names etc, but the formula does not look a lot like what I suggested.
For a start:
Mine has two 'Filter's, yours has one
The two blue items are identical for me, not so for you.

=Let(f,Filter(PivotData,PivotWeek>=RC[-1]), ....
=LET(f,FILTER(FILTER($F2:$I5,$F2:$I2>=F14), ...

Argh... I missed the on "Filter" function, now it puts the formula into my cell.
One problem, it puts in two "@", see below.

Why is that, and can that be avoided?


1630319613235.png


/Skovgaard
 
Upvote 0

Forum statistics

Threads
1,215,268
Messages
6,123,966
Members
449,137
Latest member
yeti1016

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top