excelexcel-formula

Recursive Average Delta Over Variable Size Dataset


I have an inventory tracking system that unfortunately doesn't provide data for quantity change over weeks. I'm attempting to develop something similar in Excel that will give easy access to average drop week over week in order to identify approximate reorder thresholds.

I have an inventory tracking spreadsheet with new data input weekly. I've been trying to develop a formula that will recursively calculate the change in quantities between consecutive weeks, and then average those changes to give an estimated usage count per week. Ideally this doesn't add additional output cells or use macros; just drop new data in and have the averages update automatically.

excel inventory chart

I'm familiar with both functions of calculating change and averaging, but would require helper cells to hold the change data and then average that row. In essence, in Cell B2: =AVERAGE(D2-C2, E2-D2, ...) run through the entirety of Row 2, including new data added weekly in Column O, P, Q, etc.

I have not worked with any kind of looping or recursion in Excel before as I typically work with static datasets -- is this even possible with formulas? I have not found formulas such as WHILE, LOOP, FOR.

I've tried utilizing COUNT to get the number of cells within a table row, and then INDEX to find specific data and compare backwards =INDEX(A1:N6, 2, [COUNTFORMULA]) - INDEX(A1:N6, 2, [COUNTFORMULA]-1) but obviously this is missing any kind of recursion to work down each weeks data points.


Solution

  • You can use this formula on row 2 and drag it down:

    =LET(data,TRIMRANGE($C2:XFD2),AVERAGE(DROP(data,,1)-DROP(data,,-1)))
    

    The definition of the data name is using TRIMRANGE to return those cells on the current row, from column 2 onwards, which contain data (this will grow as you add more weeks, so this is a dynamic way to capture new weeks).

    In the example in the image, data contains cells the data in C2:N2.

    DROP(data,,1) removes the first column from data, meaning what you're left with is the data in cells D2:N2.

    Similarly, DROP(data,,-1) removes the last column, leaving you with the data in cells C2:M2.

    Subtracting the former from the latter returns an array of the differences between successive weeks.

    Wrapping that in AVERAGE returns the average of those differences.

    enter image description here