Query formula Google Sheets with time - google-sheets

Does anyone know how I can add to the below query to find results between 2 time periods and sort from earliest to oldest.
'"&B5&"' in the query = MONDAY.
P in TempCSV is time from splitting date & time in column D with this formula in column O =iferror(split(D2, " ")) and this continues down the column for all other rows.
I cant use date and time from D as the date is creation date and not current date in relation to what I'm trying to achieve.
=query(TempCSV,"Select A,D where A contains 'Something' and A contains 'Anotherthing' and C contains '"&B5&"' **where P >=05:00:00 and P <=12:00:00**")
I have successfully got the following to work in another range but this is due to the date and time being correctly recorded in the same cell.
=query(TPM,"SELECT A,B,C where A contains 'Something' and A contains 'Anotherthing' and C >= datetime '"&text($C$5,"yyyy-mm-dd 05:00:00")&"' and C < datetime '"&text($C$5,"yyyy-mm-dd 12:00:00") & "'")

Related

Google Sheets Select X where (max(y)<=z)

my title is potentially not that enticing. But I am trying to create a semi-dynamic formula in order to find a "stock on hand" up to a particular date in time. There is a set number of locations ids 1-10, and two product types 3 & 4.
It is not guaranteed that each location will have a stock count at the date in question. I want to use query to find THE MOST RECENT stock count where location and product type and <= date
here is the basic formula
=QUERY(Sheet1!A160:E3530,"SELECT D WHERE ((B = "&$H$1&")) AND (E <= date '"&TEXT(MAX($M$3),"yyyy-mm-dd")&"') AND ((A = "&G2&"))", true)
But I need to figure out how to use MAX to find the most recent date within the date range specified.
Any help appreciated!
EDIT 23/06/2021
You will note this is a fraction of the data I have in my set (in the example sheet), so most numbers show as zero, but the formula
=MAXIFS($C$3:$C$6040,$A$3:$A$6040,I3,$B$3:$B$6040,$J$2,$E$3:$E$6040,(MAX(QUERY($A$3:$E$6040,"SELECT E WHERE (E <= date '"&TEXT($R$2,"yyyy-mm-dd")&"') AND ((A = "&I3&")) AND ((B = "&$J$2&"))", true))))
works on my full data. So this finds the most recent record of equipment type 3 or 4, up to the specified date and from a specified yard. Further filtering is done based on a change type of "converted, removed,dead,added,etc". What I want to do now is do a monthly or fortnightly line chart over time, eg the 14th and 29th of each month, or the 20th of each month and plot the the sum of each column J:Q. To start I hoped to use the date in U:U and populate the V:AC accordingly.
I have played with the onEvent script but I am struggling to make progess here

Convert date in query function and order by date column

