How to Take month number in select statement in google Query - google-sheets

How to take the current month number in the Select query in google sheets?
I am writing a query like this wherein Col21 I have month numbers like 1,2,3 4, etc based on month
=QUERY({Sheet1!A:X},"Select Col7,Col13 where Col21= '&month(today())&' ")
It is giving me headers only
whereas when I am writing a query like this :
=QUERY({Sheet1!A:X},"Select Col7,Col13 where Col21 contains '8' ")
It is giving me all records, but I want to replace the static value of month by Current month number only; what should I do for this so that when month changes I don't have to do any changes in the query.

Please make sure Col21 is in number, not in string
=QUERY({Sheet1!A:X},"Select Col7,Col13 where Col21= "&month(today()))

Related

Filter inside QUERY based on substring

So I am using a combination of query() and importdata() to pull data from another file. A simplified version of the results I get are the columns Supplier and Week where Week would be of the form WK01, WK11, WK43 etc.
How can I filter the Week column so that I only get weeks which have a week below WK10 in my results? In normal SQL you could just do something like where right(Week,2)<10 and this would return only results where the Week would be below WK10 but not sure how to do such a substring filter in Google Sheets' Query function.
you can use regex like:
where Col2 matches 'WK0[1-9]'
or:
where Col2 matches 'WK0.'

In Google sheets, can a QUERY statement determine the oldest date in a column, add 6 months to it , then display that range?

Currently, I am having to query the entire set, jot down the oldest date, and then add a GTE (>=) and a LTE (<=) back into the query statement to limit the results to 6 months, like this:
=QUERY(RawData!A1:D, "SELECT * WHERE A = 'N502SY' and C >= date '2020-03-20' and C <= date '2020-09-20' ORDER BY C ASC")
Ideally, the query statement would determine ON ITS OWN what the oldest date is and then display all rows found with that date PLUS all rows that have a date within a 6-month period (from the oldest date).
Have done a ton of google searching to find an answer but have come to the conclusion that QUERY statements may not be as robust as I am hoping for. Here is the URL (kindly do not change the raw date):
https://docs.google.com/spreadsheets/d/17n3PYBrhlbD1RIgToohS3a1QbN5qhrQnREcMAnZdcbY/edit#gid=626607315
your shared sheet has restricted access. so here's a working formula based on rough sample data.
=QUERY(A:D, "SELECT * WHERE A = 'N502SY' and C >= date '"&text(EDATE(MAX(C:C),-6),"yyyy-mm-dd")&"' and C <= date '"&text(MAX(C:C),"yyyy-mm-dd")&"' ORDER BY C ASC")
-
You can try with FILTER and SORT too:
=SORT(FILTER(A1:D,A:A="N502SY",C:C>=EDATE(MAX(C:C),-6)),3,1)

Google sheets sumif with odd data

