need spill formula

Scott R

Active Member
Joined
Feb 20, 2002
Messages
483
Office Version
  1. 365
Platform
  1. Windows
Rows 2:8 show inputs. Rows 10:14 show desired output--I'd like a dynamic array formula in B10 that spills to B10:C14 (two columns in this example but I really need to spill to 40+ columns).

B2: Item1 needs 10,000 of purchases.
B8: Total purchases are 50,000.
B10: 10,000 of the purchases are allocated to Item1, so the new balance for Item1 is 0, and there are 40,000 of purchases to allocate to Items 2, 3, and part of 4.
Repeat for each Month (column).

So it's basically Rows 2:6 minus Row 8 = Rows 10:14, where no values in 10:14 would be negative and allocation order would cascade from Item1 to Item5.

Book2
ABC
1Month1Month2
2Item1 begin bal10,00010,000
3Item2 begin bal11,00023,000
4Item3 begin bal26,00021,000
5Item4 begin bal7,00011,000
6Item5 begin bal29,00015,000
7
8tot purchased50,00020,000
9
10Item1 end bal00
11Item2 end bal013,000
12Item3 end bal021,000
13Item4 end bal4,00011,000
14Item5 end bal29,00015,000
Sheet1
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I'm afraid I can't follow what kind of calculations you want for B10:B14. You say B2 is 10,000, so B10 is 0, but why would B10 would be 0? What calculation determines that? Could you give us some explicit calculation examples, maybe for B10, B12, and B14?
 
Upvote 0
In Month1 there is a beginning balance of 83,000 in total (B2:B6).
Purchases are 50,000 (B8) and are applied to the beginning balance starting with Item1.
The resulting ending balance of 33,000 is in B10:B14.
 
Upvote 0
I think your calculation for the positive amounts is not the same as the negative amounts.. no matter how I write the calc, I get 33,000 in B14... which makes sense for what the formula is doing.
Maybe once the amounts get positive, the calc needs to change a little bit..

Cell Formulas
RangeFormula
I2:I6I2=$B$8-SUM($B$2:B2)
J2:J6J2=SUM($B$2:B2)
K2:K6K2=SUM($B$2:B2)-$B$8
 
Upvote 0
That explanation gave me enough to figure it out. It's easy to get a formula for these cells, like this for B10:B14:

Excel Formula:
=LET(sum_input,SUM(B$2:B2), sum_output,SUM(B$9:B9), IF(sum_input>B$8, sum_input-B$8-sum_output, 0))

But that's not a spill formula. I'm not great with spill-type formulas, so I'm having trouble getting something to work.
 
Upvote 0
In my attempt to solve this, I got close, but @Fluff ultimately provided the solution: Dynamic Array referencing previous row.

You'll just have to drag it across the months:
Book1
ABC
1Month1Month2
2Item1 begin bal1000010000
3Item2 begin bal1100023000
4Item3 begin bal2600021000
5Item4 begin bal700011000
6Item5 begin bal2900015000
7
8tot purchased5000020000
9
10Item1 end bal00
11Item2 end bal013000
12Item3 end bal021000
13Item4 end bal400011000
14Item5 end bal2900015000
Sheet1
Cell Formulas
RangeFormula
B10:C14B10=MAP(-SCAN(B8,B2:B6,LAMBDA(a,b,a-b-MIN(0,a))),LAMBDA(m,MAX(0,m)))
Dynamic array formulas.
 
Upvote 0
Works great! Any way to have B10 spill to columns? I'd like it also process Column C and beyond.
 
Upvote 0
Hello, just for fun (definitely use dreid1011's solution):

Excel Formula:
=LET(
a,B2:C6,
b,B8:C8,
array,VSTACK(a,b),
c,DROP(REDUCE("",SEQUENCE(COLUMNS(array)),LAMBDA(f,g,HSTACK(f,DROP(VSTACK(TAKE(CHOOSECOLS(VSTACK(array),g),-1),SCAN(TAKE(CHOOSECOLS(VSTACK(array),g),-1),DROP(CHOOSECOLS(VSTACK(array),g),-1),LAMBDA(x,y,IF(x-y<0,0,x-y)))),-1)))),,1),
IF(a-c<0,0,a-c))
 
Upvote 0
Solution
Hello, just for fun (definitely use dreid1011's solution):

Excel Formula:
=LET(
a,B2:C6,
b,B8:C8,
array,VSTACK(a,b),
c,DROP(REDUCE("",SEQUENCE(COLUMNS(array)),LAMBDA(f,g,HSTACK(f,DROP(VSTACK(TAKE(CHOOSECOLS(VSTACK(array),g),-1),SCAN(TAKE(CHOOSECOLS(VSTACK(array),g),-1),DROP(CHOOSECOLS(VSTACK(array),g),-1),LAMBDA(x,y,IF(x-y<0,0,x-y)))),-1)))),,1),
IF(a-c<0,0,a-c))
I don't know how some of you are able to manage these formulas. It's hard for me to break them down.
 
Upvote 0
Here's what I landed on:
BYCOL spills right, VSTACK spills down (thx for the tip, Sofia!)

Excel Formula:
=LET( ary, B2:C6,     purch, B8:C8,
           DROP( REDUCE( "", SEQUENCE( ROWS( ary )), LAMBDA(a,b, VSTACK( a,
                           LET( prevCumul, IF( b = 1, SEQUENCE( , 2, 0, 0 ), BYCOL( TAKE( ary, b - 1 ), LAMBDA(c, SUM( c )))),
                                    bbal, CHOOSEROWS( ary, b ),
                                    ebal, IF( prevCumul > purch, bbal, bbal - purch + prevCumul ),
                                    IF( ebal < 0, 0, ebal ))))),
   1 ))
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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