Summary

To sum every n rows, you can use the WRAPROWS function with the BYROW function and the SUM function. In the example shown, the formula in F5 is:

=BYROW(WRAPROWS(C5:C19,5),SUM)

where n is 5 because each week has 5 rows of data. WRAPROWS arranges the amounts in rows of 5, and BYROW returns the sum of each row, one total per week. To average every n rows, use AVERAGE in place of SUM. In Excel 2021 and earlier, use the OFFSET formula at the end of this page.

Generic formula

=BYROW(WRAPROWS(data,n),SUM)

Explanation

In this example, the goal is to calculate a weekly total for the amounts in column C. Each week has 5 rows of data (Monday to Friday), so we need to sum every 5 rows. This is an interesting problem, because SUM returns one total for whatever range you give it, so we need a way to split one range into groups. This turns out to be an excellent use case for the WRAPROWS function. The formula above works in two steps: first, it reshapes the amounts so that each week is one row, then it sums each row.

Table of contents

Group values with WRAPROWS

The WRAPROWS function wraps a single row or column of values into rows of a given length. Given the 15 amounts in C5:C19 and a row length of 5:

WRAPROWS(C5:C19,5)

WRAPROWS returns a 3 x 5 array, one row per week:

{45,70,20,0,85;42,35,30,50,45;25,105,80,40,35}

Commas separate the values in each row, and semicolons separate the rows. To change the size of the groups, change the second argument: 7 for daily data by week, 12 for monthly data by year, and so on.

Sum each row with BYROW

The BYROW function runs a calculation on each row in an array and returns one result per row. Here, the array comes from WRAPROWS, and the calculation is SUM:

=BYROW(WRAPROWS(C5:C19,5),SUM)

BYROW gives each row of 5 amounts to SUM and returns the three totals in an array:

{220;202;285}

These totals spill into F5:F7. Notice that SUM appears by name only, with no parentheses. This is an eta lambda, a shortcut for LAMBDA(row,SUM(row)). The formula is dynamic: if an amount changes, the totals update immediately.

If the number of rows isn't a multiple of n, WRAPROWS fills the empty slots in the last row with #N/A, and SUM returns #N/A for that group. To avoid this problem, give WRAPROWS an empty string ("") as the pad_with argument: =BYROW(WRAPROWS(C5:C21,5,""),SUM). SUM will automatically ignore the empty strings and sum the remaining numeric values.

To total by calendar week from the dates instead of by counting rows, see Sum by week.

Average every n rows

To average every n rows, use AVERAGE in place of SUM. A common reason to do this is to reduce data recorded more often than needed. In the worksheet below, a sensor recorded a reading every 0.2 seconds, and the goal is one average for each second, which means averaging every 5 rows. The formula in F5 is:

=BYROW(WRAPROWS(C5:C16,5,""),AVERAGE)

Average every n rows with WRAPROWS and BYROW

There are 12 readings, so the last second has only 2, and this is where the pad_with argument matters. By default, WRAPROWS fills the 3 empty slots in the last row with #N/A, and AVERAGE returns #N/A. Padding with zero is no better, because AVERAGE counts the zeros and returns 0.64 instead of 1.59. An empty string ("") works because AVERAGE ignores text, so the last result is the average of the 2 readings that exist. Empty cells in the data are ignored the same way; only a zero counts as a reading.

To label each group with its start time, the formula in E5 uses the TAKE function to return the first column of the wrapped times:

=TAKE(WRAPROWS(B5:B16,5),,1)

You can get the same averages from a 5-point moving average by keeping every 5th result, since every 5th result averages exactly one group of 5. The formula above does the job in one step, with no helper columns.

Excel 2019 and earlier

Legacy Excel has no WRAPROWS or BYROW function, and neither does Excel 2021. In these versions, you can use the OFFSET function to build a range for each group and the SUM function to add it up. In the worksheet at the top of the page, the formula in F5, copied down, would be:

=SUM(OFFSET($C$5,(ROW()-5)*5,0,5,1))

The generic form is:

=SUM(OFFSET(A1,(ROW()-offset)*n,0,n,1))

To build a range that corresponds to the correct 5 rows for each week, we use the OFFSET function. To sum the values returned by OFFSET, we use the SUM function.

Calculating ranges

The OFFSET function returns a reference to a range constructed with five inputs: (1) a starting point, (2) a row offset, (3) a column offset, (4) a height in rows, and (5) a width in columns. In the example shown, we use OFFSET inside of SUM like this:

OFFSET($C$5,(ROW()-5)*5,0,5,1)

The starting reference is C5 since this is the first value in the data. Notice we use an absolute reference because we don't want this reference to change later when we copy the formula down. The next argument, rows, is the crux of the problem because this is where we need logic that will figure out the correct starting row for each week.

To calculate the right starting point, we use the ROW function. ROW returns the row number for a given reference or, if no reference is provided, the row number of the cell containing the formula. Because the formula sits in cell F5, ROW() will return 5. We use this fact to create the logic we need. We first subtract 5 as an "offset" because we want to create a zero-based index for reasons made clear below. Then, we multiply the result by 5. This is how this snippet will evaluate in cells F5, F6, and F7:

F5=(ROW()-5)*5 // returns 0
F6=(ROW()-5)*5 // returns 5
F7=(ROW()-5)*5 // returns 10

To finish off the arguments for OFFSET, we provide cols as zero, because we want to stay in the same column. We provide height as 5 because we want a range that contains 5 rows. Finally, we provide width as 1, because we want a range that contains 1 column. After the row logic runs, this is how OFFSET works in F5, F6, and F7:

F5=OFFSET($C$5,0,0,5,1) // returns C5:C9
F6=OFFSET($C$5,5,0,5,1) // returns C10:C14
F7=OFFSET($C$5,10,0,5,1) // returns C15:C19

Summing ranges

The final step in the problem is just to sum the ranges provided by OFFSET. This is done with the SUM function. After OFFSET is evaluated, the resulting range is delivered directly to the SUM function, which sums the values in the range and returns a final result. As the formula is copied down into F5:F7, we get the final totals per week:

SUM(C5:C9) // returns 220
SUM(C10:C14) // returns 202
SUM(C15:C19) // returns 285

To recap, OFFSET returns a calculation of the correct range for each week using the row logic explained above. The SUM function sums the ranges returned by OFFSET. To average every n rows instead, replace SUM with AVERAGE.

Note: the WRAPROWS function is available in Excel 2024+ and Excel 365.

Support Exceljet

Many websites (including Exceljet) have lost over half their traffic in the past year because of the crushing impact of AI on web searches. Exceljet is still at it, publishing free content each week. If you find Exceljet helpful, you can support the site and go ad-free with the Exceljet Lifetime Pass. One payment, no subscription, and ads are gone forever. Plus, you get our Excel Shortcuts video course as a thank you.

Get the Lifetime Pass

Dave Bruns Profile Picture

AuthorMicrosoft Most Valuable Professional Award

Dave Bruns

Hi - I'm Dave Bruns, and I run Exceljet with my wife, Lisa. Our goal is to help you work faster in Excel. We create short videos, and clear examples of formulas, functions, pivot tables, conditional formatting, and charts.