How to use TEXT inside a QUERY in Google Sheets - google-sheets

I am trying to create a simple daily time recorded in google sheets. I have created a Google Form named "Time In Records". Each time a user submit a record using that form, it records their name and the time they inputted, as well as the automatic timestamp.
On my other Sheet, which is named "DTR Summary", I am extracting the data from the "Time-In Column" of the "Time In Records" Sheet. I use this code to do that:
=QUERY('TIME IN RECORDS'!$A$2:$E, "select C where E = '"&TEXT($C15,"dd/mm/yyyy")&"' and (B = '"&$A$2&"')")
Basically, I am taking the "Time-In" of a user based on their name(A2) and the date(C15). This works fine, the problem is that I have to manually do this code on the "Time-In Records" Sheet every time a new user submit a data. (The code below is inputted on column E of the "Time-In Records" Sheet) because every time a new data is added, the column E is left blank so I have to manually drag the first cell to the last added data.
=TEXT($A2,"dd/mm/yyyy")
What I would like to happen is to automatically convert the timestamp into a date so that it will match my QUERY. I thought of something like this:
=QUERY('TIME IN RECORDS'!$A$2:$E, "select C where text(A, "dd/mm/yyyy") = '"&TEXT($C15,"dd/mm/yyyy")&"' and (B = '"&$A$2&"')")
Sadly it does not work. I would appreciate any help. Thanks!

For all who are having the same concern, I have figure this out.
On the "Time-In Records" Sheet, I've put this code to column E:
=ArrayFormula(IF(A2:A<>"",TEXT($A2:A,"dd/mm/yyyy"),""))
This automatically converts the timestamp column into date, the current and newly added data.

formula syntax should be:
=QUERY('TIME IN RECORDS'!A2:E,
"select C
where E = date '"&TEXT(C15, "yyyy-mm-dd")&"'
and B = '"&A2&"'")

Related

Match Value based on corresponding dates

I have the spreadsheet attached.
I'd like to find Client No from lookup sheet based on the date provided in the live sheet.
The same client can appear with a different client number, so i need to lookup the name and date (from live sheet) and find the corresponding client number in the lookup sheet where the date from live sheet falls between the 2 dates on the lookup sheet.
I hope this makes sense.
Any help appreciated.
Thank you
This might do what you're looking for.
=IFERROR(
QUERY(SORT(FILTER(Lookup!A$2:D,Lookup!C$2:C=B2,Lookup!A$2:A<=A2),1,0),
"SELECT * WHERE COL4 >= DATE '"&TEXT(A2,"YYYY-MM-DD")&"' LIMIT 1",0),
QUERY(SORT(FILTER(Lookup!A$2:D,Lookup!C$2:C=B2,Lookup!A$2:A<=A2),1,0),
"SELECT * LIMIT 1",0) )
I've added a tab Live-GK to your sheet, with this formula in C2. It has to be dragged down. There may be another approach where it can be done as an arrayformula, but I haven't figured that out.
Note that on my tab, I'm doing the lookups from Lookup-GK, since I could add more test data there. The above formula can be used as is, pasted into cell C2 in your Live tab.
Note that for debugging purposes, column H of my tab returns all of the columns, not just the client #, so the start and end dates can be verified.
Let me know if this helps you.
Explanation:
The inner filter selects all rows from the Lookup tab where:
i) the client name (column C in Lookup) matches the client name in column B (of Live), and,
ii) the start date (column A in Lookup) is less than or equal the client date in Live.
These records are sorted in descending date order.
Then the query selects the first record where the end date (column D in Lookup) is greater than the client date in Live.
If the Lookup record has no end date, this gives an error (empty query result) so IFERROR, a second query is run, but without the filtering by end date, selecting the one record with no end date, but an appropriate start date.
These seemed to work with the few test records I used. If there is a duplication of client dates, the first client # is returned. See client #1 and #7 in my test data. Some more error handling might be necessary if your client records might have overlapping date ranges, as CalculusWhiz asked.

Format Date and Time

In column A I have date and time.
In Column B I have this formula to split them:
={"COL B","COL C"; ArrayFormula(IF(NOT(ISBLANK(A2:A)), SPLIT(A2:A, " ")))}
My sheet is linked to a Google form.
Every time a new sheet is submitted, the new data looks like 44075 and 0.9756944444 and I always need to format as Date and the other one as time.
Is there any way that my formula will automatically format the splitted value in col b and c?
You might try this:
={"Date","Time"; ArrayFormula(IF(ISBLANK(A2:A),, {DATEVALUE(A2:A),TIMEVALUE(A2:A)}))}
or this:
={"Date","Time"; ArrayFormula(IF(ISBLANK(A2:A),, {INT(A2:A),MOD(A2:A,1)}))}
In either case, you'll want to do Format>Number Date for B and Format>Number>Time for C.
UPDATE:
Since you seem to want the formatting to appear a certain way without using the Format>Number option, this will output dates and times...
=ARRAYFORMULA({"Date","Time";IF(A2:A="",,TEXT(A2:A,{"m/d/yy","h:mm am/pm"}))})
I've tried a lot of formula and surrendered.
I've just added a script:
var date = ss.getRange('R2:R');
date.setNumberFormat("MM/dd/yyyy");
var Time = ss.getRange('S2:S');
Time.setNumberFormat("hh:mm:ss AM/PM");

