How to make a condition inside a query function on sheets? - google-sheets

I've been trying to get a list of people who finish their work shift at an specific our, I have not had any problem with this since I've used just a query function:
=QUERY(A1:K48,"select A where B = '7:00 AM' OR C = 'Sunday' OR D = 'Sunday' OR K = TRUE",0)
So, if someone ends their work shift at 7:00 AM or someone rests on Sunday the formula will return these people's names.
Unfortunately, I'm struggling with the next step since those workers work an extra hour or half extra hour some specific days and I was not be able to include in the formula.
For instance, if today is Sunday and today is the Sean long day in which he has to work one extra hour I would like to this Query Formula doesn't includes the Sean's name at 7:00 AM instead of it I would like to include the Sean's name until 8:00 AM. To do this I add "not" in the query formula:
=QUERY(A1:K48,"select A where B = '7:00 AM' AND NOT E matches 'Sunday' OR C = 'Sunday' OR D = 'Sunday' OR K = TRUE",0)
To indicate that Sean is still working, and the formula shouldn't include his name, but now I don't know how to get Sean's name at 8:00 AM.
This is the sheet where I'm working on this Query: Sheet
https://docs.google.com/spreadsheets/d/1Vk95yM8On5nLlFljFerzDH4IG8dZkOgKyHzX36uUWOQ/edit?usp=sharing
To get a clearer idea: I'm getting a list based on column B, C and D values(normal shift days) but before of getting this result I would like to based the list on column E and column F data (long day and hour of the long day) and then if there is no data to take from the E and F column run the first formula.
I hope someone could help me with this I'll be so obliged. Thank you!

Related

EOMONTH returns the 1st day of the next month for months with 30 days

When I use the formula below the results of the EOMONTH function
returns the start of the next month for any month with 30 days instead of the last day of the specified month. The month and years are correct, so I'm pretty sure it's EOMONTH when used in another function.
For example,the results in B3 should be "11/31/1965" but it returns "12/1/1965".
=DATE(YEAR(B2),MONTH(B2)+6,DAY(TEXT(EOMONTH(MONTH(B2)+6,0))))
I have tried subtracting a day, but it returns the end-of-month -1 for months with 31 days (30). So I have the same problem in the other case.
I have also used IFS() to account for months with 30 days, and it miscalculates the date the same way.
=IFS( MONTH(B2)+6 = 4,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0))-2) ,
MONTH(B2)+6 =
6,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0))-2) ,
MONTH(B2)+6 =
9,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0))-2) ,
MONTH(B2)+6 =
11,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0))-2) ,
TRUE ,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0)) ) )
The EOMONTH function by itself where I just pass in the date as a string works correctly (column F).
Any Idea on what I'm doing wrong?
Thanks in advance.
Yes, as #player0 has explained, you can't just add something to a month and feed it into eomonth. Try putting
=eomonth(month(B2)+6,0)
into B3 (formatted as a date).
You get
1/31/1900
Why? month(b2)+6 gives 11 (which is just a number). Dates in google sheets are represented as days since 12/31/1899. So 11 formatted as a date gives 1/11/1900. Applying eomonth to that gives the last day of January 1900, which is the 31st. Feeding that into your formula would give 11/31/65, but that date doesn't exist, so you get 12/1/65.
If you want to go forward 6 months and then get the last day of the month, you need
=eomonth(date(year(B2),month(B2)+6,1),0)
You can also use the Edate function, which does not roll over into the first day of the next month:
=eomonth(edate(B2,6),0)
EOMONTH does not understand MONTH. instead, it converts it into date. to use EOMONTH you need to supply it with valid date
=EOMONTH(B2, 0)

Array Formula (return number of leave days taken if dates across multiple months) not working properly

