COUNTIF with more than one matching string in Google Spreadsheets - google-sheets

I'm wondering how I can use COUNTIF with more than one matching string in Google Spreadsheets.
The following is the important part, I would like to count in the spreadsheet those records that contain "BAIRRO NOVO" and "Externo". These strings appear in different columns, not in the same cell.
=COUNTIF(IMPORTRANGE("10OAEb2fBfvAqCdp1yyuTBQ4NErtxtOyJ29whFkvVqaw";"Data!B:P");"BAIRRO NOVO")

This could be done with countifs but only if the data was placed in the sheet: i.e., importrange(...) is done and then countifs refers to its columns like
=countifs(C:C, "BAIRRO NOVO", G:G, "Externo")
To get this count without putting all the data in the current spreadsheet, use query
=query(importrange(...), "select count(Col1) where Col2 = 'BAIRRO NOVO' and Col7 = 'Externo'", 1)
Here Col1, Col2, ... are columns of the imported range; in your case Col2 is C, for example, because the range begins with B. The last parameter "1" is the number of header rows the queried range has: typically 0 or 1.
To make sure the output is a single cell, without a header like "count(something)", add an empty label as follows.
=query(importrange(...), "select count(Col1) where Col2 = 'BAIRRO NOVO' and Col7 = 'Externo' label count(Col1) ''", 1)
To have the query output 0 instead of #N/A when nothing is found, wrap it in iferror(..., 0):
=iferror(query(importrange(...), "select count(Col1) where Col2 = 'BAIRRO NOVO' and Col7 = 'Externo' label count(Col1) ''", 1), 0)

Related

Split each row in a column whose value contains commas into transposed rows

So I have data in the following format:
I want to summarize this into another sheet like this:
So I can use TRANSPOSE(SPLIT(A1:A)) to do this individually
I can use ARRAYFORMULA(SPLIT(A:A)) to run this over a range, however I cant seem to use both of them together to the same effect
To summarize
Column A contains comma seperated values for car names
I want to get all the car names as rows in another sheet and add counts against them
You can get the whole mini-report with this one formula:
=ArrayFormula(QUERY(FLATTEN(TRIM(SPLIT(A2:A,","))),"Select Col1, COUNT(Col1) Where Col1 > '#' GROUP BY Col1 LABEL COUNT(Col1) ''"))
SPLIT will split every entry in A2:A at the comma (if there is one) and will result in an error for every blank row (dealt with later).
TRIM removes the spaces after any comma-splits.
FLATTEN creates one column from all results (words, blanks and errors).
QUERY(...,"Select Col1, COUNT(Col1) Where Col1 > '#' GROUP BY Col1 LABEL COUNT(Col1) ''") starts with everything in that FLATTEN column and a COUNT of each GROUPed by the name ruling out anything WHERE the value in Col1 is not > the '#' symbol (which will rule out all blanks and errors). Finally, LABEL will get rid of the superfluous header of "count" that would otherwise be generated for the COUNT(Col1) column of the QUERY.
Alternatively, you could rule out the errors first with a FILTER and then use the WHERE part of the QUERY to rule out blanks:
=ArrayFormula(QUERY(FLATTEN(TRIM(SPLIT(FILTER(A2:A,A2:A<>""),","))),"Select Col1, COUNT(Col1) WHERE Col1 Is Not Null GROUP BY Col1 LABEL COUNT(Col1) ''"))
Managed to solve it from this question
https://webapps.stackexchange.com/questions/114791/google-sheets-split-and-unique
=unique(transpose(arrayformula(trim(split(join(",",A1:A),",")))))
The most efficient is probably QUERY. Unlike JOIN, this has no limit on the number of characters in the result.
Basic approach
=ARRAYFORMULA(TRANSPOSE(SPLIT(
QUERY(A:A&",",,9^9),
", "
)))
Unique
=ARRAYFORMULA(UNIQUE(TRANSPOSE(SPLIT(
QUERY(A:A&",",,9^9),
", "
))))
Counting the number in a group
=ARRAYFORMULA(QUERY(
TRANSPOSE(SPLIT(QUERY(A:A&",",,9^9),", ")),
"select Col1, count(Col1) group by Col1 label count(Col1)''"
))

Google Sheets - How to COUNTIF multiple values in each one cell of column

