Find the last value of a matching column in Google Spreadsheets - google-sheets

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, )))<>"")))

Related

How can I get the last numerical value value in a column in Google Sheets?

I need to find the last numerical value in a column. I was using this formula to get the last value in column G, but I made some changes and it no longer works: =INDEX(G:G, COUNTA(G:G), 1). My column now looks like this:
645
2345
4674.2345
123.1
"-"
"-"
"-"
...and the formula returns "-". I want it to return 123.1. How can I do this?
There are many ways to go about this. Here is one of them:
=QUERY(FILTER({G:G,ROW(G:G)},ISNUMBER(G:G)),"Select Col1 ORDER BY Col2 Desc LIMIT 1")
FILTER creates a virtual array of only numeric values in G in the first column and the row of those numeric values in the second column.
QUERY returns flips the order by row number and returns only the new top value from the first column (which winds up being your last numeric value in the original range).
However, if your numeric values start at G1, and if there are only numeric values up to where you start adding hyphens in cells, you could just alter your original formula like this:
=INDEX(G:G,COUNT(G:G))
This would work because COUNT only counts numeric values while COUNTA counts all non-null values (including errors BTW).
Not to take anything away from the accepted answer, but I've been working on this a bit lately in relation to this for the never-ending last row discussion and thought I'd share some potential similar solutions. These ideas are inspired by a pattern of google sheet array questions that seem to be coming up more often. I am also intentionally using different ways to do the same thing just to give people some ideas (i.e. left and Regex).
Last Row that is...
Number: =max(filter(row(G:G),isnumber(G:G)))
Text: =max(filter(row(G:G),isText(G:G)))
An error: =max(filter(row(G:G),iserror(G:G)))
Under 0 : =max(filter(row(G:G),G:G<0))
Also exists in column D: =max(filter(row(G:G),ISNUMBER(match(G:G,D:D,0))))
Not Blank: =max(filter(row(A:A),NOT(ISBLANK(A:A))))
Starts with ab: =max(filter(row(G:G),left(G:G,2)="ab"))
Contains the character !: =max(filter(row(G:G),isnumber(Find("!",G:G))))
Starts with a number: =max(filter(row(G:G),REGEXMATCH(G:G,"^\d")))
Only contains letters: =max(filter(row(G:G),REGEXMATCH(G:G,"^[a-zA-Z]+$")
Last four digits are upper case: =Max(filter(row(G:G),REGEXMATCH(G:G,"[A-Z]{4}$")))
To get the actual value (which I realize was the actual question), just wrap an index function around the Max function. So for this question, a solution could be :
=Index(G:G,max(filter(row(G:G),isnumber(G:G))))

Troubleshooting formula with array - array arguments are of different size to EQ

In Google Sheets, I have a formula that displays the value of an item in a row if one of its cells contains any of the values listed in a different sheet. It looks like this:
=ARRAYFORMULA(IF(OR(L2 = ZRSKUs!$A$1:$Z$12005), O2, "0"))
If L2 contains any of the values in sheet ZRSKUs, this formula displays the value of the item, which is held in O2. If I drag the formula down it produces the value of every column and I can then get a SUM of this column. I wanted a way to do this without having to drag the formula down every single row (this spreadsheet has about 20,000 rows so it takes a long time to do). I also wanted the formula to add it up too, so it is all done in one cell.
I tried editing the formula to do this, and this is what I came up with:
=ARRAYFORMULA(SUM(IF(OR($L3:$L = ZRSKUs!$A$1:$A$500), $O3:$O, "0")))
However, this gives me an "Array arguments to EQ are of different size" error. I tried adjusting the number of rows in the ZRSKUs sheets so it had the exact same number as my other sheet, but this made no difference.
I'm not sure what's going wrong, so any help or advice would be greatly appreciated!
You get the error because that is not a well-formed array formula, as $L3:$L and ZRSKUs!$A$1:$A$500 are not equal in length. We could rectify this by using another function for the lookup, in this case, MATCH:
=ARRAYFORMULA(SUM(IF(ISNA(MATCH($L$1:$L, ZRSKUs!$A$1:$A$500, 0)), 0, $O$1:$O)))

Index match find closest value with multiple search criteria

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.

Google Sheet - Transform two columns into one column using arrayformula (more than 50,000 characters)

I'm using Google Sheets and looking for an arrayformula that able to take a list in two columns and arrange it alternately in one column. The sheet contains about 5,000 rows, each row has more than 35 characters.
I tried this:
=transpose(split(join(" ", query(transpose(B5:C),,50000)), " "))
But then I got this message:
Please take a look at the sheet here:
https://docs.google.com/spreadsheets/d/11T1Roj1trviOSiiTZS292-4l3oODid7KLi9oGz3Z66o/edit#gid=0
Assuming your 2 columns are A and B, this formula will "interlace" them:
=query(
sort(
{arrayformula({row(A1:A3)*2, A1:A3});
arrayformula({row(B1:B3)*2+1, B1:B3})}
),
"select Col2")
Explanation, unwrapping the formula from the inside:
Each value gets a unique number, based on its row number times 2 (+1 for the 2nd column)
Everything is sorted based on this number
Only the 2nd column is extracted for the result.
There is a function for this called FLATTEN().
This works perfectly as a general solution since it takes an array of any size and outputs the items in the order they appear left-right-top-down (See here).
It can be combined with TRANSPOSE() to accomplish the same thing but in the horizontal case, and if needed blank cells can be omitted with FILTER().
EDIT:
My sincere apologies, I did not read the question carefully enough. My response is incorrect.
This should work:
={B5:B12;C5:C12}
just be careful to NOT change it to
={B5:B;C5:C}
This will start an infinite loop where the spreadsheet will increase the amount of rows in the spreadsheet to allow this output column to expand, but in doing so increases the length of the 2 input columns, meaning the length of the output column increases even more, so the spreadsheet tries adding more rows, etc, etc. It'll make your sheet crash your browser or something each time you try to open it.
In Row5:
=ArrayFormula(offset(B$5,INT((row()-5)/2),iseven(row())))
Would need to be copied down however.

Getting Corresponding Cell In Google Sheets?

I have a Google sheet for tracking my weight. I have two columns: Date and Weight. While the goal is to have the weight column sorted in descending order, that doesn't always happen in reality...
The data essentially looks like this (weights changed to far lower values, of course):
Date |Weight
04/01/10|195
04/02/10|194
04/03/10|190
04/04/10|198
etc.
Anyway, I have a cell in another spot on the sheet that shows the minimum value from the weight column using this formula
=(Min(B:B))
What I would like to do is display the corresponding date cell for whatever the minimum value from the weight column is. So, with this dataset, I want to show 190 for weight and 04/03/10 for date. Is there any way to get that corresponding cell? I looked through the function reference for Google docs, but can't get anything going. I tried using some of the functions from the Lookup category, but got nowhere. Most want a cell reference, but the min() function returns a value. I think I need to somehow get min() to give me a cell reference, but I don't know how to do that. HLOOKUP() sort of seemed like it might be appropriate, but the docs were a bit spotty, and it didn't do anything but error out the cell.
Of course, I may be barking up the wrong tree entirely.
Thoughts?
I would use the following two formula's:
MIN(B2:B)
FILTER(A2:A;B2:B=minimal value)
If there are more results, they need to be included as well.
See example file I've created: Getting Corresponding Cell In Google Docs Spreadsheet?
Not barking up the wrong tree, actually very close:
=index(A:A,match(min(B:B),B:B,0))
should meet your requirement.
Working inside out: min(B:B) (as you had) returns the lowest weight (ie 190) in ColumnB.
match then finds the position of that value relative to the start of the range in which the value is searched for. So assuming Date is in A1, that returns 4, being the fourth row in ColumnB, where 190 is. The 0 in the formula is to ensure that only the position of an exact match is returned.
Now we know we need the content of the fourth row we can go looking for the value there in ColumnA with index, returning 04/03/2010.
Not all is ideal however. It is possible that a weight of 190 was achieved on separate days. It is the nature of match that where an exact match is required and found the function stops looking for any further instances. Hence as things stand 04/03/2010 will be returned for 190 however often that is the weight for a day after 04/04/2010 - unless other steps are taken, such as to delete/remove/ignore data from 04/03/2010.
You need to change the order of the column as the search column (the weight should be the first in the search array. Then you can use the VLOOKUP formula:
=VLOOKUP(C7,A:B,2,false)
C7 holds the MIN formula that you used: =(Min(A:A)) - note the column order change
one-cell solution to get minimal value with the latest day:
={MIN(B:B), TO_DATE(VLOOKUP(MIN(B:B), SORT({B:B,A:A}, 2, 0), 2, 0))}
to get all minimal values with dates:
=QUERY(A:B, "select B,A where B matches '"&MIN(B:B)&"' order by A desc", 0)

Resources