Google Sheets formula with variable number of disjoint columns - google-sheets

In the table I have multiple disjoint columns with similar meaning: “is this row interesting?”
I want to create an array formula to get all interesting rows. How can I approach this?
Example table:
Obj id | Case 1 data | Case 1 interesting? | Case 2 data | Case 2 interesting?
1 | … | YES | … | NO
2 | … | NO | … | NO
3 | … | NO | … | YES
4 | … | NO | … | NO
5 | … | YES | … | YES
6 | … | NO | … | NO
The actual table is split into several sheets with different subsets of ids on each sheet.
My current approach is stuck with INDIRECT function not accepting array or ranges. I first search for my columns: FILTER(COLUMN(A1:1), REGEXMATCH(A1:1, "interesting")), then I convert column addresses to ranges, but when I feed the result to INDIRECT, it only returns the first column.
The desired formula would output an array of unique object ids where each row is interesting for at least one case.
UPDATE: here is a test table for this problem. There are 3 sheets: student's data with ids and 2 programs. Each program has several exams (not known beforehand. The desired formula would output an array of unique student ids with at least 1 Passed exam (in the test sheet: 1, 3, 4, 6)

={"Passing"; ARRAYFORMULA(UNIQUE(QUERY({
IF(IFERROR(REGEXEXTRACT(REGEXREPLACE(TRIM(TRANSPOSE(QUERY(
TRANSPOSE('Program 1'!A1:Z), , 999^99))), "Pass", "♠"), "♠"))="♠", 'Program 1'!A1:A, );
IF(IFERROR(REGEXEXTRACT(REGEXREPLACE(TRIM(TRANSPOSE(QUERY(
TRANSPOSE('Program 2'!A1:Z), , 999^99))), "Pass", "♠"), "♠"))="♠", 'Program 2'!A1:A, )},
"where Col1 is not null order by Col1", 0)))}
if you want to VLOOKUP it:
=ARRAYFORMULA(IF(LEN(A2:A), IF(IFERROR(VLOOKUP(A2:A, UNIQUE(QUERY({
IF(IFERROR(REGEXEXTRACT(REGEXREPLACE(TRIM(TRANSPOSE(QUERY(
TRANSPOSE('Program 1'!A1:Z), , 999^99))), "Pass", "♠"), "♠"))="♠", 'Program 1'!A1:A, );
IF(IFERROR(REGEXEXTRACT(REGEXREPLACE(TRIM(TRANSPOSE(QUERY(
TRANSPOSE('Program 2'!A1:Z), , 999^99))), "Pass", "♠"), "♠"))="♠", 'Program 2'!A1:A, )},
"where Col1 is not null", 0)), 1, 0))<>"", "PASS", "FAIL"), ))

Related

How to transpose & split multiple columns and repeat specific cells in a column

I am looking to transpose, split, and keep the correct corresponding Category/Reference Number.
Column A: Category / Reference Number.
Column B: Email (CSV)
| A | B | | A | B |
|001|Email1,Email2,Email3| |001|Email1|
|002|Email4,Email5,Email6| |001|Email2|
| | | |001|Email3|
| | | |002|Email4|
| | | |002|Email5|
| | | |002|Email6|
Here is another post which is similar to what I am looking to accomplish. The only difference is in this post, the OP requested that the formula duplicates data X times. Here is the formula that is used:
=ARRAYFORMULA({TRANSPOSE(SPLIT(CONCATENATE(REPT(B2:B&",", A2:A^2)), ",")),
TRANSPOSE(SPLIT(CONCATENATE(REPT(C2:C&",", A2:A)), ","))})
I have tried modifying this formula by removing the "^2", "A2:A" replacing with a COUNTIF (to determine the number of emails in each row), and keep breaking the formula.
What am I doing wrong?
Here is my sheet.
try:
=ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT('Form Responses'!C2:C, ","))="",,
'Form Responses'!B2:B&"×"&SPLIT('Form Responses'!C2:C, ","))), "×"),
"where Col2 is not null")))

use ARRAYFORMULA to find all matching rows/values in a source table from a lookup table

