Look up value and return 1 row below - google-sheets

I've been trying to figure out how to find a value in a table and return the string below that.
Most solutions I've tried with index don't work, and I don't think the solution is through vlookup.
So to give an example, I want the formula to look at today() and return the value 1 or 2 rows below corresponding to that date (which row is dependent on the AM/PM, but I'm pretty sure I can set that up with Ifs)
Let's say it's 26-08, 1PM then the formula should return 'Eee' as value in the following table:
25-08-2022 26-08-2022 27-08-2022
Aaa Bbb Ccc
Ddd Eee Fff
28-08-2022 29-08-2022 30-08-2022
Ggg Hhh Iii
Jjj Kkk Lll

Give a try on below formula-
=INDEX(A1:C6, MAX(ROW(A1:C6)*(A1:C6=E1))+IF(F1="AM",1,2),MAX(COLUMN(A1:C6)*(A1:C6=E1)))

Related

Calculate Days between series of dates in a range

I wanted to calculate the difference of numbers that are inputted in a Google sheet row like this:
Row 1: 35 | 37 | 39 | 38 and So on (until last non-empty cell)...
And the output would be:
Row 1 Output: (37-35)+(39-37)+(38-39) = (2+2-1)=3
the suggested formula for this calculation is:
=LAMBDA(rg,SUMPRODUCT(rg-OFFSET(rg,0,-1)))(INDEX(1:1,2):INDEX(1:1,COUNTA(1:1)))
Taking forward to this concept, now I want to calculate days between dates instead of numbers.
something like this (date format:yyy-mm-dd):
Row 1: (blank cell) | 2022-07-06 | (blank cell) | 2022-07-07 | 2022-07-08 and So on (until last non-empty cell)...
Expected Row 1 Output:(2022-07-07-2022-07-06)+(2022-07-08-2022-07-07) = (1+1)=2
I used the above formula but I reckon formula is producing incorrect output because it does not exclude blank cells in between these dates, that's why it gives 44749 an an output,image is also attached:
Here is the how the table looks like in sample sheet in case you want to test it.
Desired Output (No. of Days)
Date1
Date2
Date3
Date4
Date5
Date6
3
2022-07-06
2022-07-07
2022-07-08
2022-07-09
(G2-E2)+(E2-D2)+(D2-B2)
1
2022-07-06
2022-07-07
(G4-C4)
Table is starting from column A to Column G. Any guidance would be much appreciated, thank you.
There are many ways of finding the first and last non-blank cell in a row, but let's use xlookup for the sake of argument:
=ArrayFormula(xlookup(true,(B2:2<>""),B2:2,,0,-1)-xlookup(true,(B2:2<>""),B2:2,,0,+1))
But people just don't believe me when I tell them that (G2-E2)+(E2-D2)+(D2-B2) simplifies to G2-B2. It's basic algebra folks!
Or this is equivalent to my original formula but doesn't lend itself to being written as an array formula:
=index(filter(B2:2,B2:2<>""),count(B2:2))-index(filter(B2:2,B2:2<>""),1)
EDIT
Possible array formula (but could be a bit inefficient):
=iferror(byrow(B2:Z,lambda(r,index(filter(r,r<>""),count(r))-index(filter(r,r<>""),1))))
Also if dates are in ascending order left to right (or more exactly, if the first date is the smallest and the last date the largest), and because min and max ignore blanks, you can simplify the original formula to:
=max(B2:2)-min(B2:2)
or with byrow:
=ArrayFormula(byrow(B2:Z,lambda(r,if(min(r)=0,,max(r)-min(r)))))

vlookup does not show date search

I am trying to vlookup a date in a range of text, numbers, dates. vlookup search for number days of a given date, tried to change the number to date, but it does not work.
my formula is =vlookup(date(2022,7,5),A:C,1,false) for the below table. and it should show value text5.
| A B C
---------------------------
1 | text1 7/1/2022 001
2 | text2 7/2/2022 002
3 | text3 7/3/2022 003
4 | text4 7/4/2022 004
5 | text5 7/5/2022 005
It shows #N/A and the Error message was Did not find value '44747' in VLOOKUP evaluation.
I thought it was days and I need to change it to date format. to be =vlookup(to_date(date(2022,7,5)),A:C,1,false). the same problem.
VLOOKUP() will not work in this case, because VLOOKUP() always search on first column and your first column is text field and dates are on second column. So, use INDEX()/MATCH() instead. Try-
=INDEX(A:A,MATCH(DATE(2022,7,5),B:B,0))
Also FILTER(), QUERY() functions will work in your case.

Comparing 2 columns for certain words

=COUNTIF(B4:B, "" & D4:D & "")
So column B4:B will have something like this
Joe Smith, STIM
and column D4:D would have something like this
Joe Smith 10/19/1999 AC
I am trying to make a True False statment in Col G that if column B has Joe Smith and Column D has Joe smith it will say yes or no. This will be a list of names that always change dynamically so one day B4 might be Joe Smith but the next day it could be Jan Doe. Column D will be a list of names in alpha order that changes all the time as well.
I was able to replicate what you need with the example provided by using:
=ArrayFormula(IF(D:D="",,REGEXMATCH(LOWER(D:D),FILTER(LOWER(I:I),I:I<>"")&"\b")))
You can see the results in my testing spreadsheet here.
At the moment I have limited the range from column I but you can just change it so it takes the whole column.
Just one little detail is that with this function you can only have 1 name in each row in the list of names.

Lookup a value in Column B or C and return key in Column A

I am trying to write a formula that will look for a value in a column, and return the first cell in the row in which it finds the value. So a little like VLOOKUP, but I don't want to search the the first column.
Here is an example dataset:
Room
Monday
Tuesday
DWG 1
S01
S02
DWG 2
S02
S04
DWG 3
S03
S06
DWG 4
S04
S07
Here is what I would like to generate using a formula.
So for the value at B2, I would like it to look up A2 ("S01") in the B column ("Monday") of the top table, and return the value of the cell in the 1st column ("DWG 1").
Ideally it would return nothing or a blank if it doesn't find the exact string in the top table.
Section
Monday
Tuesday
S01
DWG 1
S02
DWG 2
DWG 1
S03
DWG 3
S04
DWG 4
DWG 2
S05
S06
DWG 3
s07
DWG 4
After seeing your in-sheet data and layout, and meeting up with you there live, this is the formula I left for you in the newly added sheet "Erik Help":
=ArrayFormula({"S"&TEXT(SEQUENCE(24,1),"00"),IFERROR(VLOOKUP(FILTER(B1:1,B1:1<>"")&"S"&TEXT(SEQUENCE(24,1),"00"),SPLIT(FLATTEN(FILTER(Sheet1!B1:1,Sheet1!B1:1<>"")&FILTER(INDIRECT("Sheet1!B3:"&ROWS(Sheet1!A:A)),Sheet1!B1:1<>"")&"|"&FILTER(Sheet1!$A3:$A,Sheet1!$A3:$A<>"")),"|"),2,FALSE))})
For the understanding of others, the days of the week (i.e., Monday, Tuesday...) are entered manually as top headers in B1:F1. The header "Section" is entered in A2 with B2:F2 blank. (This is just how the OP wanted it set up.) And the formula is in A3, processing data for A3:F.
The first part of the virtual array just generates the SEQUENCE of section names (S01 - S24) in A3:A26.
The next part looks up every element of one array within another array. The first array is a concatenation of every weekday with every section number from Column A. The second array didn't technically need to be as long as it is in the formula, because we already know exactly how many weekdays, classrooms and sections there are. But it is written to accommodate flexibility, perhaps for future use where four or six days are required, with more or fewer sections.
That second array concatenates every weekday with every section from Sheet1 followed by a pipe symbol and the room for each row from Sheet1. That grid is FLATTENed to one column, and then SPLIT to two columns at the pipe symbol.
Found elements, then, return the class name (which was SPLIT to Column 2 of the virtual VLOOKUP array). If there is no match, IFERROR returns null.
The shorter version possible since we know exactly how many days, sections and rooms we have (and which I left in a new sheet called "Erik Help 2") is this:
=ArrayFormula({"S"&TEXT(SEQUENCE(24,1),"00"),IFERROR(VLOOKUP(Sheet1!B1:F1&"S"&TEXT(SEQUENCE(24,1),"00"),SPLIT(FLATTEN(Sheet1!B1:F1&Sheet1!B3:F17&"|"&Sheet1!A3:A17),"|"),2,FALSE))})
The function you're looking for doesn't exist in Sheets as Vlookup match is performed horizontally from left to right. However, a workaround is to rearrange the columns within the function QUERY and perform a Vlookup to it
Here's an example formula you can use =iferror((vlookup("S01",QUERY(A2:C, "Select B,A",0),2)),"")
This will also leave the cell blank if there are no matching results.
Here's an example of what the end result would look like when I the string "S01" is Vlooked up:

Vlookup on ID and Date (Fuzzy on Date) Google Spreadsheets?

I'm trying to essentially do a Vlookup on two columns ID (where I want the match to be exact) and a date field (where I want it to be fuzzy).
Suppose you have a table in "Sheet1" about hospital admissions like so:
ID Admit_Date
000 01/01/2016
000 06/01/2016
001 02/01/2016
002 04/01/2016
Then I have another table in "Sheet2" about followup care like so:
ID Followup_Date
000 01/05/2016
000 06/06/2016
001 02/02/2016
002 04/04/2016
In sheet 1 I want to return the corresponding Followup_Date which is the closest Date After the Admit_Date
So my final result will look something like
ID Admit_Date Followup_Date
000 01/01/2016 01/05/2016
000 06/01/2016 06/06/2016
001 02/01/2016 02/02/2016
002 04/01/2016 04/04/2016
What's the best way to do this using a formula in Google Spreadsheets?
Update
Get " Followup_Date", formula on Sheet1
You could use this formula:
=QUERY(SORT(FILTER(sheet2!B:B,sheet2!A:A=A2,sheet2!B:B>=B2)),"limit 1")
Original (wrong) answer
Get "Admit_Date", formula on Sheet2
try this formula:
=VLOOKUP(B2,(FILTER(sheet1!B:B,sheet1!A:A=A2)),1)
you'll need to paste it in cell C2 and copy it down.
This formula is doing:
filtering data, doing exact match sheet1!A:A=A2
doind vlookup, with default parameter, that gives the closest Date After the Admit_Date. This will work with numbers, and date is serial number in spreadsheets, so this will fit you.
If you like big ArrayFormulas, here's one, that you don't need to drag down, it'll expand automatically:
=ArrayFormula(RIGHT(VLOOKUP(VLOOKUP(OFFSET(A1,1,,counta(A2:A)),{UNIQUE(FILTER(A2:A,A2:A<>"")),row(INDIRECT("A1:A"&COUNTUNIQUE(A2:A)))*100000},2,0)+OFFSET(B1,1,,counta(A2:A)),VLOOKUP(OFFSET(sheet1!A1,1,,counta(sheet1!A2:A)),{UNIQUE(FILTER(A2:A,A2:A<>"")),row(INDIRECT("A1:A"&COUNTUNIQUE(A2:A)))*100000},2,0)+OFFSET(sheet1!B1,1,,counta(sheet1!A2:A)),1),5)*1)

Resources