Perform Rolling Sum Based on Another Column's Value in Excel Table

137 Views Asked by At

I'm looking to perform a rolling sum for an excel table based on the value of a column.

I want the sum only up to the current row (not sum of all rows that have the same value)

I've made a simple example to explain:

I want to use table references in a formula to get the item-wise rolling sum for the first 2 columns in the table below. 'Item' and 'Value' are known ; 'Item_Wise_Rolling_Sum' is to be calculated

Item Value Item_Wise_Rolling_Sum
a 1 1
a 2 3
b 10 10
b 5 15
b 2 17
c 6 6

I tried to use the following formula to no avail:

=SUMIFS(MyTable[[#Headers],[Value]]:[@Value], [Item], [@Item])

The idea was to calculate sum uptill the current row only, not for all rows below.

Thank you in advance.

3

There are 3 best solutions below

5
On BEST ANSWER

For older versions try using SUMIFS()

enter image description here


=SUMIFS(B$2:B2,A$2:A2,A2)

Or, In MS365 try using MAP()

enter image description here


=MAP(A2:A7,B2:B7,LAMBDA(x,y,SUM((A2:x=x)*B2:y)))

Or, using SUMIF() with Structured References

enter image description here


=SUMIF(MyTable[[#Headers],[Item]]:[@Item],[@Item],MyTable[[#Headers],[Value]]:[@Value])

With SUMIFS() need to change the ranges correctly as per the parameters,

=SUMIFS(MyTable[[#Headers],[Value]]:[@Value],MyTable[[#Headers],[Item]]:[@Item],[@Item])

Another alternative way of anchoring and using SUMIFS() for running total in Excel Tables: (Comparatively Shorter Method)

enter image description here


=SUMIFS(INDEX([Value],1):[@Value],INDEX([Item],1):[@Item],[@Item])

As commented by JvdV Sir, alternative approach using MAP() if in future you think of not using Structured References then try the below.

=MAP(A2:A7,B2:B7,LAMBDA(x,y,SUMIF(A2:x,x,B2:y)))

1
On

I'm not sure if another answer is necessary, but you can also just use the current row:

=SUM([Value]*(ROW()>=ROW([Value]))*([Item]=[@Item]))

enter image description here

6
On

Using table references: =SUMIF(MyTable[[#Headers],[Item]]:[@Item],[@Item],MyTable[[#Headers],[Value]]:[@Value])

enter image description here