Get index of second match in Google Sheets - google-sheets

In a sheet made of names and scores, I am trying to build a sheet to display the name of the people with the best scores.
To do so:
I sort the scores
I get the index of the biggest value
I offset the names list with the given index
When I want to get the second biggest value, I only have to get the index of the second biggest value on step 2.
There is a problem if two values are tied for the biggest, as MATCH() will always give me the index of the first value found.
I thought of determining the index of the biggest value, then excluding this index from the range used to determine the second biggest value, but I could not achieve it as the range lengths may be different.
I also thought of using a function or script that returns the Nth index that meets a criteria from a range, but I did not find anything to do so.
Image
Here is an example spreadsheet
https://docs.google.com/spreadsheets/d/1RrUpAjbMBze9L5OqxdyEWBnYXq98LtohdgROF8s68FI/edit?usp=sharing

One way is to add a column number and sort on that as well as on the score, then take the second element in the list:
=ArrayFormula(index(sort(transpose({B1:F3;column(B1:F3)}),3,false,4,true),2,1))
Note that the headers (players' names) are sorted along with their scores.
EDIT
Actually Sort in GS is a stable sort (in other words, according to the documentation 'range is sorted only by the specified columns, other columns are returned in the order they originally appear') so this is sufficient:
=ArrayFormula(index(sort(transpose(B1:F3),3,false),2,1))

Related

VLOOKUP alternative with less than or equal to operator and with the ability to use in an ARRAYFORMULA

The range Current!E5:E contains values which can be any random value. In range Configuration!D5:E I have a key and value list which I would like to get a result from.
I'd like for the range Current!G5:G to show the results. The key should be chosen if it is less than or equal to the user's credits.
I have included a Desired outcome worksheet to visualize my desired outcome.
Example spreadsheet
Delete everything from G4:G (including the header) and place the following formula in G4:
=ArrayFormula({"Credit earned points:";IF(E5:E="",,VLOOKUP(E5:E,SORT(FILTER(Configuration!D5:E,Configuration!D5:D<>""),1,1),2,TRUE))})
This will create the header (which can change within the formula if you like).
VLOOKUP looks up the credits within a flipped version of the 'Configuration' data and using TRUE as the final parameter, which will bump backward to the last value if an exact value cannot be found.

How to Sum lots of IF results, where SUMIF and SUMIFS cannot be used

Have a sheet with items which have data attributes, and may be used for multiple purposes.
There is a lookup table to lookup a score, based on the attributes.
So I can get the score for each item, see the top right section, and then sum that for each of the purposes. So purpose 1 has 11 data attribute points etc.
The score formula is included in the image for reference.
However, rather than copy all the data and score it, ideally would like a formula that can just go into a scoring column. Otherwise, with say 200 items, I need to have 201 columns just to score this one thing...
However, sumifs and sumif won't do this. What I really want is a "sum(foreach cell in range, do this formula)"
Does anyone know how this might be done?
Just on this mini example, you could use
=ArrayFormula(sum(if(C4:E4="yes",vlookup(C$2:E$2,$B$9:$D$11,match(C$3:E$3,$C$8:$D$8,0)+1,false))))
so you do a lookup on attribute 2 to find which column to do the lookup on attribute 1.

Get Row Value based on count

I have a google spreadsheet document and I am trying to list next person's turn. Essentially a user will enter a number and by the count of total numbers entered, the next person is shown.
Normally if I wanted to list a value from another cell I would type
=B2
However I need my number 2 to be dynamic, so something like:
=BCOUNTA(A2:A900)
Of course the above doesn't work, how do I get it to work?
EDIT: Found an answer with INDIRECT
INDEX have optional rows&columns offset.

Google Spreadsheet LOOKUP by Dynamic Value

I am trying to figure out if it is possible to use LOOKUP function that is based on a dyanmic value. For example:
=LOOKUP("A", C$2:C$1000, B$2:B$1000)
The above will look for Letter A in C Column and then write the value from the corresponding B Column row. What I wish to do is now read the Letter A from the D column, like so:
=LOOKUP(D2, Sheet2!C$2:C$1000, Sheet2!B$2:B$1000)
However, the above gives me an error. Is there a way to accomplish the above?
The above keeps returning me with an error saying value not found, I'm not sure what I am doing wrong.
EDIT
Ok so I have been playing more with this and I started having some very strange results. Lets take the following table: https://docs.google.com/spreadsheets/d/1ki3pmCOQoI1DLcbjEO-uwgwZGFfnHhM-fodspw8v1Qs/#gid=1001637055
Then my second sheet is this:
https://docs.google.com/spreadsheets/d/1ki3pmCOQoI1DLcbjEO-uwgwZGFfnHhM-fodspw8v1Qs/edit#gid=1993578337
If you look at the second sheet, bob shows up multiple times and I don't understand why.
Reason for #N/A's
lookup produces #N/A if it is looking up an empty range as is the case here. `'Draft Options'!A:A is empty so lookup cannot find a value to look up against.
Reason for multiple appearances
The documentation states that:
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.
If search_key is not found, the item used in the lookup will be the
value that’s immediately smaller in the range provided. For example,
if the data set contains the numbers 1, 3, 5 and search_key is 2, then
1 will be used for the lookup.
Now, you are looking up Text but the same thing applies, it needs to be ordered alphabetically.
This is because lookup is doing something called binary search
Since your data is unordered and (assuming you have column A filled with keys the following can happen which leads to weird results:
Lookup looks at the middle entry and checks if the value comes before or
after the lookup value in an alphabetically sorted list.
If it comes
before it checks the the entry int he middle between the beginning
of the list and the middle.
If your entry happens to be in the latter half lookup never finds this entry
or
This value might not exists so it picks the first value it can find that would come before that (assuming the data is sorted) or something (seemingly random) where lookup finds value n to be alphabetically after the lookup value and n+1 alphabetically before and returns value n+1.
You should only use lookup if you know the lookup values will be there and that they will be sorted. Otherwise you might want to take a look at VLookup or Index Match
Solution
If Column A in Sheet Draft options Actually contains those letters you are looking up you can use a simple VLookup:
=VLOOKUP(F2, `Draft Options`!$A$2:$B$1000, 2, FALSE)
Here FALSE specifies that VLookup shall not use a binary search algorithm and instead go through the list linearly (i.e. one by one) which is slower but will retrieve the first matching value or throw an error if it is not there instead of returning something odd.

How do I populate a cell based on multiple criteria in Google Sheets?

So here is the situation. I have one spreadsheet in Google sheets that has a column for the names of TV stations. I have a second column that lists airing times for ads. This is the format the date and times are in.
14-12-22 08:06:05
I have a second sheet that has the same column for TV station names. I also have a column that has a time range in the format
09:00-16:00
Then there is a third column for Rate.
What I am trying to do is add a Rate column to the first spreadsheet and populate that my matching up the TV Station name and the time range on the second sheet. My first thought was a VLOOKUP but I'm trying to match 2 conditions with the second one being a bit tricky since I am using an exact time vs a time range.
Any ideas?
As it is permitted to parse the time intervals I would recommend doing so (say with something like =SPLIT(A1,"-") since the results might then be arranged into a compact matrix such as shown in the image in ColumnsF:J. The differences in the rates for different stations at different times are readily apparent.
I have left the above in the same sheet as one with a representation of your other data since I (am lazy and) don't know the relevant sheet names anyway - but prefix the relevant sheet name (and !) to the column references in the formula that are later in the alphabet than C:
=vlookup(A2,F:J,match(C2,$G$1:$J$1,1)+1,0)
With extraction of the time element (into ColumnC) of your data (from ColumnA) the formula attempts to find the time from C in the first row, but accepts an inexact comparison by defaulting to the next lower value where there is no exact match. Once found, the MATCH() function returns the position of the match relative to the start of the range searched.
This is then used in a VLOOKUP() function to determine how far across to return the result of a search for the exact A column value in ColumnF.
Details of the syntax of the functions may be found via Help > Function list.

Resources