Pivot tables with actual entries - google-sheets

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.

Related

How to prevent duplicates between two different sheets on google sheets?

I have 2 sheets, one is sheet1, other is sheet2.
Data in sheet1
Name
A
B
C
D
E
L
M
N
O
P
Data in sheet2
Name
F
G
D
H
G
Here in sheet2, two data is duplicated. One is "D" compared to sheet1, other is "G" in same sheet2.
I want to prevent duplication in both these cases so that whenever I enter new value in sheet 2, it must check both the sheets and reject duplicate entries.
I am using Google Sheet, and using data validation with a custom formula =COUNTIF(sheet1!A2:A&sheet2!A2:A,Sheet2!A2)<2
But, it's not working. Please help me.
Try with:
=COUNTIF({sheet1!$A2:$A;sheet2!$A$2:$A},Sheet2!A2)>1
With curly brackets you can join the ranges

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.

Improve Query Function in 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

Trying to include additional columns in QUERY result on GoogleSheets

I'm using Google Sheets and using the following formula without any issue:
=QUERY(Sheet!A:D,"select A, max(B) where not B is null group by A label A 'Client', max(B) 'Most Recent'")
Obviously, this is returning columns A and B of the referenced data
However, I would like to also return the corresponding cells from column C.
sample data: https://docs.google.com/spreadsheets/d/1CJKiDNPUWMMUbLLb-UDA6iRA2PBnlMHDsEB9vELe0Rk/edit?usp=sharing
Using this example, what I would like to see is cell G2 populated with "Pizza" (i.e., from C3), and so on.
Am I using the right formula? What do I need to change?
What you are trying to do is not very SQL-like, because max(B) does not point to any particular row: it's just the maximum value of B among the selected rows. This value could be attained by several rows, making the choice of C, D ambiguous. I don't think that a single query can do this.
Here is an implementation with unique and several query commands.
In E2, enter =unique(A:A) to get the list of unique client names.
In F2, enter
=query(A:D, "select B,C,D where A ='"&E2&"' order by B desc limit 1")
and drag this formula down. This selects all rows with the A value matching E2, and picks one with maximal B value.
You don't want to have a header row in the output of these queries so either add label B '', C '', D '' or just don't include the header row in the queried range.
Version with grouping by C,D
To also select C and D within a single query, expand the select clause
select A, max(B), C, D
which will then require including those in the group by clause:
group by A, C, D
The formula will be
=QUERY(A:D,"select A, max(B), C, D where not B is null group by A, C, D label A 'Client', max(B) 'Most Recent'")
This does mean that the only rows to be grouped together will be those where not only A but also C and D are equal.
A possible one-formula solution:
=ArrayFormula(IFERROR(VLOOKUP(UNIQUE(A2:A),SORT(A2:C,2,0),{1,2,3},0)))
Here is what I did that finally worked.
In E1, enter =unique(A:A) to get the list of unique client names. In F2, enter
=query(A$2:D101, "select B,C,D where A ='"&F2&"' order by B desc limit 1")
and drag this formula down. This selects all rows with the A value matching E2, and picks one with maximal B value.
You don't want to have a header row in the output of these queries so just add desired text in header row.

Resources