I have data that I'm importing from Salesforce, and I'm using query functions to find all rows where any of the columns has a date in a given range. Here's an example of the data:
The query that's not working is:
=query('Salesforce Data'!A2:C,"SELECT A,C WHERE C >= date '"&TEXT(DATEVALUE($A$1),"yyyy-mm-dd")&"' AND C < date '"&TEXT(DATEVALUE($B$1),"yyyy-mm-dd")&"'")
I'm using the same query except in one case, it's looking at dates in column B, and in the other, it's looking at the dates in column C. The column B version works, the column C version does not. I have verified that there is at least one date in column C that falls in the range, so it should not be an issue of no data, as the error suggests:
I've looked over data formatting, and there is no difference between columns B and C in that regard. These are the same types of field in Salesforce as well, so I would not expect a difference in formatting. I tried manually changing the first value in column C to a date (that was an obvious difference between the columns), but that also didn't work.
After a lot of trial and error, I found the issue: it seems that Google Sheets classifies the column of data based on what the majority of the cells are. So, even though both columns B & C have some cells with valid dates and some with a - signifying null, column B has more dates than strings, but C has more strings than dates, so date compare queries won't work on column C at all.
My solution for now is to add a formula sheet to transform all of the null values, -, into a date that won't mess with my query, 1/1/1970:
Example formula:
=IF( OR('Salesforce Data'!C2="-",'Salesforce Data'!C2=""), date(1970,1,1), 'Salesforce Data'!C2)
Another solution would be to edit the data source, but this solution will work entirely within sheets.
Also note, I dragged this formula down far below where I needed, just in case, make sure that if you have a text column (like my column A), you replace empty values there with junk text of some sort. At first I replaced with 0 and then my text column wasn't picked up by the query.
try:
=ARRAYFORMULA(QUERY(TO_TEXT('Salesforce Data'!A2:C),
"select Col1,Col3
where Col3 >= date '"&TEXT(A1, "yyyy-mm-dd")&"'
and Col3 < date '"&TEXT(B1, "yyyy-mm-dd")&"'", 0))
Thank you thank you so so much. This thread helped me a lot.
I have used these from this thread. Someone may need in future:
"select A, B, C, G, H, J where I='"&TEXT($A$2, "dd-mmm-yyyy")&"'"
"select B, C WHERE F= date '"&TEXT(DATEVALUE($A$2),"yyyy-mm-dd")&"'"
"select A, B, C, G, H, J where I='"&TEXT($A$2, "dd-mmm-yyyy")&"' or I='"&TEXT($A$2, "d-mmm-yyyy")&"'"
Related
For this question I have made a simplifed example spreadsheet to illustrate what I am trying to achieve.
This can be found here.
https://docs.google.com/spreadsheets/d/1qbzuDHfRnGHkDhrcQWbQxRVcxsqo3MlJ3pZtU16nq7w/edit?usp=sharing
I will give background on the sheet first and then explain the problem, Forgive me if it's a bit convoluted it's my first time asking a question on here.
In the left hand side table I have example bank transactions made. In the right hand side table I have example data from my cash flow forecast. The goal is to match bank references and amounts to make sure that the forecast and the real transactions match up.
Actual Bank Transaction Table on Left Hand Side
Column A - Transaction references
Column B - Transaction amount
Column C - Formula to count the number of reference matches between the real transactions references in Column A and the forecast transactions reference range F3:F16
Column D - THIS IS THE PROBLEM I AM SEEKING A SOLUTION TO
Forecast Transactions on Right Hand Side
Column F - Forecast bank reference. (Either an exact match or a string contained within the string in Column A to allow for references that change every month i.e. Col A = "NETFLIX 23424589745897872134" and Col F = "NETFLIX")
Column G - Forecast transaction amount
The reason I am doing it with counting ref and price matches is that I then use these to automatically decide whether the transaction is a match to the forecast or not.
The problem is how to achieve my formula for Column D of my example spreadsheet. I need to check for price matches between each cell in column D and the range G3:G16 but only count matches where the transaction reference in column A matches to the range F3:F16 as per column C. This is to cut out many price matches that are not relevant.
The formula in C3 is currently
=if(A3="","",SUMPRODUCT(--ISNUMBER(SEARCH($F$3:$F$16,A3))*($F$3:$F$16<>"")))
This formula works great and is able to look at the range F3:F:16 and count both an exact match or strings contained within the string in Column A (i.e. Col A = "NETFLIX 23424589745897872134" and Col F = "NETFLIX"). This formula essentially allows me to count text strings in a range that exist within, or match, a single cell.
So at this point I cannot work out what the formula needs to be in Column D. I have tried to create a COUNTIFS formula with wildcards "" & & "" but I can't make anything work. My problem is that I can't utilise the current formula in Column C within a COUNTIFS in column D. Is there a way I can achieve the same outcome of Column C in a COUNTIF formula? Then I would be able to use that within a COUNTIFS formula in column D and my problem is solved... Hooray!
I know there must be a way but so far gogle has not been able to help me find the answer.
Please help!
Thank you :-)
in D3 try:
=INDEX(IFNA(VLOOKUP(REGEXEXTRACT(A3:A, TEXTJOIN("|", 1, F3:F)), F:G, 2, 0)))
update 1:
=INDEX(IF(A3:A="",,(B3:B=IFNA(VLOOKUP(REGEXEXTRACT(A3:A,
TEXTJOIN("|", 1, F3:F)), F:G, 2, 0)))*1))
update 2:
=INDEX(IF(A3:A="",,
IFNA(VLOOKUP(REGEXEXTRACT(A3:A, TEXTJOIN("|", 1, F3:F)), QUERY({F3:F},
"select Col1,count(Col1) group by Col1"), 2, ), 0)))
I can't seem to find the right equation to find a cell from a row that matches only a few specific characteristics. In this example, I am trying to find the equation for Column D which would be the cell in A that has the same cells for B & C.
Hope this makes sense!
I'll provide two options.
If you're sure your data will only ever have zero or one match, you can place the following formula into D2 of an otherwise empty range D2:D...
=ArrayFormula(IF(A2:A="",,SUBSTITUTE(VLOOKUP(B2:B&C2:C,{B2:B&C2:C,A2:A},2,FALSE)&VLOOKUP(B2:B&C2:C,SORT({B2:B&C2:C,A2:A,ROW(A2:A)},3,0),2,FALSE),A2:A,"")))
However, if you think more than one match may turn up and you want "None" to be returned if there is no match, you can use the following formula in D2 or an otherwise empty range D2:D...
=ArrayFormula(IF(A2:A="",,REGEXREPLACE(REGEXEXTRACT(REGEXREPLACE(SUBSTITUTE(VLOOKUP(B2:B&C2:C,TRIM(SPLIT(FLATTEN(QUERY(QUERY({B2:B&C2:C&"~",A2:A&","}, "Select MAX(Col2) where Col2 IS NOT NULL GROUP BY Col2 PIVOT Col1"),, 9^9)),"~")),2,FALSE),A2:A,""),"^[,\s]+$","None"),"([^,\s].+[^,\s])[,\s]*$"),"[,\s]+",", ")))
The second formula will work even if there will only ever be zero or one match; it's just not necessary to have it be that lengthy. And the second formula is only as lengthy because it was unclear from your posted examples whether the data in Col A, B and C will really only ever be one word or not; so the formula is built to assume there will not always be one-word strings in those columns.
Either formula will provide results for the entire column without dragging.
Here's an option, You can use this formula in column D2:
=iferror(textjoin(", ",true,query($A$2:$C,"Select A where A is not null and A != '"&$A2&"' and B = '"&$B2&"' and C = '"&$C2&"'",0)),"None")
Limitation:
You need to manually drag the formula to its succeeding rows. Arrayformula() cannot be used in looping the query string values.
What it does?
Using query(), filter the data from A2:C that has the same current row last name(Column B) and food(Column C) at the same time having a different first name(Column A)
If there are multiple results, use textjoin() to combine them with ", " as its delimiter.
If there is no matched found, it will return an error, hence use iferror() to set the default value to "None"
Output
Google sheets user here.
I am using the formula minifs to return the lowest match (out of multiple possible match). Is there a way I can use arrayformula as well to auto-populate an entire column so I don't need to copy the same formula to an entire column?
Sample data below:
Column D and J are data manually inputted. Column I is the formula(s).
Essentially what I want to do here is:
Look at Column D - sees the name "Tom"
Sees that "Tom" has 3 scores 100, 90, 70 in Column J
Formula slaps "70" back into Column I because that is the lowest score
Repeats logic for "John" and "Mary"
Note: The actual data type for column J and I is a date instead of a number. But it is easier to illustrate the problem this way.
So I can do this elegantly with the formula: =minifs(J:J,D:D,D2) and D3,D4,D5,D6...etc.
However, I will have to manually drag the formula to the entire column. This is a problem because my colleagues often insert rows in between (and forget to copy n paste the formula to Column I), is there a way I can auto-populate the entire column like I could with an arrayformula?
Assuming your data are A2:C, you can get the min or max of each row by this way: (you can also add a condition in query)
=query(transpose(query(transpose(A2:C),"select " & "min(Col"&arrayformula(textjoin("),min(Col",,row(A2:C)-1))&")")),"select Col2")
https://docs.google.com/spreadsheets/d/1Ia05jywxlvT2amFDG4vQhYOd0lo68FKdOY733MzU-MQ/copy
This question already has an answer here:
How to compare dates or date against today with query on google sheets?
(1 answer)
Closed 5 months ago.
I need to compare a text column, which contains a string that represents a date in the format dd/mm/yyyy, to another date.
The range is in another sheet. I cannot edit it to format the column to a date field:
I need something like this:
=query(importrange(...); "select A, B WHERE C >= '2017-01-08'")
Of course this doesn't work because only Aldo row will be visible. Is there any way to convert, in the query function, the column C to a date or to a string with the pattern yyyy-mm-dd to compare it to the right hand side value of the comparison?
Cumbersome but seems to work:
Import (say to Sheet1!A1):
=importrange(" k e y ","Sheet1!A:C")
Add a column with recognisable date formats (say in D1):
=ArrayFormula(datevalue(C1:C))
then apply the query:
=query(Sheet1!A:D,"select A,B,C where D >= date '2017-01-08' ")
I know this is WAY late, but I ran into this same problem a couple of years ago and fixed it by doing the following:
Cell A1 contains: 2/21/2018
Cell A2 contains: =DATEVALUE(B1)
which results in 43152 and I copy into the QUERY statement:
Cell A5 formula: =QUERY(Sheet!Range, "select B, C, E where B=43152")
But, to make it somewhat better by only having to type in the date, I used the CONCATENATE function to create the text "select B, C, E where B=43152" in a cell and then referenced that cell in my QUERY, like so:
Cell B1: =CONCATENATE("select B, C, E where B=", A2)
Cell A5 formula: =QUERY(Sheet!Range, B1)
This works well for me and I hope it helps someone else or at least gives someone an idea.
I use the Google Sheets SQL-ish Query function to summarize data in a number of my spreadsheets. This has been working well for years, albeit slowly.
Today, I'm having problems with some of my queries - specifically with some that compare dates in the source data to TODAY().
To demonstrate, here's a link to a shared spreadsheet that I've used to reproduce the problem on fake data.
Edit: Example has been updated with AdamL's suggestion.
The source data is in range A1:D6, with columns "Serial No.", "Type", "Location", and "Warranty Expiration". The last column is a date.
This function in A9 summarizes all data:
=query(A1:D6,"select B, count(A) group by B pivot C")
...like so:
Here's the thing. If I try to filter using WHERE and DATE(), the Query seems to break down completely. What I want is a table that looks like the one above, but including only data rows that have a date in column D that is in the past.
=query(A1:D6,"select B, count(A) where D < now() group by B pivot C")
If I change the filter to something not involving dates, I get the expected output:
How do I get this to give me the summary I want?
The now() scalar function returns a datetime value, and you have date values in the source data. Comparisons between the two will unfortunately fail. The workaround is to convert now() to a date value:
=QUERY(A1:D6;"select B, count(A) where D < toDate(now()) group by B pivot C")
As an aside, there is a limitation (bug?) with the QUERY function whereby the now() scalar function does not (necessarily) operate in the time zone of your spreadsheet, and there doesn't appear to be any way of modifying this behaviour. I believe that the now() scalar function will always return the current time in Pacific Daylight Time (eg US west coast). So for me, right now in Brisbane, Australia, toDate(now()) used in a QUERY select clause returns yesterday's date.
The safer bet is to use a spreadsheet function to generate today's date, and concatenate that into the QUERY clause:
=QUERY(A1:D6;"select B, count(A) where D < date '"&TEXT(GoogleClock();"yyyy-MM-dd")&"' group by B pivot C")