Each cell has multiple values that separated by comma and I want to count a value.
Let say I want to count Mapping Your Future. How to do that?
delete everything you got and use:
=INDEX(QUERY(TRIM(FLATTEN(SPLIT(TEXTJOIN(",", 1, questionnaire!G:G), ","))),
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''"))

Google Sheets Formula to Count Occurrences of Unique Values in Columns

I'm looking for a formula, =query possibly, that will turn this:
Into this:
I used the following formula successfully to get the AFTER picture, but I can't get the counts. I tried using concatenate but there isn't a way to do that to each item in the array. Maybe there is a way to do this using a query?
=transpose(split(join(",",unique(C1:C98)),","))
And here's my flavour of it, which also needs to be dragged to the right, to fill the other columns:
=ARRAYFORMULA(QUERY(
{UNIQUE(A$2:A) & " - " & COUNTIF(A$2:A,UNIQUE(A$2:A))},
"where Col1<>' - 0'",0))
Yes, you can use query to get the unique values and counts then join the two columns
=ArrayFormula(index(query(A2:A,"select A,count(A) where A is not null group by A label count(A) ''"),0,1)&"-"&index(query(A2:A,"select A,count(A) where A is not null group by A label count(A) ''"),0,2))
Apologies, in order to drag it across you would have to put:
=ArrayFormula(index(query({A2:A},"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"),0,1)
&"-"&index(query({A2:A},"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"),0,2))
Try the following in cell A2 and drag to the right.
=ARRAY_CONSTRAIN(ArrayFormula(IFERROR(CONCAT(UNIQUE(A2:A99)&" - ",
ArrayFormula(COUNTIFS(A2:A99,UNIQUE(A2:A99)))))),COUNTUNIQUE(A2:A99),1)
Also using a query
=ArrayFormula(index(query({A2:A},"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"),0,1)&"-"&
index(query({A2:A},"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"),0,2))
The only difference is that using the first formula you maintain the names within the order they appear, while using the query they get sorted alphabetically.
Functions used:
ARRAY_CONSTRAIN
ArrayFormula
IFERROR
CONCAT
UNIQUE
COUNTIFS
COUNTUNIQUE

Filter IMPORTHTML data

When I import data, it comes in this format (image 1), with blank spaces. I would like to know if there is any way to adjust so that these blanks disappear, the two models expected (image 2 and 3) if there was any way to reach them would be important to me.
Remembering that all dates have / and all times have :
I tried to filter from QUERY, but when trying to "Select Col1, Col2, Col4 Where Col2 is not null" the dates disappear and only the times remain, I tried via REGEXMATCH to separate the dates from the times using / and : but also I was not successful.
I also tried it via IMPORTXML, but some data ends up not being imported correctly on some pages of the site, for IMPORTHTML these errors do not happen. The XML's I used were:
"//tr[#class='no-date-repetition-new' and ..//td[#class='team team-a']] | //tr[#class='no-date-repetition-new live-now' and ..//td[#class='team team-a']]"
"//td[#class='team team-a']/a | //td[#class='team team-a strong']/a"
The current formula is as follows:
=IMPORTHTML("https://int.soccerway.com/national/austria/1-liga/20192020/regular-season/r54328/","table",1)
IMPORTHTML Original:
Expected formats:
---
Rather than filtering what you need is to restructure the imported data.
Anyway, I think that the easier solution to get the final result is to use multiple IMPORTXML formulas.
URL
A1: https://int.soccerway.com/national/austria/1-liga/20192020/regular-season/r54328/
Headers
A2: //table[contains(#class,'matches')]/thead/tr/th
Day
A3: //td[contains(#class,'date')]/parent::tr
Teams and Score
A4: //td[contains(#class,'team-a')]/parent::tr
A6: =transpose(IMPORTXML($A$1,A2))
A7: =IMPORTXML($A$1,A3)
B7: =IMPORTXML(A1,A4)
You might want to replace the formula on A6 by static values in order to place them properly.
You can join 2 queries together (one next to the other) in a single formula, to get your results
={QUERY(IMPORTHTML("https://int.soccerway.com/national/austria/1-liga/20192020/regular-season/r54328/","table",1),
"select Col1 where Col2 is null and not Col1 contains '*'",1),
QUERY(IMPORTHTML("https://int.soccerway.com/national/austria/1-liga/20192020/regular-season/r54328/","table",1),
"select Col1, Col2, Col3, Col4 where Col2 is not null label Col1 'Time'",1)}
How the formula works:
As you notice the data part of both queries is the same in both of them. What is actually different is "what we ask for from the query"
In the first one we use "select Col1 where Col2 is null and not Col1 contains '*'"
In the second one "select Col1, Col2, Col3, Col4 where Col2 is not null label Col1 'Time'"
We create an array by joining them together as in ={1stQUERY,2ndQUERY}

How to use ARRAYFORMULA in query where clause?

In cell C1:C of my table I got 6 rows with ticket id's. I like to search different spreadsheets to search for those ticket id's and calculate the total hours spent on that ticket.
I have it working using the following formula:
=QUERY({IMPORTRANGE("SPREADSHEETID";"B3:B")\ARRAYFORMULA(TO_PURE_NUMBER(IMPORTRANGE("SPREADSHEETID";"F3:F")-IMPORTRANGE("SPREADSHEETID";"E3:E")))};"SELECT SUM(Col2) WHERE Col1 = '"&C1&"' GROUP BY Col1 LABEL SUM(Col2) ''")
In this example, C1 is where the ticket ID can be found.
Now I thought I could just wrap QUERY in a ARRAYFORMULA and use C1:C instead of just C1 but that won't work. Now I could just copy and paste the above formula in every cell but there must be a cleaner way.
ANSWER
I used the following formula to make it work, thanks to Max's answer below.
=QUERY(
{
IMPORTRANGE("SPREADSHEETID";"B3:B")\
ARRAYFORMULA(
TO_PURE_NUMBER(
IMPORTRANGE("SPREADSHEETID";"F3:F") - IMPORTRANGE("SPREADSHEETID";"E3:E")
)
)
};
"
SELECT Col1, SUM(Col2)
WHERE Col1 = '" & JOIN("' OR Col1 = '";FILTER(C:C; C:C <> "")) & "'
GROUP BY Col1
LABEL SUM(Col2) ''
")
Sample formula is:
=QUERY({A:B},"select * where Col1 = '"&JOIN("' or Col1 = '",FILTER(D2:D,D2:D<>""))&"'")
No, one cannot create an array of query strings and use arrayformula(query(...)) to run them all at once.
Alternative: instead of
SELECT SUM(Col2) WHERE Col1 = '"&C1&"' GROUP BY Col1 LABEL SUM(Col2) ''
use the query
SELECT Col1, SUM(Col2) GROUP BY Col1
elsewhere on the sheet, and then use vlookup to look up the sum for each value of Col1 that you want. vlookup can be used inside of arrayformula like this:
=arrayformula(vlookup(C1:C10, E:F, 2, 0))
looks up each of values in C1..C10 in the column E (exact match required) and returns the corresponding value in column F (2nd column of the searched range).

Resources