I have an array formula that's not working exactly as I want.
The formula I have does not return correct value (returns FALSE) when leave dates are in the same month and month = current month
The formula is supposed to check for these conditions:
Col. E = Annual Leave
Col. J = Complete
and return the number of leave days taken by an employee for each month as per these conditions:
(let's assume Current month = Feb-2019)
a) IF Col. O = Yes AND Current month = Leave end date (means start date and end date are in the same month = current month)
THEN return number of leave days taken
b) IF Col. P = Yes (leave dates start in one month and end in another month)
(i) IF Current month = Leave start date THEN return number of days between leave start date and end of start date month
(ii) IF Current month = Leave end date THEN return total days taken (Col. H) minus number of days between leave start date and end of start date month
=ArrayFormula(iferror(if(($J$2:$J<>"Complete")*($E$2:$E<>"Annual Leave"),"",if($P$2:$P="yes",if(month($F$2:$F)=month(R$1),NETWORKDAYS.INTL($F$2:$F,EOMONTH($F$2:$F,0),1,Holidays!$B$2:$B),if(month($G$2:$G)=month(R$1),$H$2:$H-NETWORKDAYS.INTL($F$2:$F,EOMONTH($F$2:$F,0),1,Holidays!$B$2:$B),if(month($G$2:$G)=month(R$1),if($O$2:$O="Yes",$H$2:$H,0)))))),"x"))
The formula I have does not return correct value (returns FALSE) when leave dates are in the same month and month = current month
All other conditions are OK (i.e. if True, all other conditions are executed properly and correct value displayed except the above condition)
Link to google sheet
I've done some more research - and also based on the suggestion - I have managed to resolve this problem.
Here's the working formula:
=ArrayFormula(iferror(if((($J$2:$J="Complete")*($E$2:$E="Annual Leave")),
IF($P$2:$P="Yes",
IF(month($F$2:$F)=month(R$1),NETWORKDAYS.INTL($F$2:$F,EOMONTH($F$2:$F,0),1,Holidays!$B$2:$B),
IF(month($G$2:$G)=month(R$1),$H$2:$H-NETWORKDAYS.INTL($F$2:$F,EOMONTH($F$2:$F,0),1,Holidays!$B$2:$B),
IF(month($G$2:$G)=month(R$1)))),
IF((($O$2:$O="Yes")*(month($F$2:$F)=month(R$1))),$H$2:$H,"")),""),""))
you have 3 incomplete IFs which return falses...
=ARRAYFORMULA(IFERROR(
IF(($J$2:$J="Complete")*($E$2:$E="Annual Leave"),
IF($P$2:$P="yes",
IF(MONTH($F$2:$F)=MONTH(S$1), NETWORKDAYS.INTL($F$2:$F, EOMONTH($F$2:$F, 0), 1, Holidays!$B$2:$B),
IF(MONTH($G$2:$G)=MONTH(S$1), $H$2:$H-NETWORKDAYS.INTL($F$2:$F, EOMONTH($F$2:$F, 0), 1, Holidays!$B$2:$B),
IF(MONTH($G$2:$G)=MONTH(S$1),
IF($O$2:$O="Yes", $H$2:$H, 0), ))), ), ), ))

Google spreadsheet, comparing durations

I calculated a duration between two times, e.g. between 9:00 am and 11:00 am. So far so good. Now I need to decide if this duration is less 6 hours.
I do remember that this was pain in the s in excel but nevertheless I tried it the simple way:
=IF(E2 < 06:00:00; "y"; "n")
of course that didn't work. Next:
=IF(DURATION(E2) < DURATION(06:00:00); "y"; "n")
still, it didn't work.
So, okay, how can I compare two duration?
Divide hours by 24:
=IF(E2 < 6/24, "y", "n")
Value is E2 is a formatted time, actually 1 hour is 1/24, 1 day is 1.
Some info about date and time formats here:
http://www.excel-easy.com/examples/date-time-formats.html
You can also use the HOUR function if you want to
=if(HOUR(E2)<6,ʺyesʺ,ʺnoʺ)
or
=if(E2<time(6,0,0),ʺyesʺ,ʺnoʺ)
(if you write 06:00:00 in a formula it takes it as a string not a time)
but as I'm sure someone is about to point out, the first formula above gives the wrong answer for durations of more than a day (because it takes the hour part of a datetime).
What I find interesting is that you can assume for a worksheet formula that dates and times are represented as whole numbers (days) and fractions (parts of a day) just like in Excel. If you ever have to deal with them in Google App Scripts, you suddenly find that it's object-oriented and you have no choice but to use methods like hour() to manipulate them.
I needed to use the equivalent of:
=if(TIMEVALUE(E2)<6/24, "yes", "no")

Returning Modified Following business date while building Quarterly calendar

