player0
player0

Reputation: 1

ArrayFormula of Resetting Running Total in Google Sheets

I'm looking for a (non-dragging) ArrayFormula of running total that resets every time the value in alt column changes. example:

          desired result
a    2          2
a    3          5
a    5         10
b    2          2
c    3          3
c    4          7

so every time value in the 1st column changes the sum resets. the table is always sorted if it matters.


non-reseting regular running total formulae:


I was trying somehow to combine it with this counter formula but no luck so far:

=ARRAYFORMULA(COUNTIFS(A1:A6, A1:A6, ROW(A1:A6), "<="&ROW(A1:A6)))

also I did some research, but only found either script which I am not interested in or dragging/MS Excel formulae solutions like:

=SUM(INDIRECT("L"&SUMPRODUCT(MAX(($H$2:H4=0)*ROW($H$2:H4)))+1):L5)
-----------------------------------------------------------------------------------------------------------
=SUM(L$3:L5)-SUM(M$4:M4)
-----------------------------------------------------------------------------------------------------------
=SUM($C$2:$C2)-IFERROR(SUM($C$2:OFFSET($C$1,LOOKUP(2,1/($B$2:$B2="reset"),ROW($B$2:$B2)-ROW($B$2)+1),0)),0)
-----------------------------------------------------------------------------------------------------------
=MOD((ROW()-ROW(E$1))*1,(1+5))

Upvotes: 1

Views: 1165

Answers (3)

player0
player0

Reputation: 1

modification of @JPV solution focused on speed:

=INDEX(MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(
 INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(
 INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))))*(
 INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(
 INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))), 
 INDIRECT("B2:B"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(
 INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))^0))

enter image description here

shortened:

=INDEX(LAMBDA(x, MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(x))>=
 ROW(x))*(x=TRANSPOSE(x)), OFFSET(x,,1), 0)), ROW(x)^0))
 (A2:INDEX(A:A, MAX(ROW(A:A)*(A:A<>"")))))

enter image description here


=INDEX(IF(B2:B="",, ROW(B2:B) - VLOOKUP(ROW(B2:B), FILTER(ROW(B2:B), B2:B<>"", B2:B<>B2:Boffset1), 1, 1) + 1)) - B=T/F.txt

Upvotes: 2

Tom Sharpe
Tom Sharpe

Reputation: 34180

If the table is always sorted on column A, you can just do:

=ARRAYFORMULA(SUMIF(ROW(B1:B6), "<="&ROW(B1:B6), B1:B6)-SUMIF(A1:A6, "<"&A1:A6, B1:B6))

enter image description here

If the table is not sorted, you can still do it with a vlookup:

=ARRAYFORMULA(SUMIF(ROW(B1:B6), "<="&ROW(B1:B6), B1:B6)-SUMIF(row(A1:A6), "<"&vlookup(A1:A6,{A1:A6,row(A1:A6)},2,false), B1:B6))

enter image description here

Upvotes: 1

JPV
JPV

Reputation: 27242

In addition, you could also try

=ArrayFormula(if(len(A:A),mmult(--transpose(if( (transpose(row(A:A))>=row(A:A))*(A:A=transpose(A:A)),B:B, 0)),row(A:A)^0),))

This should also work if the data is unsorted.

enter image description here

Upvotes: 1

Related Questions