I'm trying to pull the last value of a matching column, but I'm finding increasingly issues and building a troubling long formula. Imagine I want to pull the last price for Fish.
So far I have
=MATCH(Crafts!A20,$1:$1,0) --> 3
Then
=INDEX($1:$1000,3,MATCH(Crafts!A20,$1:$1,0)) --> 25
This index formula is working when I give it a manual row. However, I don't know how to find the last value in that column so it returns 22.
COUNTA doesn't work as it ignores blanks. I could do COUNTA + COUNTBLANK, but they need a range, and I only have the MATCH, which returns a column index, not the column range.
Is there any easier way to achieve this?
Thanks.
I think I found the solution myself and it was indeed easier than expected, just needed a bit of a workaround.
=LOOKUP(9^9,INDEX($1:$1001,,MATCH(Crafts!A20,$1:$1,0)))
By removing the ROW index in the INDEX formula, it returns an array, the entire column. By performing a LOOKUP of a huge number, it just returns whatever the last value of the array is.
Works, and it's cleaner than everything else I tried, by far.
Use:
=INDEX(INDIRECT("C"&MAX((ROW(C:C))*(C:C<>""))))
Update
Assuming cell Crafts!A20 = Fish - White and the desired output is in the same sheet as input Fish - White column then:
=INDEX(INDIRECT(SUBSTITUTE(
ADDRESS(1, MATCH(Crafts!A20, 1:1, )), "1", )&MAX(ROW(A:A))*(INDIRECT(
ADDRESS(1, MATCH(Crafts!A20, 1:1, ))&":"&
ADDRESS(ROWS(A:A), MATCH(Crafts!A20, 1:1, )))<>"")))
I have two sets of data in columns A and B. I would like to pick the maximum value from column A which is also less than the value in the corresponding row in column B. I think I ought to be able to do this with the MAXIFS function but all the examples I can find compare against static values. I tried these options
=MAXIFS(A1:A10, B1:B10, "<")
=MAXIFS(A1:A10, B1:B10, A&"<"&B)
but neither of them worked as expected. In the first case, it is always 0 suggesting the condition is never met, in the second it gives an error.
I know that I could do this by creating a separate region of cells which first filter out the data that doesn't match the conditional and then simply pick the max from what remains but I'd rather do it in a single cell if possible.
Is there a syntax for this comparison and, if so, what is it?
To the best of my knowledge there isn't a way of getting it to work with MAXIFS.
You can write this
=maxifs(A:A,A:A,"<"&B:B)
and it will accept it, but it just uses the first value in column B and doesn't do a side-by-side comparison.
So you have to do it another way e.g. with a combination of Max and If:
=ArrayFormula(max(if(A:A<B:B,A:A)))
or you can use max with a filter or query:
=max(filter(A:A,A:A<B:B))
=max(query(A:B,"select A where A<B"))
=ARRAY_CONSTRAIN(ARRAYFORMULA(
IF(LEN(INDIRECT(ADDRESS(ROW(), COLUMN(B:B))))>0,
IF(INDIRECT(ADDRESS(ROW(), COLUMN(B:B)))<MAX($A$1:$A),
MAX($A$1:$A), LARGE(UNIQUE($A$1:$A), 2)), )), 1, 1)
I am trying to find a closest absolute value with index match. I looked at several other posts like here but what i am trying to do is a bit different as i want to add multiple search criterias.
As you can see , I am trying to get the absolute closest time for a specific person.
I am using the formula =index(C2:C21,match(F4,B2:B21,-1),match(E4,A2:A21,0)) and I had to copy column B in column C to make my 1st match work. The result is shown in G4. Unfortunately I am struggling to get the correct result.
Effectively I would like use the formula that was posted in the previous post (see link at the top) =INDEX(E2:E21,MATCH(TRUE,INDEX(ABS(D1:D21-I4)=MIN(INDEX(ABS(D2:D21-I4),,)),,),0))
with with a search criteria (the name of the person).
Any help would be much appreciated
Thank you
Thanks #avram
I still end up with some cases where the formula does not work. See below. in G6 and G7 i should get 10:25. (You can ignore column A)
Try this formula in G4,
=index(C$2:C$21, match(min(index(abs(index(C$2:C$21+(B$2:B$21<>E4)*1E+99, , )-F4), , )), if(B$2:B$21=E4, abs(C$2:C$21-F4), 1E+99), 0))
This will work in either google-sheets as a standard (non-array/non-CSE) formula or excel as an array (CSE) formula.
If anyone else wants to tackle this problem with a more elegant formula, you can copy the sample data from this publicly shared google-sheet.
Index match find closest value with multiple search criteria
Perhaps this may exempt a fourth person from retyping the same tired data that the op delivered in image(s).
A very simple approach using a "helper" column with data like:
We want the closest absolute match for larry to 10:15 AM. We enter larry in E1 and 10:15 AM in F1
Then in D2 we enter:
=IF(A2=$E$1,ABS(B2-$F$1),"")
and copy downward. (this is the absolute difference for larry) Finally in E2:
=INDEX(B:B,MATCH(MIN(D:D),D:D,0))
With bigger tables having more columns, it is very easy to add additional criteria if needed.
This answer uses Array Formulas which must be entered using CTRL+SHIFT+ENTER. It's kind of complicated, so I'll do my best to explain and will revise if necessary. Here's a screenshot:
Here is the formula in its raw form; names are entered in column A, Times in Column B.
=INDEX(B1:B7,MATCH(MIN(IF(A1:A7=D2,ABS(E2-B1:B7),"")),IF(A1:A7=D2,ABS(E2-B1:B7),"")))
As you might suspect, it uses INDEX/MATCH to get the job done, but the key is using an IF statement to generate both the search criteria and the array that the MATCH function searches within. Let's break it down.
Sec 1, Match Search Array
IF(A1:A7=D2,ABS(E2-B1:B7),"")
This creates the Search array for the match function. If the name in D2 (our criteria) is equal to the name in the search array, it return the absolute value of the difference between the criteria time and the time in the array we're searching. Otherwise it returns a blank value. Do not use 0 for this as it will skew the match result.
Sec 2, Match Search Criteria
MIN(IF(A1:A7=D2,ABS(E2-B1:B7),""))
This tells us the smallest value in the above array. We use this value as the search criteria in the MATCH function.
Sec 3, putting 1 & 2 Together
MATCH(MIN(IF(A1:A7=D2,ABS(E2-B1:B7),"")),IF(A1:A7=D2,ABS(E2-B1:B7),"")) This searches for the smallest abs difference defined in Section 2 within the array created in Section 1 and returns the row number.
Sec 4, Indexing the times
=INDEX(B1:B7,MATCH(MIN(IF(A1:A7=D2,ABS(E2-B1:B7),"")),IF(A1:A7=D2,ABS(E2-B1:B7),"")))
This returns the time value from column B in whatever row is identified by the Match function above.
Hopefully this all makes sense. Remember to enter it as an array formula.
How to create a Google Spreadsheet sum() which always ends on the cell above, even when new cells are added? I have several such calculations to make on each single column so solutions like this won't help.
Example:
On column B, I have several dynamic ranges which has to be summed. B1..B9 should be summed on B10, and B11..B19 should be summed on B20. I have tens such calculations to make. Every now and then, I add rows below the last summed row , and I want them to be added to the sum. I add a new row (call it 9.1) before row 10, and a new raw (let's call it 19.1) before row 20. I want B10 to contain the sum of B1 through B9.1 and B20 to contain the sum of B11:B19.1.
On excel, I have the offset function which does it like charm. But how to do it with google spreadsheet? I tried to use formulas like this:
=SUM(B1:INDIRECT(address(row()-1,column(),false))) # Formula on B10
=SUM(B11:INDIRECT(address(row()-1,column(),false))) # Formula on B20
But on Google Spreadsheet, all it gives is a #name error.
I wasted hours trying to find a solution, maybe someone can calp?
Please advise
Amnon
You are probably looking for formula like:
=SUM(INDIRECT("B1:"&ADDRESS(ROW()-1,COLUMN(),4)))
Google Spreadsheet INDIRECT returns reference to a cell or area, while - from what I recall - Excel INDIRECT returns always reference to a cell.
Given Google's INDIRECT indeed has some hard time when you try to use it inside SUM as cell reference, what you want is to feed SUM with whole range to be summed up in e.g. a1 notation: "B1:BX".
You get the address you want in the same way as in EXCEL (note "4" here for row/column relative, by default Google INDIRECT returns absolute):
ADDRESS(ROW()-1,COLUMN(),4)
and than use it to prepare range string for SUM function by concatenating with starting cell.
"B1:"&
and wrap it up with INDIRECT, which will return area to be sum up.
REFERRING TO BELOW ANSWER from Druvision (I cant comment yet, I didn't want to multiply answers)
Instead of time consuming formulas corrections each time row is inserted/deleted to make all look like:
=SUM(INDIRECT(ADDRESS(ROW()-9,COLUMN(),4)&":"&ADDRESS(ROW()-1,COLUMN(),4)))
You can spare one column in separate sheet for holding variables (let's name it "def"), let's say Z, to define starting points e.g.
in Z1 write "B1"
in Z2 write "B11"
etc.
and than use it as variable in your sum by using INDEX:
SUM(INDIRECT(INDEX(def!Z:Z,1,1)&":"&ADDRESS(ROW()-1,COLUMN(),4))) - sums from B1 to calculated row, since in Z1 we have "B1" ( the 1,1 in INDEX(...,1,1) )
SUM(INDIRECT(INDEX(def!Z:Z,2,1)&":"&ADDRESS(ROW()-1,COLUMN(),4))) - sums from B11 to calculated row, since in Z2 we have "B11" ( the 2,1 in INDEX(...,2,1) )
please note:
Separate sheet named 'def' - you don't want row insert/delete influence that data, thus keep it on side. Useful for adding some validation lists, other stuff you need in your formulas.
"Z:Z" notation - whole column. You said you had a lot of such formulas ;)
Thus you preserve flexibility of defining starting cell for each of your formulas, which is not influenced by calculation sheet changes.
By the way, wouldn't it be easier to write custom function/script summing up all rows above cell? If you feel like javascripting, from what I recall, google spreadsheet has now nice script editor. You can make a function called e.g. sumRowsAboveMe() and than just use it in your sheet like =sumRowsAboveMe() in sheet cell.
Note: you might have to replace commas by semicolons
NOTE
After testing this answer, it will only work if the sum is in a different column due to a circular dependency error. Otherwise, the solution is valid.
It's a bit of algebra, but we can take advantage of Spreadsheets' lower right corner drag.
=SUM(X:X) - SUM(X2:X)
Where X is the column you are working with and X2 is your ending point. Drag the formula down and Sheets will increment the X2, thus changing the ending point.
*You mentioned that you had tens of such calculations to make. So in order to fit your exact need, we would subtract your last summation to get that "middle" range that we wanted.
e.g.
B1..B9 should be summed on B10, and B11..B19 should be summed on B20
Because of the circular dependency error mentioned earlier, I can't solve it exactly and put the sum on the same line, but this could work in other cases where the sum needs to be stored in a different column.
=SUM(B:B) - SUM(B9:B) //Formula on C10 (Sum of B1..B9)
=SUM(B:B) - SUM(B19:B) - B10 // Formula on C20 (Sum of B11..B19)
This is based on #PsychoFish, here is the solution:
=SUM(INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","")&"3:"&ADDRESS(ROW()-1,COLUMN(),4)))
Simply replace the "3:" for the row to start sum.
#PsychoFish is correct but cannot be dragged and copied since the column is literal and hard coded, and #Druvision was in the right direction but was wrong... basically ended up with the same issue of having to re-enter the ranges and then sliding the formulas over and over.
You guys are making this harder than you have to. I just leave a couple of empty rows above by "sum" row (you can format them to be filled with color or something to keep them from being inadvertently used), then just add your new rows just above those special rows.
Agree with what user7255446 said that everyone is overcomplicating. Keep one row blank before your sum row. And then whenever you want to insert a new row, click on your blank row and use "Insert row ABOVE" instead of "insert row below". Your sum formula will automatically adjust.
Example: I want to sum from B1 to B19. I leave row 20 blank. In cell B21, put =SUM(B1:B20). Then if you ever need to insert a new row, click on row 20 and choose "Insert row above". The sum formula automatically changes to =SUM(B1:B21) for you. And of course your sum cell is now B22.
General syntax:
=SUM(INDIRECT(cell_reference_as_string1 &":"& cell_reference_as_string2)
with for example:
cell_reference_as_string1 = ADDRESS(ROW(),COLUMN(),4)
cell_reference_as_string2 = ADDRESS(ROW()-1,COLUMN(),4)
I like how #abernier describes the general solution. So far only alphabet-based A1 notation (A being first column, 1 being first row) are being used. It keeps confusing me, especially when thinking of number of columns left of another column. I like the number-based R1C1 notation much better. To use R1C1 notation for INDIRECT, you need to pass FALSE like so:
=SUM(INDIRECT("R1C"&COLUMN()&":R"&(ROW()-1)&"C"&COLUMN(), FALSE))
I hope you find that helpful, too.
OFFSET() can be used/abused for this purpose. Give it the absolute address of the top left of the range, 0 and 0 for the row/column offsets, and the height/width of the range. Let OFFSET() be the argument to SUM(), SUMIF(), etc.
ROW() and COLUMN() are handy when computing the desired height/width. Be sure to remember to subtract one to exclude the current row/column, or else you're liable to end up with a circular reference. If you have header rows/columns, subtract for them too.
For example, to sum everything from A2 down, excluding the current row, try:
=SUM(OFFSET($A$2,0,0,ROW()-2,1))
To sum everything to the left of the current cell, wherever it may be, try:
=SUM(OFFSET(INDIRECT("RC1",FALSE),0,0,1,COLUMN()-1))
Now let's flip things upside down, to show that this works in the other direction. Suppose you want to sum the B column, starting below the current row, until (and including) row #10. Try this:
=SUM(OFFSET($B$10,ROW()-9,0,10-ROW(),1))
You can avoid negative offsets, while still summing column B:
=SUM(OFFSET(INDIRECT("RC2",FALSE),1,0,10-ROW(),1))
Remove the "2" to instead sum the current column:
=SUM(OFFSET(INDIRECT("RC",FALSE),1,0,10-ROW(),1))
(Credit to Tom Sharpe, who commented above.) INDEX() can be used in a range expression. You might prefer this over OFFSET(), so I'm putting it here. The following sums everything from G1 down to the row above the current:
=SUM(G1:INDEX(G:G,ROW()-1))
Here's how I do it.
This formula does not require you to edit or enter anything about the particular column you would like to sum
=SUM(INDIRECT(CONCATENATE(address(1,column(),4),":",LEFT(address(1,column(),4),1))&ROW()-1))
The answer by #PsychoFish led me in the correct way.
The only issue that I had to rewrite the formula again from each column and each sum. So here is the improved formula, which sums the previous 9 cells on the same column, without hardcoding the column or row numbers:
=SUM(INDIRECT(ADDRESS(ROW()-9,COLUMN(),4)&":"&ADDRESS(ROW()-1,COLUMN(),4)))
The only issue is that I had to rewrite the formulas if someone adds or deletes a row. In this case I should change 9 to 10 or 8 corrspondingly.