Google Sheets VLOOKUP/IF - google-sheets

Plain english: Output 1 (yes) if the same ID# has more than one entry in the sheet within 5 minutes.
Example sheet, feel free to change anything: https://docs.google.com/spreadsheets/d/184jhnAmu63HSYl69prnAP7xo7j2PFvzjrWgDS4zj7dU
My thought is to get VLOOKUP to output something with an IF statement. But could also VLOOKUP in column E, IF calculation in column F?
VLOOKUP: input column D, output 1 in column e when input value is between 0 and 0.00347222221898846 (5 minutes reformatted). Output 0 if >0.00347222221898846.
Then I was trying to have an IF statement parse column A so that if the same value in A is present within the last 5 minutes. But I can't really figure out the IF statement.
Something like: time elapsed is less than 5 minutes (0.00347222221898846) AND that ID# was a previous entry (within the last 5 minutes). In some instances, another ID# will be the previous entry, but still within 5 minutes, so the IF can't just look up one row.

Well, I ended up just using IF/AND statements and a few extra calculation columns, see example sheet.
If anyone has any more efficient methods, let me know.

Related

Find column with specific value, then get average of cells below

My Google Sheets has sheets that look something like this
Sheet1:
Date
Object 1
Object 2
Group A
Any Date
1
3
Group B
Any Date
2
4
Sheet2:
Date
Object 2
Object 5
a
b
Group C
Any Date
5
6
Now what I want is a formula for any a and b, that checks which object it belongs to, and gets all values, in the last 6 months, in the column of that object across all specified sheets, obviously without including itself.
In this case, a would be the average of 5, 3 and 4, so 4. Because 5 is in the same column as a, and 3 & 4 are in the column with object 2, which is the object a is in.
Basically because a is in the object 2 column, I want the average of all values of object 2.
In the case of b however, 6 should be the result because there is no object 5 in the other sheet (it may be in other sheets though) and therefore it takes the average of just 6.
Sheet1 does not care what is in Sheet2, there is at least one other Sheet, aside from Sheet1, that Sheet2 will get its values from.
Currently Im doing it manually, but for any change I have to check all sheets for cells that that would be affected. That would take too much time in the future though, as the amount of data will increase.
The formula for now looks like this:
=(SUMIF($D6:$D;">=" & edate(today();-6); S6:S) + SUMIF('Sheet1'!$D6:$D;">=" & edate(today();-6); 'Sheet1'!S6:S))/(COUNTIF($D6:$D;">=" & edate(today();-6)) + (COUNTIF('Sheet1'!$D6:$D;">=" & edate(today();-6))))
In my sheets column D has the Dates, and starting with Column S comes the Data. a and b are all in the first 5 rows, so they are never included.
This can probably be done with a Query or Arrayformula, but Im not good enough with those. I was at most able to recreate the edate portion of the formula.

ArrayFormula & Vlookup: exclude the current row from Vlookup range

I'm building a simple booking system using GoogleSheets.
Each entry (Row) is a "booking request".
The intervals being entered on the sheet (via a form) are fixed to 1, 2 or 3 hours (which makes this a "simple booking system"). I'm validating these "booking requests" based on "overlaps" in Start & End Time in a couple separate columns L, M, N.
Sheet Data
The Formula
IF(ROW($A:$A)=1,
"CheckEndDateTimes (1 hour after Start)",
IF(ISERROR(VLOOKUP($E1:$E+1/24,$G$2:$G,1,FALSE)),
"",
"overlaps 1 hour ahead of start"
)
)
)
I'm using ArrayFormula, such that it auto-updates Sheet data as entries are added via a form. I'm using VLOOKUP because it seems to work well with ArrayFormula (as opposed to Index/Match).
The generalized issue:
Column E contains the search_key values (i.e. $E1:$E) (Start Datetime).
Column G contains the range (i.e. $G:$G) (End Datetimes), which I am using to compare intervals (1, 2, 3 hours) ADDED to the search_key value (Column E)
SO far this works fine, EXCEPT that the Row that is being evaluated ALWAYS evaluates to an overlap; clearly, the mere existence of the End Time in the Row being evaluated is always resulting in an overlap.
My Question
Is there a way I can EXCLUDE the current row from the range being evaluated in the VLOOKUP function. (I tried adding "<>", as follows, but it results in NO matches being found:
VLOOKUP($E1:$E+1/24,$G$2:$G<>$G1:$G,1,FALSE)
Or even (to test),
VLOOKUP($E1:$E+1/24,$G$2:$G<>$G$4,1,FALSE)
Any help would be appreciated, Thank you kindly. :)
On reflection I think you need to use countifs to check for any rows with a matching overlapped time and with a row number not equal to the current row like this:
=ArrayFormula(if(A2:A="",,countifs(C2:C,A2:A+1/24,row(C2:C),"<>"&row(A2:A))))
Then you can test whether the result is non-zero and display a message:
=ArrayFormula(if(A2:A="",,if(countifs(C2:C,A2:A+1/24,row(C2:C),"<>"&row(A2:A)),"overlaps 1 hour",)))
and similarly for 2 and 3 hours.
BTW I don't think row three does overlap at one hour.

Google Sheets - IF Statement Formula