I have sales data that gives me dates in a bad format. Every new sale gets automatically added to the sheet. Looks like this:
Column A
Column B
Column C
Order 1
2022-12-02T02:09:37Z
$1025.19
Order 2
2022-12-02T01:25:15Z
$873.65
This will continue on for all sales. Now the date format is UTC for whatever reason and I can't adjust that, so within this formula I have to subtract 6 hours to get it to central time. I'm trying to create an auto-updating chart that shows an average day for 7 days, so I'm trying to do a sumif formula.
Here's what I have on Sheet2:
=sumif(Sheet1!C:C,index(split((index(split(Sheet1!B:B,"T"),1)+index(split(left(Sheet1!B:B,19),"T"),2))-0.25,"."),1),A1)
Where A1 is a single date. Testing this with one date and not the range shows that it does match. When I do the range, the total comes to 0, even though multiple different dates should match. What am I doing wrong?
Assume A1 has the value: 2022-12-02T02:09:37Z
Apply this formula:
=LAMBDA(RAW,TUNEHOUR,
LAMBDA(DATE,TIME,
TEXT((DATE&" "&TIME)+TUNEHOUR/24,"yyyy-mm-dd hh:mm:ss")
)(TEXT(INDEX(RAW,,1),"yyyy-mm-dd"),REGEXREPLACE(INDEX(RAW,,2),"Z",""))
)(SPLIT(A1,"T"),-6)
returns:
2022-12-01 20:09:37
And assume you have a set of data like this:
you can apply this formula:
=ArrayFormula(
LAMBDA(DATES,AMOUNTS,START,END,DFORMAT,TFORMAT,SKIPBLANK,TUNEHOUR,
LAMBDA(DATES,AMOUNTS,DTFORMAT,START,END,
LAMBDA(DATES,TIMES,
LAMBDA(VALIDDATES,AMOUNTS,
TEXT(SUM(FILTER(AMOUNTS,VALIDDATES>=START,VALIDDATES<=END)),"$#,##0.00")
)(TEXT((DATES&" "&TIMES)+TUNEHOUR/24,DTFORMAT),IF(ISNUMBER(AMOUNTS),AMOUNTS,VALUE(REGEXEXTRACT(AMOUNTS,"^\$(.+)"))))
)(TEXT(INDEX(DATES,,1),DFORMAT),REGEXREPLACE(INDEX(DATES,,2),"Z",""))
)(SPLIT(QUERY({DATES},SKIPBLANK),"T"),QUERY({AMOUNTS},SKIPBLANK),DFORMAT&" "&TFORMAT,TEXT(START,DFORMAT)&" 00:00:00",TEXT(END,DFORMAT)&" 23:59:59")
)($B$5:$B,$C$5:$C,$B$1,$B$2,"yyyy-mm-dd","hh:mm:ss","WHERE Col1 IS NOT NULL",-6)
)
Where you enter a start date and an end date at B1 & B2 to sum up the amount with.
The provided date column will be deducted by 6 hours.
What this formula does is...
format the date column into a valid date,
compare dates from step 1 with a given start and end date as filter condition,
filter the given amount column with conditions from step 2,
sum the result of filter from step 3 as an array,
format the output as price.
Use regexreplace() and query(), like this:
=arrayformula(
query(
{
weeknum(
regexreplace(B2:B, "([-\d]+)T(\d\d:\d\d).+", "$1 $2")
-
"6:00"
),
C2:C
},
"select Col1, avg(Col2)
where Col1 is not null
group by Col1
label Col1 'week #' ",
0
)
)
I think you're trying to split the values and sum them. I can't understand fully what's the purpose of 19 in LEFT function, and why are you again splitting it? Maybe some approach similar to yours is use LEFT function with 10 characters for the date, and MID from 12th character to get the time. Then substract .25 for the 6 hours as you did, and ROUNDDOWN with 0 digits to get the only the day
=ARRAYFORMULA(ROUNDDOWN(LEFT('Sheet1'!B:B,10)+MID('Sheet1'!B:B,12,8)-0.25,0))
And then you can insert it in your SUMIF:
=SUMIF(Sheet1!C:C,ARRAYFORMULA(ROUNDDOWN(LEFT(Sheet1!B:B,10)+MID(Sheet1!B:B,12,8)-0.25,0)),A1)

Sum range by month in Google Sheets with date conditional

I have a sheet with these columns:
A (Date): Range of dates for the year
B (Amount): An amount for an expense
D (Month): Name of each month in the year (e.g. June, July, etc)
I've tried this as suggested to work in other posts:
=SUMPRODUCT(B:B, ISDATE(A:A)*MONTH(A:A)=MONTH(D3&1))
I get the error Function MONTH parameter 1 expects number values. But 'Date' is a text and cannot be coerced to a number. with this.
If I remove the header, I just get a 0 for the sum for each month, which is incorrect.
How do I get this to work for each month the way my sheet is setup?
What the sheet currently looks like:
https://docs.google.com/spreadsheets/d/1_8DQTa9aXGjvd7twL6RZMcFyqxa4Sg7eVTcD2wDXH2A/edit?usp=sharing
Try this instead. Not sure where you got that other formula:
=SUMPRODUCT(B:B,TEXT(A:A,"mmmm")=D3)
use:
=ARRAYFORMULA(IFNA(VLOOKUP(MONTH(D3:D10&1),
QUERY(A2:B, "select month(A)+1,sum(B) group by month(A)"), 2, )))
Use this
=IF(D3="",,IFERROR(SUM(FILTER($B$2:$B,MONTH($A$2:$A&1)=MONTH(D3&1))),""))

Google Sheets =MONTH works but MONTH in QUERY does not

Google Sheets: I checked my column of dates is in DATE format not Automatic.
When I use MONTH in a cell I get the correct month back from that column.
When I do a QUERY such as =query('Main'!A1:M20,"select MONTH(M)",1) I get #VALUE! with the comment:
Unable to parse the Function QUERY for parameter 2: Can't perform the function MONTH on a column that is not a DATE or a DATETIME column
Why does QUERY not see the column as being in DATE format but =MONTH does?
months in a query are numbered and starts from 0, therefore, you will need to add +1 to get the first month and then do a weird logic: "where month1=month2" to get february (month(A)+1=3 for march, month(A)+1=12 for december, etc.)
=QUERY(A1:D10, "select A,B,C,D where month(A)+1=2", 1)

Resources