Find query matches for a multi-row input - google-sheets

Basically I have a set of CSV that I want to search through using a key from outside the query area. The query data is arranged like:
| COLUMN L | COLUMN K |
|----------|----------|
| A,B,C,D | 1 |
| E,F,G,H | 2 |
| I,J,K | 3 |
etc
What I have so far is
query(A1:L12,"Select L where K matches '(?:^|,)"&A13&"(?:,.*|$)'",-1)
So if A13's value is G, 2 is returned.
This works fine and returns the correct row value where it matches A13, but what I actually want to do is for this to process a column of data and return results for each key (or A13 value), but I can't work out how to translate this into "arrayformula" format. I tried this:
query(A1:L12,"Select L where K matches '(?:^|,)"&A13:A24&"(?:,.*|$)'",-1)
But no luck.

Related

How to use ARRAYFORMULA to work with an array of array

I'm currently working on a formula but I can't seem to make it work the way I intend to.
The column A of the spreadsheet look like something like that:
| A |
| B |
| C |
| D |
| E |
And what I am trying to do is get an output like that:
| A |
| A | B |
| A | B | C |
| A | B | C | D |
| A | B | C | D | E |
So I tried using offset, to get from the first line to the current line for each of my lines.
=ARRAYFORMULA(TRANSPOSE(OFFSET(A1;0;0;ROW(A1:A5)))
But since ROW(A1:A5) doesn't return an array the cell was just | A |
So I tried adding ARRAYFORMULA around the ROW(A1:A5) and what I go was:
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
Which is what I need! But if I add it to the original function, I still only get | A |
Is there a way for me to "force" ARRAYFORMULA to run on A1:A5 so that I get the required output?
Additionnal data
I know this would be easier to do with a script but a script solution is not what I'm looking for. I'm trying to understand why it does that and how to prevent it.
This formula is meant to be used inside another so I need it to be only 1 formula, I can't use the cross to expand it.
I don't mind if the solution isn't showable (if the array superpose itself by example).
I made a demo here.
I also tried with INDIRECT instead of OFFSET to get the ranges and I had the same results.
Please try, somewhere in the Row1, assuming that is where your first A is, and copied down to suit:
=SPLIT(JOIN("|",A$1:A1),"|")
(as an array formula, if you must.)
=ARRAYFORMULA(IF(ROW(A2:A6)>=TRANSPOSE(ROW(A2:A6));TRANSPOSE(A2:A6);))
ROW() is used to create series of vertical numbers,which are compared against the same series of numbers horizontally to create a 5x5 matrix of TRUE/FALSE,which can then easily be extrapolated to the desired output.
Assuming your data is located at A1:A8 enter this formula at C1 then copy it to C2:C8:
= TRANSPOSE ( ARRAY_CONSTRAIN( $A$1:$A$8 ,
1 + ROW() - ROW( $C$1 ) , 1 + COLUMN() - COLUMN( $C$1 ) ))

Query completed with an empty output

https://docs.google.com/spreadsheets/d/1033hNIUutMjjdwiZZ40u59Q8DvxBXYr7pcWyRRHAdXk
That's a link to the file in which it is not working! If you open it, go to sheet named "My query stinks".
The sheet called deposits has data like this in columns A (date), B (description), and C (amount):
+---+-----------+-----------------+---------+
| | A | B | C |
+---+-----------+-----------------+---------+
| 1 | 6/29/2016 | 1000000044 | 480 |
| 2 | 6/24/2016 | 1000000045 | 359.61 |
| 3 | 8/8/2016 | 201631212301237 | 11.11 |
+---+-----------+-----------------+---------+
The sheet "My Query Stinks" has data in columns A (check number), B (failing query) and C (amount):
+---+-----------------+------+--------+
| | A | B | C |
+---+-----------------+------+--------+
| 1 | 1000000044 | #N/A | 480 |
| 2 | 1000000045 | #N/A | 359.61 |
| 3 | 201631212301237 | #N/A | 11.11 |
+---+-----------------+------+--------+
In Column B on My Query Stinks, I want to enter a query. Here's what I'm trying:
=query(Deposits!A:C,"select A where A =" & A2)
For some reason, it returns "#N/A Error Query completed with an empty output." I want it to find that 1000000044 (the value in C4) matches 1000000044 over on Deposits and return the date.
Try
=query(Deposits!A:C,"select A where B ='" &A2&"'")
Explanation
Values like 1000000044 in Column B of the Deposit sheet and Column A of My Query Stinks sheets are set as text (string) values, so they should be enclosed on single quotes (apostrophes) otherwise QUERY think this values are numbers or variable names.
Try this:
=query(Deposits!A:C,"select A where B = '"&A2&"' LIMIT 1")
You'll need LIMIT 1 as you have multiple deposits for the same value in your second column.
Another solution for this problem could be to replace '=' with 'contains':
=query(Deposits!A:C,"select A where B contains '" &A2&"'")
Simple, but this error cost me half a morning.

Get a row from a range based on a single value in column

I'm using a spreadsheet to store highscores. I have one column (Initials [Column D]) and one column (Scores [Column E]). They are already sorted from highest to lowest (dependent upon the Scores). I want to get the first occurrence of all initials and that initials score.
For example if I had this:
|Initials|Scores|
| ABC | 5 |
| NOT | 4 |
| ABC | 2 |
| LOL | 1 |
I want to get this:
|Initials|Scores|
| ABC | 5 |
| NOT | 4 |
| LOL | 1 |
I've been able to get just the names portion with =UNIQUE(D:D), but how would one also get the scores from the next column? I've been trying for a while now, and can't figure it out.
Since the values in E are already sorted, try:
=ArrayFormula(vlookup(unique(filter(D2:D, len(D2:D))), D2:E, {1,2}, 0))
of if you want to use a limited range:
=ArrayFormula(vlookup(unique(D2:D50), D2:E50, {1,2}, 0))
See if that works ?

Comparing Values on the Same Row

Sorry if this has been answered but hours of Google-ing has revealed no elegant solution.
I have a sheet that looks like this only there are hundreds of rows.
+---+---+---+---+-----+
| A | B | C | D | E |
+---+---+---+---+-----+
| X | a | Y | b | 1.2 |
| X | b | Y | c | 1.5 |
| Y | c | Z | c | 1.8 |
+---+---+---+---+-----+
My goal is to count rows where for example the character in column A="X", character in column C="X" and characters in columns B and D are not the same (B!=D). The first part is working...
COUNTIFS(A:A ,"X" , C:C, "X")
but I can't figure out how to compare two cells that are both part of a range but on the same line. The following seems to compare the whole ranges...
COUNTIFS(A:A ,"X" , C:C, "X", B:B, D:D)
Additionally, I'd like to sum the values in column E for similarly defined groups.
Thanks in advance!
Solved it! Added the following formula to each row in column F...
=(B:B=D:D)+0
That will return 1 or 0 depending on whether the contents of B and D is matching. And that is something I can add to my existing formula.
UNTIFS(A:A ,"X" , C:C, "X", F:F, 0)

Google Spreadsheets: How do you concat strings in an aggregation function

Say I have a table:
A, 1
B, 1
C, 2
D, 1
E, 2
How do I view the table grouping by the 2nd column and aggregating by the first with a comma separated concat function ie:
1, "A,B,D"
2, "C,E"
In both defining a pivot table and using the QUERY syntax, it seems that the only aggregation functions available are numerical aggregations like MIN, MAX, SUM, etc. Can I define my own aggregation function?
You have to add a "Calculated Field" to the pivot table, and then select "Summarise by > Custom". This will make the column names in your formula refer to an array of values (instead of a single value). Then you can type a formula like:
= JOIN(", ", MyStringColumn)
More specifically, if you have the following table:
Create a pivot table by going to "Data > Pivot table", with the following configuration. Ensure "Summarize by" is set to "Custom"!
Another option: if the data is in A2:B, then, say, in D2:
=UNIQUE(B2:B)
and then in E2:
=JOIN(",",FILTER(A$2:A,B$2:B=D2))
which is filled down as required.
There are one-formula, auto-expanding solutions, although they get quite convoluted.
You're right, there's no easy way with pivot tables. This though, will do the trick. Inspired by this brilliant answer here.
First, have a header row and run a sort on column A to group by category.
So far, in your example, we have
| A | B
---+-----------+-----------
1 | CATEGORY | ATTRIBUTE
2 | 1 | A
3 | 1 | B
4 | 1 | D
5 | 2 | C
6 | 2 | E
In column C, let's prep the concatenated strings. Start in cell C2 with the following formula, and fill out vertically.
=IF(A2<>A1, B2, C1 & "," & B2)
...looking good...
| A | B | C
---+-----------+-----------+-----------
1 | CATEGORY | ATTRIBUTE | STRINGS
2 | 1 | A | A
3 | 1 | B | A,B
4 | 1 | D | A,B,D
5 | 2 | C | C
6 | 2 | E | C,E
In column D, let's validate the rows we want to select in a later step, with the following formula, starting in cell D2 and filling out. Basically we are marking the final category rows that carry the full concatenated strings.
=A2<>A3
...almost there now
| A | B | C | D
---+-----------+-----------+----------+-----------
1 | CATEGORY | ATTRIBUTE | STRINGS | VALIDATOR
2 | 1 | A | A | FALSE
3 | 1 | B | A,B | FALSE
4 | 1 | D | A,B,D | TRUE
5 | 2 | C | C | FALSE
6 | 2 | E | C,E | TRUE
Now, lets copy column C and D and paste special as values in the same place. Then add a filter on the whole table and filter out column D for the rows labeled TRUE. Now, remove the filter, delete columns B and D and row 1.
| A | B
---+-----------+-----------
1 | 1 | A,B,D
2 | 2 | C,E
Done. Get ice cream. Watch Road House.

Resources