Wrong results in Google spreadsheets Lookup functions (vlookup, hlookup, match) - google-sheets

The following spreadsheet demonstrates a very odd behaviour of Google Spreadsheets Lookup functions. I've used these functions many times without any problem. Can anyone confirm it happens on their side as well?
Example doc: https://docs.google.com/spreadsheets/d/16lRQ72K28CtObY_ChzpNQUVTl_EgbjEyRcpP5QOZKzE/edit?usp=sharing

By Default VLOOKUP does approximate matches. #Pnuts explained that the desired result is not being returned in every case because the the search is binary.
Excels optional parameter for VLOOKUP is called range_lookup and quote:
range_lookup Optional. A logical value that specifies whether you
want VLOOKUP to find an exact match or an approximate match:
If range_lookup is either TRUE or is omitted, an exact or approximate
match is returned. If an exact match is not found, the next largest
value that is less than lookup_value is returned.
IMPORTANT If range_lookup is either TRUE or is omitted, the values
in the first column of table_array must be placed in ascending sort
order; otherwise, VLOOKUP might not return the correct value.
For more information, see Sort data in a range or table.
If range_lookup is FALSE, the values in the first column of
table_array do not need to be sorted.
If the range_lookup argument is FALSE, VLOOKUP will find only an exact
match. If there are two or more values in the first column of
table_array that match the lookup_value, the first value found is
used. If an exact match is not found, the error value #N/A is
returned.
Google's optional parameter for VLOOKUP is called is_sorted and quote:
is_sorted - [OPTIONAL - TRUE by default] - Indicates whether the
column to be searched (the first column of the specified range) is
sorted.
If is_sorted is TRUE or omitted, the nearest match (less than or equal
to the search key) is returned. If all values in the search column are
greater than the search key, #N/A is returned.
If is_sorted is set to TRUE or omitted, and the first column of the
range is not in sorted order, an incorrect value might be returned.
If is_sorted is FALSE, only an exact match is returned. If there are
multiple matching values, the content of the cell corresponding to the
first value found is returned, and #N/A is returned if no such value
is found.
If you need exact matching with VLOOKUP just add FALSE in the optional parameter to force the lookup of exact matching. If you are using the MATCH then add a 0.
So the formulas for your spreadsheet should look like:
=VLOOKUP(A2,A1:E13,5,FALSE)
=VLOOKUP("n1-standard-2",A1:E13,5,FALSE)
=MATCH(A2,A1:A13,0)
=MATCH("n1-standard-2",A1:A13,0)
=HLOOKUP(A1,A1:E13,5,FALSE)
=HLOOKUP("n1-standard-1",A1:E13,5,FALSE)

Related

How can I find the name of the person ranked 1?

What I'm trying to do is find the name of the person who is ranked number 1 in the table shown below. I have tried =LOOKUP and =VLOOKUP but I get an error saying that a result can't be found, even though it's obviously there. I assume that I'm either using the wrong function or just not using it right.
I tried =VLOOKUP(1;D2:H19;1) and =LOOKUP(1;D2:H19;1) but neither seems to work.
Answer
The following formula should produce the behaviour you desire:
=INDEX(D2:D,MATCH(1,H2:H,0))
Explanation
=VLOOKUP can only be used to find values to the right of a search key. To find values to the left of a search key, use a combination of =INDEX and =MATCH.
The =MATCH function searches a specified range for a specified value and returns the relative position of the value in that range. In this case, the value to search for is 1, the range to search through is H2:H, and the 0 at the end specifies that the range is not sorted in any way.
The =INDEX function returns the contents of a cell within a range having a specified row and column offset. In this case, the range is D2:D, and the row is whichever row is returned by =MATCH. =INDEX could take a third argument if it was desired to specify a row offset as well, but that is not necessary here.
Functions used:
=INDEX
=MATCH
You sort your ascending order based on rank then return your desired data using INDEX() function. Try-
=INDEX(SORT(D2:H500,5,1),1,1)
=vlookup(1,{H2:H19, D2:D19},2)
Since vlookup only searches the 1st column of the input range, to use it, you need to flip the columns by composing a local array: {H2:H19, D2:D19}.
{} means concatenation. , in it means horizontal concatenation. With that, the rank column is now the 1st column in the input of vlookup and now vlookup works.
With our local array, the 2nd column are the names and therefore index should be 2.
Also note the use of comma to separate function inputs.
your VLOOKUP formula should look like:
=VLOOKUP(1, {H2:H19, D2:D19}, 2, 0)
also try just:
=FILTER(D:D; H:H=1)
or:
=SORTN(D:D; 1; 1; H:H; 1)
You can use query (usefull in case of ex aequo)
=query(D2:H,"select D where H=1",0)

Why is my INDEX/MATCH function returning a wrong value?

