Nidere
Nidere

Reputation: 129

Google Sheets sum every Nth element from a range up to M elements

I need to calculate the sum of M elements from every Nth row from data set (column A) starting from current cell's row (column B).

I've managed to do that via nested queries, but the performance is awful and the formula needs to be pasted into each cell of column B individually - while I need the table to be self-adjusting.

I've found out that most likely I need to use the SCAN formula, but I struggle to write the correct lambda for it and it gives me incorrect results.

Have a look at my table: https://docs.google.com/spreadsheets/d/1uzM1mx4uoIOiTtenI-fidb72r6Bqa3jnXmD90utPgS8/edit?usp=sharing

What am I doing wrong?

Upvotes: 0

Views: 280

Answers (2)

player0
player0

Reputation: 1

alternative:

=INDEX(BYROW(IFNA(VLOOKUP(MAKEARRAY(
 ROWS(A2:A), H1, LAMBDA(a, b, 2+(a-1)+(b-1)*H2)), 
 {ROW(A2:A), A2:A}, 2, )), LAMBDA(x, SUM(x))))

enter image description here

enter image description here

enter image description here

Upvotes: 1

z..
z..

Reputation: 13013

You can use the QUERY function with the skipping clause combined with OFFSET:

=MAP(A2:A,LAMBDA(a,SUM(QUERY(OFFSET(a,,,H1*H2),"skipping "&H2)))) 

Where A2:A is the range of numbers, H1 is n and H2 is m.

enter image description here

Upvotes: 2

Related Questions