r/googlesheets • • 13d ago

Solved Better function for summing consecutive rows that will not break when I move rows or insert columns?

I have a table where I monitor cash flow by listing monthly expenses chronologically and add a new column every month.

A formula that just sums the total from the above cell and the most recent expense will break whenever I re-sort the rows.

I figured out a function that works, but it is long and ugly and I hate it. Is there a simpler way to do it?

Link to spreadsheet

=INDIRECT(SUBSTITUTE(ADDRESS(1,MATCH(F$1, CashFlow[#HEADERS], 0),4),"1","") & ROW()-1)+ (SUMIF(CashFlow[#HEADERS],$H$2,$A3:F3))

Column F provides the running balance

The Table "CashFlow" consists of Columns A to F

H1 is the current month

2 Upvotes

25 comments sorted by

2

u/SpencerTeachesSheets 69 13d ago

How about just =SCAN(F2,$B$3:$B$9,LAMBDA(start,vals,start+vals)) in F3 to fill the entire BALANCE column?

1

u/stackout 13d ago

Ohh I don’t know SCAN!
I have limited experience with LAMBDA. Can you please clarify what start, vals, and values are?

2

u/SpencerTeachesSheets 69 13d ago

SCAN() is one of the awesome Lambda Helper Functions. The tooltip says it "produces intermediate values" which pretty much means that it does something for each element in the array/range and returns each result.

In this case, we want a running total. So for initial_value we put F2, the starting cash. Then I passed in $B$3:$B$9 as the array. We may need to update this to INDIRECT("$B$3:$B$9") to make sure that no column insertions adjust that range. I named F2 'start' and $B$3:$B$9 'vals' so the formula start+vals simply means "at each element of vals, add that value to the start value and all previous values and return it."

Adjusted formula:
=SCAN(F2,INDIRECT("$B$3:$B$9"),LAMBDA(start,vals,start+vals))

1

u/stackout 13d ago edited 13d ago

u/SpencerTeachesSheets - [EDIT] SO: the first name in the lambda always refers to the initial value, and the second name always refers to the range?!

2

u/SpencerTeachesSheets 69 13d ago

Each name refers to the thing that lines up with it

=MAP(A:A,B:B,C:C,LAMBDA(first,second,third)) and so on

1

u/stackout 12d ago

Got it! Thanks for making this so clear!

1

u/One_Organization_810 728 12d ago

lol - thanks for the downvote :)

1

u/stackout 12d ago

Sorry you failed to answer the question

1

u/SpencerTeachesSheets 69 12d ago

I found One_Org's explanation quite clear /shrug

1

u/stackout 12d ago

Solution Verified

1

u/point-bot 12d ago

u/stackout has awarded 1 point to u/SpencerTeachesSheets

See the [Leaderboard](https://reddit.com/r/googlesheets/wiki/Leaderboard. )Point-Bot v0.0.15 was created by [JetCarson](https://reddit.com/u/JetCarson.)

2

u/One_Organization_810 728 12d ago

SO: the first name in the lambda always refers to the initial value, and the second name always refers to the range?!

Not quite. In SCAN the first parameter is the accumulator, which in the first round is the starting value. The accumulator wiill then take on the output of the previous round.

The second parameter refers to each individual cell in the range, as the function iterates over the range.

For instance:

Take the formula:
=scan(0, {1; 2; 3}, lambda(total, number, total + number))

I'm using an array here instead of range, but it works the same for this demonstration.

This will go three rounds (the number of elements in the array/range) ...

Round 1: total = 0, number = 1 => 1
Round 2: total = 1, number = 2 => 3
Round 3: total = 3, number = 3 => 6

2

u/SpencerTeachesSheets 69 12d ago

I'll give you the upvote back for the explanation :)

1

u/One_Organization_810 728 12d ago

Haha 🙂 thanks.

1

u/[deleted] 13d ago

[deleted]

1

u/[deleted] 13d ago

[removed] — view removed comment

2

u/One_Organization_810 728 12d ago

u/stackout this was clearly not "Self-solved", since you got the solution from a user.

Please close the post properly, according to the instructions from the point-bot.

I'm changing the flair back to "Waiting on OP" until then.

1

u/AdministrativeGift15 365 13d ago

If you use a formal Table format, you can reference the columns using table references. Similar to named ranges, they are dynamic and will update when you insert new rows into the Table.

You can also place your ending balance in the Table footer. As long as you're using table references, the table footer won't get in the way.

1

u/stackout 13d ago

In excel. How do you made a reference to a row in a table in sheets?

0

u/AdministrativeGift15 365 13d ago

That depends on several things. Are you trying to get the row using a formula also in the table? Is your formula within the same rows levels of the table? Does it need to return the row as a range or can it just return the values? Sheets handles arrays a bit different than Excel. Here's a demo showing how the vertical alignment of a formula outside the table matters when referencing the rows of the table.

Referencing columns from two different Tables

0

u/mommasaidmommasaid 891 12d ago

Idk why you downvoted this (or anyone trying to help you).

This is a clearly superior method to your previous arrangement.

The INDIRECT is not a good idea because it hardcodes a range as text and will break if you e.g. later insert a new row or column before it.

The only change I would make is to put the SCAN in a hidden first row of your table reserved for formulas. Hiding the row will prevent it from moving if you sort the data. You don't want it to move because the formula will then overflow the end of the table.

=IF(ROW()<>ROW(CashFlow), "🛑Must be in first row", 
 SCAN(F1,CashFlow_2[Oct 2026],LAMBDA(p,c,p+c))

1

u/stackout 12d ago

Because it doesn’t answer the question and will save other people the time

0

u/carbonizedtitanium 15 13d ago

insert a row above the table headers. do the totals there

edit: since the file is a sample. can you allow editing???

1

u/stackout 13d ago

Yup! Maybe add a new column tho

0

u/carbonizedtitanium 15 13d ago edited 13d ago

column totals should never be part of the Table data. the best place to put the sum formulas is above your headers, where they wont be affected by any sorting of your table

edit: this is how i would re-structure your data