Trying to build a quarterly calendar of working business days (adjusted by holidays).
I am using =edate (A1,4), the only problem is that this function does not adjust for business days. Ideally I would wrap the formula using =workday(edate(A1,4),0,USD_Hols) but it seems that I cannot just add "0" for the formula to return the next business day.
Does anyone know how to get the next business day and also if next business day falls in the next month return the nearest previous business day?
Thanks
Paco
I think this works based on your description on comments. I'm not quite sure what you mean by "previous" in the comment. Below, I'm assuming that it means A1, if A1 is a workday, or the workday before A1 if A1 is not a workday:
=IF(MONTH(WORKDAY(A1,1,USD_Hols))=MONTH(A1),WORKDAY(A1,1,USD_Hols),WORKDAY(WORKDAY(A1,1,USD_Hols),0-1,USD_Hols))
I haven't used workday functions much. One thing I notice is that a 0 argument for days seems to return the date in A1 even if it's not a workday. That's why I included the nested IFs in the ValueIfFalse part of the main IF. It forces it to the next workday and then the one previous to that.
You must add the worksheetfunction to the Workday to Excel 2010
This is correct:
Function NextBiz(d As Range, holidays As Range) As Date
If WorksheetFunction.Weekday(d) = 7 Or WorksheetFunction.Weekday(d) = 1 Or WorksheetFunction.CountIf(holidays, d) > 0 Then
NextBiz = WorksheetFunction.WorkDay(d, 1, holidays)
Else: NextBiz = d
End If
End Function

How to utilize date add function in Google spreadsheet?

I believe the issue I am having now should be much easier in MS Excel. However, since my company uses Google Spreadsheet so I have to figure out a way.
Basically, I have a cell that contains a date value like "12/19/11", and I have another cell contains a value like "DT 30". The task assigned to me is to add the value 30(days) to the date, so the result should be "1/19/2012".
I did some trying in Google Spreadsheet, I have two questions. The first is to how to extract the numeric value "30" out of the string "DT 30", the second question is that, there seems to be no date add function built in Google Docs.
Could any experts offer some suggestions?
I like to keep it simple. If A1 holds the date and B1 holds the number of months to add, then
=date(year(A1),month(A1)+B1,day(A1))
would calculate the required result. The same way could be used for days or years
To extract a numeric value out of your string you can use these 2 functions (Assuming you have your value in cell 'A1'):
=VALUE(REGEXEXTRACT(A1, "\d+"))
This will get you a numeric value.
I've found no date add function in docs, but you can convert your date into internal date number and then add days number (If your value is in cell 'A2'):
=DATEVALUE(A2) + 30
I hope this will help.
You can just add the number to the cell with the date.
so if A1: 12/3/2012 and A2: =A1+7 then A2 would display 12/10/2012
You can use the DATE(Year;Month;Day) to make operations on date:
Examples:
=DATE(2013;3;8 + 30) give the result... 7 april 2013 !
=DATE(2013;3 + 15; 8) give the result... 8 june 2014 !
It's very surprising but it works...
The direct use of EDATE(Start_date, months) do the job of ADDDate.
Example:
Consider A1 = 20/08/2012 and A2 = 3
=edate(A1; A2)
Would calculate 20/11/2012
PS: dd/mm/yyyy format in my example
As with #kidbrax's answer, you can use the + to add days. To get this to work I had to explicitly declare my cell data as being a date:
A1: =DATE(2014, 03, 28)
A2: =A1+1
Value of A2 is now 29th March 2014
Using pretty much the same approach as used by Burnash, for the final result you can use ...
=regexextract(A1,"[0-9]+")+A2
where A1 houses the string with text and number
and A2 houses the date of interest
what's wrong with simple add and convert back?
if A1 is a date field, and A2 hold the number of days to add:
=TO_DATE((DATEVALUE(A1)+A2)
=TO_DATE(TO_PURE_NUMBER(Insert Date cell, i.e. AM4)+[how many days to add in numbers, e.g. 3 days])
Looks like in practice:
=TO_DATE(TO_PURE_NUMBER(AM4)+3)
Essentially you are converting the date into a pure number and back into a date again.
In a fresh spreadsheet (US locale) with 12/19/11 in A1 and DT 30 in B1 then:
=A1+right(B1,2)
in say C1 returns 1/18/12.
As a string function RIGHT returns Text but that can be coerced into a number when adding. In adding a number to dates unity is treated as one day. Within (very wide) limits, months and even years are adjusted automatically.

Resources