I have this table:
Name | Age
Ann | adult
Ann | adult
Andrew | adult
Mike | adult
Ann | teenager
John | teenager
John | teenager
I want this output:
Age | count Name (distinct Names)
adult | 3
teenager | 2
Unfortunately, I can't go further then this formula:
=QUERY(table; "select B, count(A) group by B"; 1)
where the 'table' is the named range with input data. And it gives me this:
Age | count Name
adult | 4
teenager | 3
I need something like:
=QUERY(table; "select B, count(unique(A)) group by A"; 1)
which obviously doesn't work.
So, how can I achieve my target output with querying?
I know, I can do that with pivot tables with countunique function, but I want to go without pivot tables.
One option could be QUERY + UNIQUE:
=QUERY(UNIQUE(A2:B),"SELECT Col2, COUNT(Col1) WHERE Col2 IS NOT NULL GROUP BY Col2")
You can also make use of this formula:
=ARRAYFORMULA(QUERY(UNIQUE({B:B, B:B & A:A, A:A}), "SELECT Col1, COUNT(Col1) WHERE Col1 IS NOT NULL GROUP BY Col1 ORDER BY COUNT(Col1) DESC LABEL COUNT(Col1)'count Name'", 1))
After
Explanation
The formula makes use of the following functions:
ARRAYFORMULA
UNIQUE
QUERY
In order to find the unique values, the UNIQUE is used for the range needed for the query (A:B) such that the sorting and counting is done on this range. The LABEL is used as well in order to set the header name for the resulted column.
Reference
ARRAYFORMULA;
UNIQUE;
QUERY.
Related
I want to merge data (name + value/s) in different rows based on another column (name). See example below:
name | value
A | ab
A | ac
A | ad
B | bc
B | bd
I want the output to be like this (in rows and concatenated in one cell):
A ab ac ad
B bc bd
Is this possible with Google sheets query?
I have added a new sheet ("Erik Help"). To achieve the exact results you wanted:
=ArrayFormula({"Result";IF(A2:A="",,IF(COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))>1,,VLOOKUP(A2:A&"*",REGEXREPLACE(TRIM(SPLIT(FLATTEN(QUERY(QUERY({A2:B}, "Select MAX(Col2) where Col2 IS NOT NULL GROUP BY Col2 PIVOT Col1"),, 9^9)),"~")),"[,\s]+$",""),1,FALSE)))})
This is a more complex formula than I can typically share on this free forum, so I will need to leave the understanding of the formula to you and others who may be interested.
I also left a second formula, which shows only the aggregated list, as a "bonus" for you:
=ArrayFormula({"Aggregated List";REGEXREPLACE(TRIM(SPLIT(FLATTEN(QUERY(QUERY({A2:B}, "Select MAX(Col2) where Col2 IS NOT NULL GROUP BY Col2 PIVOT Col1"),, 9^9)),"~")),"[,\s]+$","")})
How do I list and count unique comma-separated values (column B in the example below) if the number in column A is larger (or smaller) than X? In other words, how do I turn the below table...
Day | Fruits
+--------|--------------------------+
|
20 | Apple, Banana, Pearl
|
24 | Apple, Pearl
|
32 | Banana, Pearl
+
...into this 👇, with criteria: Day < 28.
Fruit | Frequency
+----------|---------------+
|
Apple | 2
|
Pearl | 2
|
Banana | 1
+
A solution proposed by #AdamL in this question is really close to what I want to achieve, but I can't figure out how to list values based on criteria from another column. Here's what Adam came up with:
=ArrayFormula(QUERY(TRANSPOSE(SPLIT(JOIN(",",A:A),",")&{"";""}),"select Col1, count(Col2) group by Col1 label count(Col2) ''",0))
use:
=ARRAYFORMULA(QUERY(SPLIT(FLATTEN(
IF(IFERROR(SPLIT(B2:B, ","))="",,A2:A&"♦"&TRIM(SPLIT(B2:B, ",")))), "♦"),
"select Col2,count(Col2)
where Col1 < 28
and Col1 is not null
group by Col2
label count(Col2)''"))
I think it's a bit easier to filter on column A first then split:
=ArrayFormula(query(flatten(split(filter(B2:B,A2:A<28,A2:A<>""),",")),"select Col1,count(Col1) where Col1 is not null group by Col1 label Col1 'Fruit',Count(Col1) 'Count'"))
Considering Column 'A' as days and Column 'B' as fruits with row 1 as headers, AdamL's formula works for you with a little tweak as below:
=ArrayFormula(QUERY(TRANSPOSE(SPLIT(JOIN(",",B2:B),", ",True)&{"";""}),"select Col1, count(Col2) group by Col1 label count(Col2) ''",0))
Hope this solves your problem.
I have a table similar to the one below and like to automate the calculation of the sum column. The number of rows per day varies. I'm looking for a way to find the number of empty cells in the date column after the current row. This number can then be used to fill the sum column.
Is there regardless of the solution below a way to count the number of empty cells between the dates?
Date |Value|Sum
----------+-----+---
16/07/2020| 2| 5
| 3|
17/07/2020| 2| 10
| 3|
| 5|
18/07/2020| 2| 11
| 3|
| 5|
| 1|
if you start from row 1 use:
=ARRAYFORMULA(IF(A:A="",,VLOOKUP(A:A, QUERY({VLOOKUP(ROW(A:A),
FILTER({ROW(A:A), A:A}, A:A<>""), 2), B:B},
"select Col1,sum(Col2) group by Col1"), 2, 0)))
=ARRAYFORMULA(IFNA(VLOOKUP(A:A, QUERY(IF(B:B="",,VLOOKUP(ROW(A:A),
IF(A:A<>"", {ROW(A:A), A:A}), 2, 1)),
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''"), 2, 0)))
I don't think you need the answer to your first question to figure out the answer to the Sum.
With the entirety of column C blank, try this in C1:
=ARRAYFORMULA({"Sum";IF(A2:A="",,VLOOKUP(A2:A,QUERY({VLOOKUP(ROW(A2:A),FILTER({ROW(A2:A),A2:A},A2:A<>""),2),B2:B},"Select Col1,SUM(Col2) group by Col1"),2,0))})
If that doesn't work, it might be easier to demonstrate the idea on a sample sheet.
André, try this:
1.) Delete C:C entirely (including the header).
2.) Place the following formula into cell C1:
=ArrayFormula({"Sum";IF(A2:A="","",VLOOKUP(A2:A,QUERY({VLOOKUP(ROW(A2:A),FILTER({ROW(A2:A),A2:A},A2:A<>"",B2:B<>""),2,TRUE),B2:B},"Select Col1, SUM(Col2) Group By Col1"),2,FALSE))})
UPDATE:
Your post example shows headers. The formula I suggested, then, accounted for those headers. Since your actual sample sheet is different and does not use headers like the original post, you'd use this version:
=ArrayFormula(IF(A:A="","",VLOOKUP(A:A,QUERY({VLOOKUP(ROW(A:A),FILTER({ROW(A:A),A:A},A:A<>"",B:B<>""),2,TRUE),B:B},"Select Col1, SUM(Col2) Group By Col1"),2,FALSE)))
So I am using a query function to count the number of instances a particular name appears in column A of another sheet, and display that result in Column B of this sheet with the respective name in Column A. Here is the function:
=ArrayFormula(QUERY(Attendance!A:A&{"",""},"select Col1, count(Col2) where Col1 != '' group by Col1 label count(Col2) 'Count'",1))
The problem is, while it works for the most part, some of the names appear twice, for instance Fred Jones appears as:
Col A | Col B
Fred Jones | 5
Fred Jones | 2
I have looked at the names, and there is no discernible difference between them, I do not understand why it is not grouping. Is there a way I can use wildcard or something to get Google to combine the names if they are nearly identical? Any help would be appreciated, thanks as always.
try:
=ARRAYFORMULA(QUERY(TRIM({Attendance!A:A}),
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)'Count'", 1))
I have a table that contains information on people who volunteered for multiple organizations over a one-month period. One person could have volunteered for the same organization on multiple dates, and multiple people could have volunteered on the same date. I'm interested in tracking, for each organization, unique volunteers (individuals who have volunteered at any point over the course of the month) and unique shifts (for the purposes of this data, a shift is one person volunteering on a unique date).
How can I pull the unique shifts metric? Here's an example of the type of data I'm dealing with:
Organization | Date | Volunteer
A | 10-01-2018 | Jane
A | 10-01-2018 | Ben
A | 10-02-2018 | Jane
B | 10-01-2018 | Emily
B | 10-02-2018 | Jack
You can see that Organization A has 2 unique volunteers and 3 shifts, while Organization B has 2 unique volunteers and 2 shifts. I'm able to get the unique volunteer count by using countuniqueifs with the organization as the criterion, but to get the number of shifts I need both the "Date" and "Volunteer" columns to be unique, while keeping the organization as the criterion.
I'm interested in tracking, for each organization, unique volunteers
=UNIQUE({A2:A, C2:C})
=QUERY(UNIQUE({A2:A, C2:C}),
"select Col1,count(Col2)
where Col1 is not null
group by Col1
label count(Col2)''", 0)
(individuals who have volunteered at any point over the course of the month)
=QUERY(UNIQUE(FILTER({A2:A, C2:C}, MONTH(B2:B)=10)),
"select Col1,count(Col2)
where Col1 is not null
group by Col1
label count(Col2)''", 0)
and unique shifts (for the purposes of this data, a shift is one person volunteering on a unique date)
=QUERY(UNIQUE(FILTER({A2:A, B2:B&C2:C}, MONTH(B2:B)=10)),
"select Col1,count(Col2)
where Col1 is not null
group by Col1
label count(Col2)''", 0)