Using =INDIRECT in google sheets with If statement

So I'm trying to create a Google sheet for a project where every 5 years we need to contact the users , I have set up in the first sheet a column for todays date - another column for date in 5 years then a countdown in the next column (which counts down every day)
I am trying to make it move over to the next sheet once the countdown has say 1 year left on it
I am using indirect =INDIRECT("time keeper!A:E") which is fine it moves over the information however it moves it over instantly - Can I Set it up so once the timer reaches <365 days it moves over to the new sheet and then once I have contacted them I change the date to the current date then make it move back over to the original spreadsheet? or am I expecting too much here of google sheets?
Thanks in Advance!
You can use QUERY to select data based on certain conditions. In this instance you could :
=QUERY(Sheet1! A:D, "Select * Where D =1")
Assuming D is your helper column, if you selected up to E and E was your tick box for contacted you could set it to a true false boolean
=QUERY(Sheet1! A:E, "Select * Where D =1 AND where D = 'FALSE' ")
This would only pull people yet to be coctacted.
You could chain multiple conditions to get a more fluid approach and dynamic approach.
Hope that helps.

Google sheets wont filter a query

I am trying to filter a Google sheet with columns A to O using a query on a second sheet. I can put this formula in a new sheet and it pulls all the data just like its supposed to.
=query('Inventory'!$A$1:$O, "select * ", 1 )
What I am trying to do is filter the spreadsheet with a where clause that is to use a cell dropdown list (generated by unique values in the original sheet). I have used this formula:
=query('Inventory'!$A$1:$O, "select * where B = "&C1&"", 1 )
where C1 has the drop down box. This always gives me the error:
"Error
Unable to parse query string for Function QUERY parameter 2: NO_COLUMN: value" (the value depends on what is selected in the drop down).
It also doesn't work if I wrap the B = "&C1&" in parenthesis () which i have seen other posts use.
When I looked at Google's site for their example (https://support.google.com/docs/answer/3093343?hl=en), this should be a no-brainier unless something changed and their site isn't up to date.
I apologize for something this simple.
Try
=query('Inventory'!$A$1:$O, "select * where B = '"&C1&"'", 1 )

How to automatically fill data in an invoice in Google Sheets

I am sharing this sheet with you so that you can have a look and give me a proper solution. Basically what I want to do it generate an invoice based on the data entered in "Purchases" but I don't know how to do it as the lot numbers aren't always in the proper sequence. Therefore, if you notice in the "Sale & Inventory" sheet, I have to create multiple entries for one invoice. Is there a simpler method where the invoices are generated automatically? I don't want more than 30 lots/invoice.
Thanks,
Huzaifa.
I can't leave a comment to ask a question, so I assumed you'd want all lots from a certain date. Please see the 'Invoice' tab from this spreadsheet (I duplicated yours). Changing the yellow drop-downs should fill the invoice for you. You can also create multiple pages so it stays A4 size.
I also had to make sure column C on the 'Purchases' tab was formatted as a date 'yyyy-mm-dd' by using the 'Format' menu.
I did this by adding =QUERY(Purchases!$A$2:$T$500,"Select B where B<>'' and C >= date '"&TEXT(REGEXEXTRACT(K3,"[0-9]{6}"),"20##-##-##")&"' and C <= date '"&TEXT(REGEXEXTRACT(L3,"[0-9]{6}"),"20##-##-##")&"' limit 30 offset "&IF(M6=1,0,(M6-1)*34)&"",0) to cell H8 (highlighted green) in 'Invoices' tab.
How it works:
=QUERY(
Purchases!$A$2:$T$500, //the data I want to query
"Select B //tells it which column i want data from
where B<>'' //make sure column B is not blank
and C >= date '"&TEXT(REGEXEXTRACT(K3,"[0-9]{6}"),"20##-##-##")&"'
//makes sure the date is greater than or equal to
//the start date found in your lot code and formats
//it as 'yyyy-mm-dd' which is necessary for the
//query function
and C <= date '"&TEXT(REGEXEXTRACT(L3,"[0-9]{6}"),"20##-##-##")&"'
//makes sure the date is less than or equal to
//the start date found in your lot code and formats
//it as 'yyyy-mm-dd' which is necessary for the
//query function
limit 30 //restricts the returned results to 30 so it does
//not exceed the number of lines on your invoice
offset "&IF(M6=1,0,(M6-1)*30)&" //this shows the 'page' of data that you select
")
Let me know if you have different criteria for invoices and I can help, but try and see if you can figure it out from here first.

Resources