Reputation: 25
So I have data consisting of the following:
there are multiple more rows.
Im trying to retrieve the last 5 values in a row, for further use. I can do this with a simple INDEX(MATCH()) setup, however that doesn't ignore the blank cells, which I would like it to do. It does successfully detect the first nonblank cell and returns that, however when it's blank in the middle of the 5 most recent, it doesn't remove that.
something like this is what it does now:
however i want it to come back in this format:
and so on. A blank could occur at any position, and there may not always be 5 scores available to retrieve.
TIA
Upvotes: 0
Views: 954
Reputation: 11438
You could use the following array-formula (entered with ctrl+shift+enter
in older Excel versions):
=INDEX(1:1,AGGREGATE(14,6,COLUMN(A:G)/(A1:G1<>""),{5,4,3,2,1}))
copied down.
Aggregate creates an array of the column numbers divided by 1 or 0 (TRUE or FALSE). Divided by 0 results in an error and gets ignored. It then takes the 5th largest to the largest column number without error and returns that as an array on the indexed row.
Where 1:1
represents the first row and A:G
represents the first to last column used.
If you would want it on row 2 and column A:Z you'd have to amend it like this:
=INDEX(2:2,AGGREGATE(14,6,COLUMN(A:Z)/(A2:Z2<>""),{5,4,3,2,1}))
Upvotes: 1
Reputation: 13014
Different approach - using the new Excel 365 formulas:
This will return the values of the last five non-empty cells of row 2
=LET(
data,FILTER(B2:H2,B2:H2<>""),
cntData,COUNT(data),
matrix,SEQUENCE(1,MIN(cntData,5),IF(cntData>5,cntData-4,1)),
INDEX(data,1,matrix)
)
FILTER
- formulaSEQUENCE
a matrix is build that will return the column-indices to be returned. In case less then 5 values are available, the matrix returns 1 to cntData.This could be enhanced, by storing the number of cells to be returned within a named cell - and referencing this name in the formula. By that you could easily alter the number without altering the formula.
Upvotes: 0