Improve Query Function in Google Sheets - google-sheets

I've been using the following function:
=query(Sheet1!A2:D," select A, B, C where A matches '"&JOIN("|", A2:A)&"' and D matches 'yes'")
Is there anyway that I can make is so that every row that starts with a match will be added a comma separated list in which each column occupies one cell with no duplicates as shown in sheet3.
https://docs.google.com/spreadsheets/d/1YDxIUnZzzYde9hcexPoDegv4HBuiUwk2wLKSXazu9hE/edit?usp=sharing
Sheet 2 has the function that I used and the result.

It is not entirely clear what you want to do, but try this. In Sheet1 in E2 put this combining Col A and D:
=arrayFormula(A2:A & if(isBlank(D2:D),""," ") & D2:D)
In F2 combine Col C and D with this:
=arrayFormula(B2:B & if(isBlank(C2:C),"",",") & C2:C)
In G2 find the unique values from Col F with this:
=UNIQUE(E2:E)
In H2 put this and drag the formula down:
=join(",",query(E2:F,"select F where E contains '"& G2 &"'"))
Hide Cols E & F

Related

Google sheets, flatten but with an added line

I have the following data in A1 to C5:
A B
C D E
F G
H
I J
I want to flatten this data so that each row is followed by a space, while removing the blank cells like this:
A
B
C
D
E
F
G
H
I
J
But when I use FLATTEN
I get this result:
A
B
C
D
E
F
G
H
I
J
it just treats the blanks in each row as data and slams it together.
I've tried to split and join unsuccessfully using a few variations on:
=BYROW(A1:C5, LAMBDA(row,map(row,LAMBDA(x,if(x="",,x&",")))))
You can try with:
=FLATTEN(SPLIT (JOIN (",",BYROW(A1:C5,LAMBDA(row,TEXTJOIN(",",1,row)&","))),",",1,0))
Joining each row with commas, and an additional comma so it adds the extra cell when splitting

Google Sheets: Creating a formatted list based off Form Results

I am working on a voting form to rank some things (A, B, C, D, F) using Google Forms. I have it set up to import the results into Google Sheets. What I am trying to do is to then take the results and sort them in another sheet by their rankings.
So the result of the form looks something like this:
Item A
Item B
Item C
Item D
Item E
Item F
Item G
A
B
F
C
F
A
F
B
C
F
C
D
A
F
B
A
D
C
F
A
F
B
B
D
C
D
B
F
I then have a formula that finds the majority ranking for each item and places them into another sheet, which looks like this:
Item A
Item B
Item C
Item D
Item E
Item F
Item G
B
B
D
C
D
A
F
My goal is to have the final sheet transpose that table and sort them alphabetically by their voted upon ranking. So using the dataset above, it would look like this:
Rank A
Rank B
Rank C
Rank D
Rank F
Item F
Item A
Item D
Item C
Item G
Item B
Item E
Is this something that is possible to automate in Google Sheets? Currently I am doing the last part manually, but I would love to have it auto-add each ranked item to the correct column and if possible, sort them alphabetically in their respective ranks.
try:
=ARRAYFORMULA(SUBSTITUTE(TRANSPOSE(SPLIT(FLATTEN(QUERY(
QUERY(TEXT(TRANSPOSE(SUBSTITUTE(A1:G2, " ", "×")), {"#", "Rank×#"}),
"select max(Col1) group by Col1 pivot Col2"),,9^9)), " ")), "×", " "))
I have created this example, you will need to change the ranges in the formula to match with your data.
=TRANSPOSE(INDEX(FILTER(A1:G1, A2:G2 = "A")))
Add the above formula below each “Rank” column and change the letter “A” to the respective ranking.

Repeating a list N times Google Sheets

I think it's best I provide an example for this one.
Input:
a 2
b 3
c 4
d 1
Output Goal:
a
a
b
b
b
c
c
c
c
d
So I want to repeat the value in Cell A1 n times (n is located in column B1)
I considered using the following function then transposing and stacking the array:
=SPLIT(REPT(A1&",",B1),",")
However, I was hoping there is a single formula that can solve this.
Try the following formula:
=transpose(split(concatenate(arrayformula(rept(A1:A4&",",B1:B4))),","))
=TRANSPOSE(SPLIT(JOIN(",", ARRAYFORMULA(REPT(SPLIT(
INDIRECT("A1:A"&COUNTA(A1:A)), ",")&",",
INDIRECT("B1:B"&COUNTA(B1:B))))), ","))

Pivot tables with actual entries

I have a form that populates a spreadsheet with data pairs in the form of Manufacturer-Category, like this:
Mfr Category
A Servers
A Workstations
B Components
C Workstations
D Networking
E Workstations
F Other
G Components
I would like to build a pivot table-like listing with Category entries as the column headings but with a list of Manufacturers under each:
Servers Workstations Components Networking Other
A A B D F
C G
E
Categories may be added down the road, so I would like them to be detected automatically like in a pivot table.
Try this:
=ARRAYFORMULA(TRANSPOSE(SPLIT(TRANSPOSE(QUERY(QUERY(A1:B8," select max(A) group by A pivot B"),,5000))," ")))
You cannot order the titles though.
Since my data contains spaces, using anonymous's answer as a basis, I came up with the following solution:
Use an intermediate sheet Sheet2 for referencing if spaces exist in the data. Cell A1 contains the formula
=ARRAYFORMULA(SUBSTITUTE('Sheet1'!A:B," ","_"))
On a third sheet Sheet3, enter the following in cell A1:
=ARRAYFORMULA(SUBSTITUTE(TRANSPOSE(SPLIT(TRANSPOSE(QUERY(QUERY(Sheet2!$A$1:$B$1000,"select max(A) group by A pivot B",1),,5000))," ")),"_"," "))
Explaining what each formula does in sequence:
QUERY(Sheet2!$A$1:$B$1000,"select max(A) group by A pivot B",1) does a Google Visualization API lookup to produce the following result:
Components Networking Other Servers Workstations
A A
B
C
D
E
F
G
It's starting to look like a pivot table, but there are unnecessary gaps between elements of each column.
QUERY( ... ,,5000) is a straight up hack. It takes the previous query result and concatenates all the columns and their entries into a single row of cells. A pipe has been added below to denote a new cell in the row. Note the spaces between entries.
Components B G |Networking D |Other F |Servers A |Workstations A C E
TRANSPOSE( ... ) takes this row result and turns it into a column.
Components B G
Networking D
Other F
Servers A
Workstations A C E
SPLIT( ... ," ") separates text into neat columns. Multiple space characters are ignored. Note: this and following functions will not display without being surrounded by ARRAYFORMULA().
Components B G
Networking D
Other F
Servers A
Workstations A C E
TRANSPOSE( ... ) puts everything back into columns:
Components Networking Other Servers Workstations
B D F A A
G C
E
Finally, we strip out the underscores and replace with spaces using SUBSTITUTE( ... ,"_"," ") and surround everything with ARRAYFORMULA() so it will display.

Spreadsheet Sorting Function

I want to only grab certain values from a column and put them in another column. Here is a graphic of what we would want the output to be. I am using Google Sheets.
Example of what I am trying to do:
Help of any kind would be appreciated
This is achieved with the filter formula. If in D2 you have
=filter(B2:B, A2:A = "A")
that column will contain the entries from column B where the corresponding column A entry is "A". Similarly for others.
Use query. In D2, E2, F2 put:
=query(A2:B,"select B where B contains 'A'")
=query(A2:B,"select B where B contains 'B'")
=query(A2:B,"select B where B contains 'C'")

Resources