Google Spreadsheet: avg datediff returns error - google-sheets

I'm trying to build a report in Google Spreadsheets using the query() function. I want to select the average date difference using the dateDiff() and avg() functions.
Here is my query:
select avg(dateDiff(A, B))
And here is the table I'm trying to select from:
+---------------------+---------------------+
| A | B |
+---------------------+---------------------+
| 01/08/2013 15:41:00 | 31/07/2013 13:35:00 |
+---------------------+---------------------+
| 31/07/2013 17:17:00 | 31/07/2013 09:57:00 |
+---------------------+---------------------+
| 06/08/2013 15:14:00 | 29/07/2013 13:50:00 |
+---------------------+---------------------+
And this is the error message I get:
error: Invalid query: Query parse error: Encountered " "(" "( "" at line 1, column 20. Was expecting: ")" ...
Here is an open copy of the spreadsheet I've been working on.
https://docs.google.com/spreadsheet/ccc?key=0AhEoOqMhLAGbdEE1ZkhqSjlDT0lYVnVhY3hjNmlWZ3c#gid=0
Is it even possible to do with using the query() function?

If I'm understanding correctly, you require a single average of all the date differences. In which case it would be best to use the AVERAGE spreadsheet function outside the QUERY function.
=AVERAGE(QUERY(A1:B3;"select datediff(A,B) label datediff(A,B) ''"))
It actually isn't really necessary to put the label datediff(A,B) '' as the AVERAGE function will ignore the text header populated by the QUERY, but I put it in for the sake of completeness.

Related

Count values in another sheet in Google Spreadsheet where sheet name is a cell value using ARRAYFORMULA

I have a Google Spreadsheet with multiple sheets. I cannot share a sample because this is G-Suite Enterprise and it is locked. If absolutely needed I can create a dummy one on my personal Gmail.
One of the sheets has a list of lookup values and sheet names. I am trying to come up with an ARRAYFORMULA that will count the # of times Lookup Value shows in column A of Lookup Sheet Name.
| Lookup Value | Lookup Sheet Name | Count |
|--------------|-------------------|-------|
| one | Primary | ... |
| two | Secondary | ... |
| three | Stuff | ... |
| ... | Primary | ... |
| ... | ... | ... |
I came up with this formula For C2 but it does not work. I don't exactly know why but it shows 1 for every lookup value or just one row with 1.
=ArrayFormula(IF(A2:A <> "", COUNTIF(INDIRECT(B2:B & "!A2:A"), A2:A), ))
I do not want to use Google Apps Script to solve this.
Here is one possible solution that combines the values of the different sheets in one by adding a second column with constant value (the name of the seet for example).
=ArrayFormula(
IF(A:A<>"",
COUNTIFS(
{
FILTER('sheet A'!A:A,NOT(ISBLANK('sheet A'!A:A)));
FILTER('sheet B'!A:A,NOT(ISBLANK('sheet B'!A:A)))
},"="&A:A,
{
FILTER(IF(ISBLANK('sheet A'!A:A),,"sheet A"),NOT(ISBLANK('sheet A'!A:A)));
FILTER(IF(ISBLANK('sheet B'!A:A),,"sheet B"),NOT(ISBLANK('sheet B'!A:A)))
},"="&B:B
),
)
)

Why does the min function apparently add the name of the function to the output?

I have a small table with usernames and dates in a Google Drive spreadsheet as part of some manual logging.
e.g.:
| User1 | 01/09/2019 |
| User1 | 09/09/2019 |
| User2 | 13/09/2019 |
| User1 | 05/10/2019 |
(dates are formatted DD/MM/YYYY)
I want to create an overview of when each username was first logged. For this I created a second table with below function for the first column:
=UNIQUE(A2:A7)
For the second column, I wrote below function:
=QUERY(A2:B7, "SELECT MIN(B) WHERE A='"&C2&"'", 1)
The output I'm expecting to see is this:
| User1 | 01/09/2019 |
| User2 | 13/09/2019 |
But for some reason, the output I receive is this:
| User1 | min 01/09/2019 |
| User2 | 13/09/2019 |
What can I do to avoid the 'min' being added in the output? I don't see why this is being added in the first place.
You can do this in a single step with this formula:
=query(A:B,"select A, min(B) where A is not null group by A",1)
Also, I suspect your current date formatting is MM/dd/YYYY, and that's why "13/09/2019" isn't recognised as a date and creates weird behaviours.
Try to change the format (at least temporarily), using full months names, so you'll know for sure. If that's the case, just fix your dates and the formula above should do just fine:
Why does the min function apparently add the name of the function to the output?
A: because of that last 1 in your query formula.
instead, use this formula:
=QUERY(A1:B,
"select A,min(B)
where A is not null
group by A
label min(B)''", 0)
By putting ,1) after the query, you're telling it that the data (starting in row 2) has a header. So it uses whatever it finds in row 2 (1/9/2019) and adds it to the default header (min). If you would like your original formula to work as expected, change
=QUERY(A2:B7, "SELECT MIN(B) WHERE A='"&C2&"'", 1)
to
=QUERY(A2:B7, "SELECT MIN(B) WHERE A='"&C2&"' label min(B) ''")

Google Sheet Query Error: Unable to parse query string for Function QUERY parameter 2: AVG_SUM_ONLY_NUMERIC

I am getting this error message on my query formula
"Unable to parse query string for Function QUERY parameter 2:
AVG_SUM_ONLY_NUMERIC"
and I don't know what seems to be the problem. I already change the format of column C to percentage but I'm still getting the same error.
=Query('Sheet1'!A1:C, "select A, avg(C), count(C) group by A,C", 1)
Sample Data:
Date | Name | CSAT %|
-----------|------|-------|
2017-10-22| asdf | 100%|
2017-10-15| qwer | 50% |
2017-10-08| zxcv | 75% |
2017-10-01| qwer | 90% |
One column of numerical data in my sheet would not register as numbers, no matter what (you can test this with function N(). It will show 0 for data which isn't considered numerical).
I fixed this with an extra column which added 0 to my numerical column. Even though the original column wasn't considered numerical, this somehow worked.
I finally solved the puzzle using this:
=arrayformula(query({'Sheet1'!A:N, arrayformula(if(ISBLANK('Sheet1'!O:O), -1, value('Sheet1'!O:O)))
Basically, I put -1 on blank spaces in between rows and then on my query, I placed a condition to include only rows that have a value of >=0.

How join other sheet to main sheet like DB relation?

I have a sheet name 'test'
code | count
HHHD |
YTSS |
YDUS |
and i have a second sheet name 'abc'
code | count | name
HHHD | 1 | sts
YTSS | 2 | other_name
YDUS | 6 | other_name_2
How create array formula or which function use to make in test B2 cell when code from test will exist in abc.cell then get data abc.count and insert to test.count
How can i do this ?
Classic VLOOKUP case:
But, I'll use LOOKUP here
Test B2=
=ARRAYFORMULA(LOOKUP(A2:A7,'abc'!A2:B7))
The QUERY function offers a short, readable way to analyze data. This function uses the principles of Structured Query Language (SQL) to do searches.
Parts of a QUERY formula
A QUERY formula is formatted as =QUERY(data, query, [headers]).
Full Documentation : Here

Trying to loop through all cells that start with the contents of another cell using ARRAYFORMULA and RegExMatch

I have a Google Spreadsheet setup; it's for a product import. I am trying to loop through all rows, when C2:C == "configurable" I want it to join (with a comma) all B2:B cells that start with the contents of B in the current "configurable" row.
For example, the table may look like this.
Name | SKU | Type | Simple Products
Prod 1 | 001 | configurable | 001.01,001.02
Prod 2 | 001.01 | simple |
Prod 3 | 001.02 | simple |
Prod 4 | 002 | configurable | 002.01,002.02,002.03
Prod 5 | 002.01 | simple |
Prod 6 | 002.02 | simple |
Prod 7 | 002.03 | simple |
The "Simple Products" column (D2) is where the formula would be located. It will scan each row, if C* = configurable, then it will use the B* value to find all "simple" SKUs that start with the value in B*.
I have it almost working, using the following formula:
=ARRAYFORMULA(
IF(
C3:C="configurable",
join(
",",
transpose(
iferror(
filter(
B3:B,
C3:C="simple",
RegExMatch(B3:B, B3)
)
)
)
),
""
)
)
However, the above always uses B3 for the RegExMatch, so every configurable row contains 001.01,001.02 rather than the row it is currently outputting data in.
Try:
=ArrayFormula(IF(C2:C="configurable",REGEXREPLACE(TRIM(REGEXEXTRACT(QUERY(B2:B,,ROWS(B2:B))&" ","(?:"&B2:B&"\.[0-9]+ )+"))," ",","),))
Although there's a couple of assumptions made based on your sample data (eg the SKUs are sorted).
The issue with your attempt is that the FILTER function can't iterate down the column.
Credit to Isai Alvarado who came up with the QUERY method of building very large strings; larger than the 50k character limit that is normally imposed.

Resources