I have a source table like so. As you can see, each each Lookup Value can have one or more Result.
| Lookup Value | Result |
|--------------|--------|
| a | a1 |
| a | a2 |
| a | a3 |
| b | b1 |
| b | b2 |
| c | c1 |
| c | c2 |
Then I have an input table like so:
| queries | results | | | | | |
|---------|---------|---------|---------|---------|---------|---------|
| a | ... | ... | ... | ... | ... | ... |
| c | ... | ... | ... | ... | ... | ... |
The ... for each row should be the transposed values from the lookup table. So, for example, the above table would look like this:
| queries | results | | |
|---------|---------|----|----|
| a | a1 | a2 | a3 |
| c | c1 | c2 | |
Right now I have to use multiple formulas like so:
I am trying to replace it with a single ARRAYFORMULA but it doesn't seem to work.
Is there another way to do this? Basically lookup all the matching rows from a lookup table and then transpose them?
Suppose your "Lookup Value" and "Result" data run from A1:B (with headers in A1 and B1). And suppose that your "queries" list is in D1:D (header in D1) with the "results" header in E1.
Depending on the maximum number of possible matches in B:B for any value in A:A, you could use this in E2:
=ArrayFormula(IFERROR(VLOOKUP(D2:D,QUERY(FILTER({A2:B,COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))},A2:A<>""),"Select Col1, MAX(Col2) Group By Col1 Pivot Col3"),SEQUENCE(1,10,2),0)))
If your maximum possible matches is fewer than 10 or more than 10, feel free to edit the second argument of the SEQUENCE function accordingly.
Understand that, with such an array formula that is asked to process a range, you wouldn't be able to put other data anywhere below or to the right of your "queries and results" that you've asked the array formula to assess or fill. So if you want data under it, you'll need to limit your VLOOKUP from D2:D to, say, D2:D50 (or whatever your max queries range would be). Likewise, if that second argument of the SEQUENCE function is 10, you'll have "reserved" 10 columns (i.e., E:N) for possible results, and you won't be able to put data there or you'll "break" the array formula. That being the case, you may want to give yourself some sort of visual line of demarcation around the area you've reserved for the formula's use (e.g., change the background color of the block or place a border around it, etc.).
try:
=ARRAYFORMULA(IFERROR(VLOOKUP(D:D, SPLIT(TRANSPOSE(QUERY(TRANSPOSE(IF(ISNUMBER(
QUERY(QUERY(A:B, "select A,count(A) where A is not null group by A pivot B"), "offset 1", 0)),
QUERY(QUERY(A:B, "select A,count(A) where A is not null group by A pivot B"), "limit 0", 1),
QUERY(QUERY(A:B, "select A,count(A) where A is not null group by A pivot B"), "offset 1", 0)))
,,999^99)), " "), TRANSPOSE(ROW(INDIRECT("A2:A"&COUNTUNIQUE(B:B)))), 0)))

Google Sheets function to group and concat rows

