I am working on a query formula in google sheets. I am trying to get the WHERE part of the query to end at the end of the last month. I am getting a parse error at 'TEXT(EOMONTH(now(), -1),"yyyy-mm-dd")'. I know that is the portion that is failing because if I replace it with a date (2021-07-31) the query works.
Thanks in advance.
=QUERY(Transactions!A:Z, "
SELECT K, -1 * SUM(E)
WHERE F LIKE 'Investing'
AND B > DATE '2020-05-31'
AND B < DATE 'TEXT(EOMONTH(now(), -1),"yyyy-mm-dd")'
GROUP BY K
LABEL -1 * SUM(E) 'Investments'
FORMAT K 'yyyy-MMM',
-1 * SUM(E) '$#,##0.00'
")"))"
There are more issues with your current formula than one. See my comment below your post. However, you can try this:
=QUERY(Transactions!A:Z, "SELECT K, -1 * SUM(E) WHERE F LIKE 'Investing' AND B > DATE '2020-05-31' AND B < DATE '"&TEXT(EOMONTH(now(), -1),"yyyy-mm-dd")&"' GROUP BY K LABEL -1 * SUM(E) 'Investments' FORMAT K 'yyyy-MMM', -1 * SUM(E) '$#,##0.00' ")
If that does not work, share a copy of your sheet.
Additionally, it's not clear to me why you're referencing A:Z when your QUERY only requires B:K. This won't affect the return; but in larger sheets, requesting only what you need can speed up processing.
Related
Struggling to get this to work as I need. Basically a set of nested IF formulas with some conditions (see bottom of post for the rules)
I have 3 columns - J, K and L
Column J
contains the last login date (represented by 01/01/1970 if it's never been logged into)
Column K contains the last interaction date (represented by 01/01/1970 if no interaction or blank if within the last 90 days)
Column L contains the last activity date for a different metric. (blank if within the last 90 days)
I have another column, M - which I am intending to use as the calculated field.
This is what I am trying to achieve:
J
K
L
M
01/01/1970
01/01/1970
Recent Activity
26/06/2021
26/06/2021
01/01/1970
01/02/2021
Recent Activity
30/06/2021
01/01/1970
23/03/2020
30/06/2021
So the rules are as follows:
If Col J contains 01/01/1970 OR Col J date is before 25/03/2021 then check Col K.
If Col K contains 01/01/1970 then check Col L. If Col K IS BLANK then show "Recent Activity"
If Col L IS NOT BLANK then show Col L Date. If Col L IS BLANK then show "Recent Activity"
If Cols J, K and L all contain a date, get the most recent date from them
I have pulled out a few tufts of hair trying to get this to work, so any help would be greatly appreciated!
Looking at your rules, is 25/03/2021 a static date or should it be derived from a value somewhere? If static, then try this for the IF statements:
=arrayformula(if(J1:J<>"",if(isblank(J1:J)+isblank(K1:K)+isblank(L1:L)=0,if(K1:K>J1:J,if(L1:L>K1:K,L1:L,K1:K),if(L1:L>J1:J,L1:L,J1:J)),if(J1:J<datevalue("2021-03-25"),if(K1:K=datevalue("1970-01-01"),if(L1:L<>"",L1:L,"Recent Activity"),"Recent Activity"),J1:J)),))
There might be better functions, but this is mainly done with IF().
if(J1:J<>"" will run the subsequent formula as long as there are values in col J.
if(isblank(J1:J)+isblank(K1:K)+isblank(L1:L)=0 checks each row to see if there is a date in col J, K and L.
If there is, then this gets the largest date (it's a bit clumsy but someone else might be able to help):
if(K1:K>J1:J,if(L1:L>K1:K,L1:L,K1:K),if(L1:L>J1:J,L1:L,J1:J))
An alternative to finding the largest date on each row could be:
transpose(dmax(transpose(J:L),{sequence(1,max(if(J:L<>"",row(J:J))))},{J:L}))
or:
query(transpose(query(transpose(J1:L),"select "®exreplace(join("","Max(Col"&sequence(max(if(J:L<>"",row(J:J))))&"),"),",$",)&" ",0)),"select Col2",0)
The other IFs test out the other scenarios.
based on your rules try in row 1:
=INDEX(IF(ISDATE_STRICT(I:I)*ISDATE_STRICT(J:J)*ISDATE_STRICT(K:K),
VLOOKUP(ROW(I:I), SPLIT(SORT(FLATTEN(ROW(I:I)&" "&I:J), 1, ), " "), 2, ),
IF(LEN(I:I&J:J&K:K), IF(I:I<"25/03/2021"*1, IF(J:J="01/01/1970"*1,
IF(((I:I="01/01/1970"*1)+(I:I<"25/03/2021"*1))*(J:J="01/01/1970"*1),
"Recent Activity", ), "Recent Activity"), I:I), )))
I think doing this as an arrayformula is unnecessarily complicated.
Here is a sample sheet made specifically for this question. In it, i placed this formula in cell M2 on a tab called MK.Idea and dragged it down:
=IF(J2,IF(J2>1*"25/3/21",J2,IFERROR(1/(1/MIN(N(K2),N(L2))),"Recent Activity")),)
Does it give the expected results? maybe you could add some more sample data on the sheet and test it out?
Trying to set up a formula where I can pull N counts based off multiple criteria including a date range. A typical Countif formula could work in theory:
=countifs(D2:D, J2, F2:F, J3, E2:E, J4, G2:G, J5, C2:C, ">="&TODAY()-7, C2:C, "<="&TODAY())
However, the problem I am facing is I want to be able to still pull the N count even if I leave one or more data validated cells the formula is pulling off of blank. In order to pull anything, you have to select one of the 4 data validated options. As you select more, the data gets more granular. Currently if I leave one cell blank, I then get a zero on the return. Need that not to happen
Any thoughts on how I could make this work?
I have done this with queries before, but I am not sure how to pull N count if use query. Happy to go that route as well if its easiest
use:
=COUNTA(IFNA(QUERY(C:G,
"select C
where 1=1 "&
IF(J2="",," and D = '"&J2&"'")&
IF(J3="",," and F = '"&J3&"'")&
IF(J4="",," and E = '"&J4&"'")&
IF(J5="",," and G = '"&J5&"'")&
" and C <= date '"&TEXT(TODAY(), "yyyy-mm-dd")&"'
and C >= date '"&TEXT(TODAY()-7, "yyyy-mm-dd")&"'", 0)))
Here O column is my sum range,
P2 = Date i.e. 2019-05-02
I want to sum all O column range on the bases of the last 30 days. i.e. p2-30.
This formula is working fine in Excel but not working in Google Sheets.
=SUMIFS(O:O,C:C,C2,P:P,"<"&TEXT(P2-30,"yyyy-mm-dd"))
I am really new to this and would appreciate any hints.....
You might want to give the QUERY function a try.
In
F1: =text(today()-30;"yyyy-mm-dd")
H1: =query(O1:P18;"Select sum(O) where P> date '"&F1&"'")
In case you don't want a header
H1: =query(O1:P18;"Select sum(O) where P> date '"&F1&"' label sum(O) ''")
QUERY is a very nice function. Using a date can be a bit tricky, look for examples at https://www.benlcollins.com/spreadsheets/query-dates/
=SUMPRODUCT(QUERY(O1:P, "select O
where P <= date '"&TEXT(C2, "yyyy-MM-dd")&"'
and P >= date '"&TEXT(C2-30, "yyyy-MM-dd")&"'", 0))
I would like to ask if anyone knows the exact syntax to get this query working on google sheets.
I currently have 3 Sheets:
1. Sheet A, RAW DATA (where all my Raw Data is)
2. Sheet B, Main Page (where I have my user input on the date)
3. Sheet C, Filtered Data (where I would like to use the Query to pull out filtered data from Sheet A)
So I have set up a generic query goes like this in Sheet C, it works:
=QUERY('RAW DATA'!A:J, "SELECT * WHERE F > date '2018-03-20' AND F < date '2018-03-22' AND F IS NOT NULL ORDER BY F",1)
I would now like to replace the 2 dates, with user input dates, which are B5 and D5 in Sheet B.
I have tried this syntax:
=QUERY('RAW DATA'!A:J, "SELECT * WHERE F > date '"&Main Page!B5&"' AND F < date '"&Main Page!D5&"' AND F IS NOT NULL ORDER BY F",1)
Do note that B5 and D5 have already been set up with data validation rules to ensure that only date values are allowed.
Could anyone point out my mistake?
Thank you!
The date provided to QUERY must be:
a string of
format yyyy-mm-dd
Try changing from
date '"&Main Page!D5&"'
to
date '"&TEXT(Main Page!D5,"yyyy-mm-dd")&"'
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.