Sam
Sam

Reputation: 2875

How to use SUM function with (new) dynamic arrays in Excel

Let's say 3 columns (A, B, C) are dynamic arrays and I want to create a fourth/final dynamic array formula that is the sum of these 3 columns for each row in column D. For clarity, I am looking for the row-by-row sum of each row in this final column.

This will work: =A2#+B2#+C2#

How can the same be accomplished by using the SUM function? The reason I ask is that this is easier to use on larger ranges of data.

The following gives a #REF! error: =SUM(A2:C2#)

Upvotes: 8

Views: 15873

Answers (4)

Scott Craner
Scott Craner

Reputation: 152660


New Edit:

With the addition of BYROW and LAMBDA we can do this a little easier than my original answer below:

=BYROW(A1#:C1#,LAMBDA(x,SUM(x)))

The BYROW passes each row into the LAMBDA which does the SUM iteratively and returns an array:

enter image description here


Original Answer

The problem is that SUM,MAX,MIN all allow arrays and do the whole on the full array. So we need to use something that uses arrays and spills individual results. That is what MMULT was built for.:

=MMULT(A2#:C2#,TRANSPOSE(COLUMN(A2:C2)^0))

enter image description here


Just realized with the dynamic arrays we have SEQUENCE:

=MMULT(A2#:C2#,SEQUENCE(COLUMNS(A2:C2),,1,0))

Upvotes: 20

Chris L.
Chris L.

Reputation: 11

When I tried Scott Craner's formula above I received a VALUE# error, possibly related to the fact that I was testing it on data columns of different dynamic lengths.

Even though the expression A2#:C2# returns a matrix of width 3 columns, and height of whichever of the three columns has the most rows (filling in zeros for any blank cells), MMULT didn't seem to like that expression and resulting matrix as the first argument. However I found that I was able to modify it as follows to make it work:

=MMULT((A2#:C2#*1),SEQUENCE(COLUMNS(A2:C2),,,0))

Upvotes: 1

Educerva
Educerva

Reputation: 21

Try this for sums per column (assuming A1# is the dynamic range with the source data):

=SUBTOTAL(9,OFFSET(A1#,0,SEQUENCE(1,COLUMNS(A1#))-1,ROWS(A1#),1))

Just change the first argument of the SUBTOTAL function to use any of the aggregation functions available (min, max, average, etc.). With some tweaks, this can be made to work for row totals.

Upvotes: 2

Matt L
Matt L

Reputation: 81

Absolute legend. I've been trying to work this out. Thank you so much. I have never used MMULT before, only SUMPRODUCT for similar problems. My problem was to dynamically sum up the last few columns in a spill table and I was able to adapt your solution.

In terms of the question above, if the three spill columns were one spill table with rows and columns in the one spill range it would be:

=MMULT(A2#,SEQUENCE(ROWS(A2#)),SEQUENCE(COLUMNS(A2#)),,1,0))

Upvotes: 0

Related Questions