I have a large set of data that's updated every 2 weeks, so I'm looking to simplify the solution.
Imported sheet has data from D6:G. The date is in Column B.
Right now, I insert column H and use
=ARRAYFORMULA(if(isblank(B6:B),"",datevalue(B6:B)))
In a new sheet, I import the data and order by Col H (the converted date) and Col G (the employee's name).
=query(TimeApr10!B6:H,"select B,C,D,E,F,G where H is not null order by H,G asc")
Is there a way to combine the two steps by converting Col B in the query to datevalue? If I don't convert the date, the ordering doesn't work.

select first day of month in google sheets query

I am trying to construct a query that would among other columns return a date, which is in column J of an "rawData" table and change this date to the first day of month.
In my query all date statements but the last one are for testing purposes. The last one causes an error (Unknown range name: 'J'.). Anybody knows where is the mistake? This is my query:
=query(rawData;"select C, YEAR(J), MONTH(J)+1,
date'2010-08-31',
date '"&TEXT(2010-10-1;"yyyy-mm-dd")&"',
date '"&TEXT(CONCATENATE("2020-12";"-01");"yyyy-mm-dd")&"',
date '"&TEXT(CONCATENATE(YEAR(J);"-";MONTH(J);"-01");"yyyy-mm-dd")&"',
SUM(L)
WHERE (C<>'')
AND (F=18 OR F=20 OR F=62)
group by C, YEAR(J), MONTH(J)+1";1
)
Thanks,
Andrej
First, refer the documentation for the Query Language Reference, particular the use of [Scalar Functions] (https://developers.google.com/chart/interactive/docs/querylanguage#scalar-functions). The options here are very limited and do not appear to permit the calculation of the 'first of the month' within the query, per se.
It is possible to build the 'first of the month' within the rawData sheet, and I'll demonstrate the process, but I'll think you will agree that it doesn't achieve any perceivable advantage.
Try this.
Helper Column P
1) Create a helper column "P" in rawData. In cell P2, enter this formula:
=arrayformula(IF(ISBLANK(J2:J);"";DATE(Year(J2:J);Month(J2:J);1)))
Select the entire column and format it as Date 'yyyy-mm-dd'.
This is your "first of the month date". For identification, enter "Date yyyy-mm-dd" in Cell P1.
Query#1
2) Create a new sheet for testing. In cell A1 enter this formula:
=query(rawData!A2:P;"Select C, sum(L) WHERE (C<>'') AND (F=18 OR F=20 OR F=62) group by C label C 'Reg_st_vozila',sum(L) 'Total'").
This is the product code and sum by quantity, grouped by Product Code; you should have 24 records.
Query#2
3) In the same new sheet, in cell D1 enter this formula
=query(rawData!A2:P;"Select C, P, sum(L) WHERE (C<>'') AND (F=18 OR F=20 OR F=62) group by C,P label C 'Reg_st_vozila', P 'Date', sum(L) 'Total'")
This is the product code and sum by quantity, grouped by Product Code and First of the Month date (the date from the Helper Column). You should have 1142 records.
Query#3
4) In the same new sheet, in cell H1 enter this formula:
=query(rawData!A2:P;"Select C, Year(J), Month(J)+1, sum(L) WHERE (C<>'') AND (F=18 OR F=20 OR F=62) group by C,Year(J),Month(J)+1 label C 'Reg_st_vozila', Year(J) 'Year', Month(J)+1 'Month', sum(L) 'Total'")
This is the product code and sum by quantity, grouped by Product Code by Year by Month date. You should have 1142 records.
The output is identical to the preceding formula, though the query has an extra field (one for Year, one for Month). My point is that the 'first of month' doesn't achieve anything special.
Helper Column Q
5) Create a second helper column "Q" in rawData. In cell Q2, enter this formula:
=arrayformula(J2:J)
Select the entire column and format it as Date 'yyyy-mm'.
This is a modification of the date column, but formatted for year-month.
For identification, enter "Date yyyy-mm" in Cell Q1.
Query#4
6) In the new query sheet, in cell M1 enter this formula:
=query(rawData!A2:Q;"Select C, Q, sum(L) WHERE (C<>'') AND (F=18 OR F=20 OR F=62) group by C,Q label C 'Reg_st_vozila', Q'Year/Month', sum(L) 'Total'")
This is the product code and sum by quantity, grouped by Product Code by Year & Month. You should have 1142 records.
The output is identical to Query#2 but without the aggravation of calculating the "first of the month. Again, it shows that grouping on 'fom' doesn't achieve any perceivable advantage.

QUERY with "contains" and "date range"

I am trying to write a formula that would pull data based on a selection I made and within the date range I put in as well. The code I wrote below does not work. What am I writing incorrectly?!
=QUERY(INDIRECT(CONCATENATE(A2,"!A:FB")), "where (C >=date '"&TEXT('Feedback Report'!C3,"yyyy-mm-dd")&"' and C <= date '"&TEXT('Feedback Report'!D3,"yyyy-mm-dd")&"' and E contains '"&B2&"' ",1)
I asked too soon. After trying one more time, I have found a solution:
=QUERY(INDIRECT(CONCATENATE(A2,"!A:FB")), "Select * where (C >=date '"&TEXT('Feedback Report'!C3,"yyyy-mm-dd")&"' and C <= date '"&TEXT('Feedback Report'!D3,"yyyy-mm-dd")&"' and E contains'"&B2&"')")
Here is the breakdown:
Formula is on a sheet that pulls data from another source (raw data)
INDIRECT(CONCATENATE(A2,"!A:FB")) is where the data is stored
A2 is the sheet I choose from a drop down menu to have the data be pulled from
C is the date column of the raw data
C3 is the starting query date
D3 is the ending query date
E is the category column
B2 is the category value I want to search on
Hope this helps people in the future.

matching a date in a google spreadsheet

I am trying to match a date within a google spreadsheet, but I am new to this, and I could use some help. I have a column that contains a list of birthdays. I have a cell that I am using to reference for the current date by using
=Today()
What I would like to do is compare the month and day, but ignore the year, and return the values in the two adjacent columns. I am using this query to try to get the information.
=QUERY(C2:E430; "select * where C = date '" & text(F2,"yyyy-MM-dd") & "'")
but it always returns an empty output, because the year never matches. How do I get it to ignore the year?
Thank you,
Paul
One way would be to compare the month and day separately (note that the MONTH() spreadsheet function is January = 1, while the month() function in the QUERY select clause is January = 0).
=QUERY(C2:E430;"select * where month(C) = "&(MONTH(F2)-1)&" and day(C) = "&DAY(F2))

Resources