I am taking an advanced Google sheet course and I got the following assignment.
I have a first table in Google Sheet (let's call it T1) with two variables, company name and salesman. For each company name there may be one or several salesmen. If there is more than one salesman for each company, then the table has multiple rows. For instance, in the table below, company C1 has two salesmen, while others have one only:
Company
Salesman
C1
S1
C1
S2
C2
S1
C3
S1
This is a mapping table.
I have a second table (let's call it T2) which has only the company names (C1, C2, C3, etc.) and other metadata referred to that company (O1, O2, O3, etc.). So even if there are multiple salesmen for a company, in table T2, there is only one entry for each company and its metadata (i.e., one row), like in the below:
Company
Other
C1
O1
C2
O2
C3
O3
I want to create a third table, T3, which expands T2 to accomodate the multiple salesmen. In other words, if in T2 there is C1 which has two salesmen, i.e. S1 and S2, I want the T2 to add a second row for C1, so that both salesmen can be correctly captured:
Company
Other
Salesman
C1
O1
S1
C1
O1
S2
C2
O2
S1
C3
O3
S1
How can I do this? Any Google Sheet or VBA answer would be much appreciated. I believe in SQL this would be achieved via some join/pivoting, but I am not sure in Google Sheet.
Excel ms365:
Formula in G1:
=LET(x,A1:A5,HSTACK(x,VLOOKUP(x,D1:E4,2,0),B1:B5))
Or, in terms of tables:
=LET(x,T_1[[#All],[Company]],HSTACK(x,VLOOKUP(x,T_2[#All],2,0),T_1[[#All],[Salesman]]))
Google-Sheets:
Formula in G1:
=INDEX({VLOOKUP(A1:A5,D1:E4,{1,2},0),B1:B5})
use:
={INDEX(IFNA(VLOOKUP(A2:A, D2:E, {1, 2}, ))), B2:B}
Related
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
Bassically, Lets say we have Row A, B and C
Row A is name; Row B is a ID, Row C is another ID. I have a function where if Row B repeats twice, the cell becomes red.
Lets take some examples:
Lets say in A1 we have "Joe"
In B1 we have his ID - 1234
In C1 we have his 2nd ID - 7a1d
In A2 we have "Jack"
In B2 we have his ID - 9876
In C2 we have his 2nd ID - 7a1d
Because B1 and B2 are different, but C1 and C2 are the same, both cells are RED
Now lets say we John changed his name to Thomas, so we will have:
A1 - "Joe"
B1 - 1234
C1 - 7a1d
A2 - "Thomas"
B2 - 1234
B2 - 7a1d
Because in this case, B1 and B2 are the same, C1 and C2 will show BLUE, to indicate that he changed his name
I achieved the first part, I just need help with the 2nd one. I need to formula to be in Conditional Formatting.
Something like this?
=AND(B1=B2,C1=C2)
You can anchor columns or rows if you need to do it extensive to other rangea
I have this format:
A B C D
invoice date product amount
D1 P1 10
D1 P2 100
D1 P1 10
I1 D1 P1 10
I want to calculate the sum of non-invoiced products by product. I'm almost there with this:
=query($A$2:$D;"select C, sum(D) where A is null group by C")
sum
P1 20
P2 100
But I actually want to split this result (ignoring the "sum" header) and put it into two non-adjacent columns E and G:
E F G
row1 P1 20
ro22 P2 100
I guess I could write two separate array queries, one for E1 and another for G1, but I'm still not sure how to extract pieces of the QUERY result (and there might be a simpler approach than using QUERY).
Live example
Unfortunately, ignoring columns in a formula is not possible. You need to create 2 QUERY functions.
Try these formulas:
For cell E8: =QUERY(QUERY(A2:D5, "SELECT C, SUM(D) where A is null group by C label SUM(D) ''"), "SELECT Col1")
For cell G8: =QUERY(QUERY(A2:D5, "SELECT C, SUM(D) where A is null group by C label SUM(D) ''"), "SELECT Col2")
Example:
Reference:
QUERY
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.
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'")