I need to write a formula that enters 'Y' if the patient is up to date with their immunizations based on their record and age, and 'N' if they are not. Age (D$) is in months and I have a column for every immunization (8 weeks(E$), 12 weeks(F$), 16 weeks(G$), 1 year(H$), 3 years and 4 months(I$)) which is marked as either ("Y" or colored Grey) as well as a final column which the formula is being entered into(J$). Any ideas what formula I need to use?
So far I have been thinking to use an IFS statement { =IFS(D$>=2, AND(E$="Y"),"Y")}. Although I can see the limitations to using this.
For every immunization, the patient is not up to date if the corresponding cell is blank and their age is older than the immunization date. You want to check this for multiple immunizations. You can use COUNTIFS to evaluate two (or more) conditions.
=IF(COUNTIFS(E3:I3,"",E$1:I$1,"<="&D3)>0,"N","Y")
Explanation:
COUNTIFS returns the number of shots for which the patient is not up to date.
IF returns N if the previous value is greater than 0, Y otherwise.
Note that I'm assuming the information on the number of months corresponding to each immunization (E$1:I$1 in the formula above) is present somewhere in the sheet or that you can add it. If this is not the case, you would indeed need to use IFS or something similar.

Google Sheets Query Group By / First-N-Per-Group

I'm trying to find a simple solution for first-n-per-group.
I have a table of data, first column dates and rest data. I want to group based around the date, as multiple entries per date are allowed. For the second column some numbers, but want the FIRST record.
Currently the aggregate function I could possibly use is MIN() but that will return the lowest value and not the first.
A B
01/01/2018 10
01/01/2018 15
02/01/2018 10
02/01/2018 2
02/01/2018 100
02/01/2018 20
03/01/2018 5
03/01/2018 2
Desired output
A B
01/01/2018 10
02/01/2018 10
03/01/2018 5
Current results using MIN() - undesired
A B
01/01/2018 10
02/01/2018 2
03/01/2018 2
It's a shame there isn't a FIRST() aggregate function in Google Sheets, which would make this a lot easier.
I saw a couple of examples of using the Row Number and ArrayQuery, but that doesn't seem to work for me. There are about 5000 rows of data so trying to keep this as efficient as possible, and not have to recalculate the entire sheet on any change, each taking a few seconds.
Currently I have this, which appends a third column with the Row Number:
=query({A1:B, arrayformula(row(A1:B))}, "select min(Col1),min(Col2) group by Col1")
Thanks
EDIT 1
A suggested solution was =SORTN(A:B,2^99,2,1,1), which is a clean simple one. However, this requires a large range of "free space" to display the returned dataset. Imagine 3000+ rows.
I was hoping for a QUERY() -based solution, as I wanted to do further operations with the results. Specifically, count the occurrences of distinct values.
For example: I wanted a returned dataset of
A B
01/01/2018 10
02/01/2018 10
03/01/2018 5
Yet I want to count the occurrences of those values (and then ignoring the dates). For example:
B C
10 2
5 1
Perhaps I've confused the situation by using numbers? the "data" in ColB is TEXT (short 3 letter codes), however I used numbers to show I couldn't use MIN() function as that returns the numerically lowest value.
So in brief:
Go through all rows (3000+ rows) and group by the FIRST row of a particular date
return the FIRST value of that row
COUNT() all unique occurrences of those FIRST values, disregarding the date. Just a list with the unique values and their count (again, only the first one of any particular day)
=SORTN(A:B,2^99,2,1,1)
If your data is sorted as in the sample, You can easily remove duplicates with SORTN()

How to use "if contains"?

I'm trying to make a scheduler for work and I have a dropdown list of the hours that the employees work in one column and I want it to display how many hours it is next to that. I.e.:
Column B (Selected from a drop down menu) Column C
6:00 - 14:30 to display 8 as it is an 8 hour shift
10:00 - 15:00 to display 5 as it is an 5 hour shift
Is there a way to do this?
So for the sake of clarity, I am going to develop this step by step, in several columns. These could be combined into one impenetrable formula, but that will not help you follow. You can do what I suggest here and then hide the columns with the calculation.
Suppose your time is in column A. You can do the following in the first row (mine assumes row 1, if you have headers, probably row 2) and then copy the formulas on down. In column B, I placed, =search("-",A1), which tells where the - sign is. In column C, I find the first time as a string with =left(A1,B1-2), which takes the first characters up to 2 before the dash. In column D I have =mid(A1,B1+2,5) which takes from 2 characters after the dash to the end (if it is only 4 characters long, it copies 4 not 5),and finally in column E we find the desired result, with =HOUR(timevalue(D1)-timevalue(C1)).
That does what you asked. If you wanted to add minutes you could use =MINUTE(timevalue(D1)-timevalue(C1)). Finally if a 22:00 - 6:00 graveyard shift existed, you would need to add logic for it.
You could also simplify the string calculation by in column B using the formula =split(A1,"-") and then putting =HOUR(timevalue(C1)-timevalue(B1))
And so if you really want a single formula, it could be =hour(INDEX((split(A1,"-")),2)-INDEX((split(A1,"-")),1)), which subtracts the first part from the second and converts to hours.
If in the course of time you want to handle the wrap around midnight, =iferror(hour(INDEX((split(A1,"-")),2)-INDEX((split(A1,"-")),1)),24-hour(INDEX((split(A1,"-")),1)-INDEX((split(A1,"-")),2))) should do the trick.
UPDATE: Sheets recognizes the times that resulted from the split as times. So if in B1 you place =split(A1,"-"), D1 can contain =C1-B1 if you are willing to keep the minutes. It even gives the right answer for 22:00 - 2:00.
Make a table with a column for the shifts (this could be the list used for the Validation, if you chose that method) and a column immediately to its right of the respective shift durations. I named that table Larry. Then in C2 (assuming your first dropdown is in B2):
=ArrayFormula(vlookup(B1:B,Larry,2,0))

Resources