Sample sheet: https://docs.google.com/spreadsheets/d/1AeP0sxDi0-3aaesUdCNTKfricIimjTMFaKO-FX9_g50/edit?usp=sharing
I am trying to find a formula that will group a table on a column and concat the values from all the rows in another column.
For example, if this is my table:
| name | value |
|-------|---------|
| one | alpha |
| two | bravo |
| three | charlie |
| one | delta |
| two | echo |
| four | foxtrot |
| two | golf |
| three | hotel |
| four | india |
This is what I want the formula to output:
| one | alpha, delta |
| two | bravo, echo, golf |
| three | charlie, hotel |
| four | foxtrot, india |
I wish I could share some formula that gets me close but I can't find anything. I thought maybe this formula but, as you can see from the sample sheet, it does not work.
=ARRAYFORMULA(JOIN(", ", TRANSPOSE(FILTER(B2:B, A2:A = {UNIQUE(A2:A)}))))
My thought was, get a unique list of values in the name column, and then use arrayformula to get a list of values in the value column where the name column equals each value in the unique list. :/
try:
=ARRAYFORMULA(REGEXREPLACE(TRIM(SPLIT(TRANSPOSE(
QUERY(QUERY({A2:A&"♦", B2:B&","},
"select max(Col2)
where Col1 !=''
group by Col2
pivot Col1"),,999^99)), "♦")), ",$", ))
or:
=ARRAYFORMULA(IFNA(VLOOKUP(UNIQUE(A2:A),
REGEXREPLACE(TRIM(SPLIT(TRANSPOSE(
QUERY(QUERY({A2:A&"♦", B2:B&","},
"select max(Col2)
where Col1 !=''
group by Col2
pivot Col1"),,999^99)), "♦")), ",$", ), {1, 2}, 0)))

In Google Sheets How do I get a sum of comma separated values in rows against an ID

I have a table which has a list of id's against names
Sheet 1
A | B
1 | Joe
12 | Dave
23 | Pete
I then have a table of rows which shows when a person was present at an event (through their ID)
Sheet 2
A | B
boston | 1
florida | 1,12
nyc | 12,23
In the 3rd sheet for appearances, I am then looking to achieve the following
Sheet 3
A | B (Appearances)
Joe | 2
Dave | 2
Pete | 1
I can get this to work when just one person makes an appearance with something like =COUNTIF(appearances!A:A, INDEX(name_db!$A$2:$A$1000, MATCH ($A11, name_db!$B$2:$B$1000, 0)))
But as soon as I add a comma value it all goes wrong.
I've tried looking into vLOOKUPS and things like that but can't seem to quite figure it out
Any help on where to look would be much appreciated
=ARRAYFORMULA(IFERROR(VLOOKUP(G:G, QUERY(VLOOKUP(TRANSPOSE(SPLIT(CONCATENATE(
IF(IFERROR(SPLIT(E:E, ","))<>"", "♦"&SPLIT(E:E, ","), )), "♦")), A:B, 2, 0),
"select Col1,count(Col1) group by Col1 label count(Col1)''", 0), 2, 0)))
This works for me:
=ARRAYFORMULA(IFERROR(SUM(SPLIT(D2;","))))

Counting number of occurrences in column?

What would be a good approach to calculate the number of occurrences in a spreadsheet column? Can this be done with a single array formula?
Example (column A is input, columns B and C are to be auto-generated):
| A | B | C |
+-------+-------+-------+
| Name | Name | Count |
+-------+-------+-------+
| Joe | Joe | 2 |
| Lisa | Lisa | 3 |
| Jenny | Jenny | 2 |
| Lisa | | |
| Lisa | | |
| Joe | | |
| Jenny | | |
A simpler approach to this
At the beginning of column B, type
=UNIQUE(A:A)
Then in column C, use
=COUNTIF(A:A, B1)
and copy them in all row column C.
Edit: If that doesn't work for you, try using semicolon instead of comma:
=COUNTIF(A:A; B1)
Try:
=ArrayFormula(QUERY(A:A&{"",""};"select Col1, count(Col2) where Col1 != '' group by Col1 label count(Col2) 'Count'";1))
22/07/2014 Some time in the last month, Sheets has started supporting more flexible concatenation of arrays, using an embedded array. So the solution may be shortened slightly to:
=QUERY({A:A,A:A},"select Col1, count(Col2) where Col1 != '' group by Col1 label count(Col2) 'Count'",1)
=COUNTIF(A:A;"lisa")
You can replace the criteria with cell references from Column B
Just adding some extra sorting if needed
=QUERY(A2:A,"select A, count(A) where A is not null group by A order by count(A) DESC label A 'Name', count(A) 'Count'",-1)
=arrayformula(if(isblank(B2:B),iferror(1/0),mmult(sign(B2:B=TRANSPOSE(A2:A)),A2:A)))
I got this from a good tutorial - can't remember the title - probably about using MMult
Put the following in B3 (credit to #Alexander-Ivanov for the countif condition):
={UNIQUE(A3:A),ARRAYFORMULA(COUNTIF(UNIQUE(A3:A),"=" & UNIQUE(A3:A)))}
Benefits: It only requires editing 1 cell, it includes the name filtered by uniqueness, and it is concise.
Downside: it runs the unique function 3x
To use the unique function only once, split it into 2 cells:
B3: =UNIQUE(A3:A)
C3: =ARRAYFORMULA(COUNTIF(B3:B,"=" & B3:B))

Resources