I am currently trying to make a sequence number generator in Google Sheets, and everything was going smoothly.
However, in the final steps, I noticed that one of my functions was returning the wrong value.
As you can see in C9, it returns the AMS20-00001 value despite BBAS2 is not present in A12:A13.
I noticed this happened with some of my previous sheets before this, and it got me wondering what causes this? Shouldn't it return as #N/A or #ERROR instead?
I would be glad if someone could give me an explanation of why this happens as well as what can I do to fix it.
Applies to Google Sheets and Microsoft Excel
There are three arguments in MATCH
Lookup value
Lookup array
Match type
MATCH(Lookup Value, Lookup Array, [Match Type])
Match type is optional and accepts one of three values
1 = exact or next smallest (default)
0 = exact match
-1 = exact or next largest
You've omitted the match type in your formula...
MATCH(A9,$A$12:$A$13)
This is the same as using the the default match type...
MATCH(A9,$A$12:$A$13,1)
The exact match was not found so your formula returned the nearest value that is less than the lookup value
Using 0 forces the exact match
MATCH(A9,$A$12:$A$13,0)
In your example, this results in the expected error
=INDEX($E$12:$E$13,MATCH(A9,$A$12:$A$13,0))
use vlookup:
=ARRAYFORMULA(IFNA(VLOOKUP(A8:A10, A12:E, 5, 0)))

VLOOKUP formula cannot read dataset generated by another formula

I am using Google Sheets and the VLOOKUP formula within it. Why does the following formula return a 0?
Columns C2:Q150 are generated from a formula and number's pulled from other sheets. Columns U4:U18 are generated by using the LARGE function (to find max in a sorted manner)
According to the documentation you posted, Google Sheet's VLOOKUP function works in the same way as Excel's.
So you are looking in the range $C$2:$C$150 (the first column of your table, which you specified as $C$2:$Q$150) to find the last value in that column that is less than or equal to your 36, i.e. the value in cell U4, and then returning the value in the first column of your $C$2:$Q$150 range on the found cell's row.
If the last cell in column C contains a zero, the answer will be 0.
Re the fourth parameter (which you are setting to TRUE), the documentation says in its Notes:
If is_sorted is set to TRUE or omitted, and the first column of the range is not in sorted order, an incorrect value might be returned. If VLOOKUP doesn’t appear to be giving correct results, check that the last argument is set to FALSE. If the data is sorted and you need to optimize for performance, set it to TRUE. In most cases it should be set to FALSE.
For excel try this in V4,
=INDEX(A:A, AGGREGATE(15, 6, ROW($2:$20)/(C$2:Q$20=U4), COUNTIF(U$4:U4, U4)))
Fill down as appropriate.
In google-sheets this translates to,
=index(A:A, small(if(C$2:Q$20=U4, row($2:$20)), countif(U$4:U4, U4)))

lookup function doesn't work with unsorted list

Why does this work, and looks up values correctly
but once i change the order of values, it produces incorrect values?
If you read the notes on the LOOKUP function, it says:
The LOOKUP function will only work properly if data in search_range or search_result_array is sorted. Use VLOOKUP, HLOOKUP, or other related functions if data is not sorted.
Change your formula to use VLOOKUP as follows:
=VLOOKUP(D3, A1:B6, 2, FALSE)
Syntax:
VLOOKUP(search_key, range, index, [is_sorted])
search_key - The value to search for. For example, 42, "Cats", or I24.
range - The range to consider for the search. The first column in the range is searched for the key specified in search_key.
index - The column index of the value to be returned, where the first column in range is numbered 1.
is_sorted - [OPTIONAL - TRUE by default] - Indicates whether the column to be searched (the first column of the specified range) is sorted.

VLOOKUP alternatives

I am trying to do a VLOOKUP but its just not going to cut the mustard. There is no way I can alphabetize the first column in my list.
What I am wondering is there any alternatives or combinations of functions that I could use along VLOOKUP in the case that the list can not be alphabetized?
From the link in your comment on your OP:
Range_lookup A logical value that specifies whether you want
VLOOKUP to find an exact match or an approximate match:
If TRUE or omitted, an exact or approximate match is returned. If an
exact match is not found, the next largest value that is less than
lookup_value is returned.
The values in the first column of
table_array must be placed in ascending sort order; otherwise, VLOOKUP
may not give the correct value. You can put the values in ascending
order by choosing the Sort command from the Data menu and selecting
Ascending. For more information, see Default sort orders.
If FALSE, VLOOKUP will only find an exact match. In this case, the
values in the first column of table_array do not need to be sorted. If
there are two or more values in the first column of table_array that
match the lookup_value, the first value found is used. If an exact
match is not found, the error value #N/A is returned.
It isn't terribly obvious the way that document is formatted, but the qualification that the lookup range must be sorted ascending only applies for when the 4th argument in VLOOKUP is TRUE, or a non-zero positive number, or omitted. (If the 4th argument is a non-zero negative number, the first column must be sorted descending.) In these cases, you are allowing the formula to return a non-exact match.
If you are looking for an exact match only, then you must specify FALSE (or zero) for the 4th argument, and the first column needn't be sorted at all.
If you are looking for an non-exact match on an unsorted range, then something like this should work in Google Sheets (assuming you are looking for a match of C1 in column A, and returning the corresponding entry in column B):
=INDEX(SORT(FILTER(A:B;LEN(A:A));1;1);MATCH(C1;SORT(FILTER(A:A;LEN(A:A));1;1));2)
DGET - there are a bunch of database style functions in google spreadsheets, I suspect DGET is the one you want.
Plain Text: With vlookup 1 and "1" are not the same. If you format the column as plain text it can do the lookup. This works even if your cell has a formula that produces a number.

Resources