I am trying to have a master file that contains data from 3 separate sheets. Each sheets has the same first 5 columns in order but 3 other columns are in different order in the 3 sheets. How can I combine the data when columns are located differently into one master file?
you can rearrange columns in {} array:
=QUERY({
{QA_Assignments_2021!A2:H};
{QA_Group_2021!A2:F, QA_Group_2021!H2:H, QA_Group_2021!G2:G};
{QA_LOMFCU_2021!A2:H}},
"where Col1 is not null")
or you can do it with multiple queries like:
=QUERY(
{QUERY(QA_Assignments_2021!A2:H, "select A,B,C,D,E,G,H,F");
QUERY(QA_Group_2021!A2:H, "select *");
QUERY(QA_LOMFCU_2021!A2:H, "select A,B,C,H,G,E,F,D")},
"where Col1 is not null")
player0 already provided a great idea of how you will solve your issue. If you are doing it right, it should look like the formula below:
=Query({
Query(QA_Assignments_2021!A2:AB, "select A,B,C,D,E,F,I,N,H");
Query(QA_Group_2021!A2:AB, "select A,B,C,D,E,F,T,L,R");
Query(QA_LOMFCU_2021!A2:AB, "select A,B,C,D,E,F,Y,H,S")
}, "where Col1 is not null")
Output:
Note:
The three A2:AB can be simplified into A2:N, A2:T and A2:Y respectively since you don't need the columns beyond that. But either of the two should work. Adjust the ranges and columns to be queried accordingly if needed.
Related
I'm looking for an efficient way to gather and aggregate some date in Google Sheets. I've been looking at the query function, pivot tables, and Index + Match formulas, but so far I've not found a way that brings me to the result I'm looking for. I have a set of data which looks more or less as follows.
The fields with an X represent irrelevant data which I don't want to show up in my end result. They only serve to illustrate that there are columns of data that I don't want in between the columns of data that I do want. The data in those columns is of varying types and of varying values per type, they are not actually fields with an "X" in it. Only the fields with numbers are of interest along with the related names at the top and left of those. The intent is to create a list that looks more or less like this.
I've highlighted those yellow fields because that data has been aggregated. For example, in the original file field D3 shows a relation between Laura and Pete with the number 1, and field L3 also shows a relation between Laura and Pete, so the number in that field is to be added to the number in the other field resulting in an aggregated total of 2 for that particular combination.
I would really appreciate any suggestions that can help me get to an elegant and efficient solution for this. The only solutions I can come up with would involve multiple "in-between" sheets and there just has to be a better way.
UPDATE:
Solved by applying the solution in player0's answer. I just had to switch around the order of Col1 and Col2 in the formula to get the table sorted the way I needed it. Formula looks like below now. Many thanks to both player0 and Erik Tyler for their efforts.
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"select Col2,Col1,sum(Col3)
where Col2 is not null
and Col3 is not null
group by Col2,Col1
label sum(Col3)''", ))
try:
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"where Col3 is not null and Col2 is not null", ))
update:
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"select Col1,Col2,sum(Col3)
where Col3 is not null
and Col2 is not null
group by Col1,Col2
label sum(Col3)''", ))
Given your current data set (which only appears to extend to Col N), place the following somewhere to the right of Col N:
=ArrayFormula(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(SPLIT(QUERY(FLATTEN(FILTER(IF(NOT(ISNUMBER(D2:N)),,D1:N1&"~ "&A2:A&"|"&D2:N),A2:A<>"")),"Select * WHERE Col1 Is Not Null"),"|"),"Select Col1, SUM(Col2) GROUP BY Col1 LABEL SUM(Col2) ''")&"~ "),,2)),"~ ",0,1))
It would be better if this were placed in a different sheet from the original data. Supposing that your original data sheet is named Sheet1, place the following version of the above formula into a new sheet:
=ArrayFormula(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(SPLIT(QUERY(FLATTEN(FILTER(IF(NOT(ISNUMBER(INDIRECT("Sheet1!D2:"&ROWS(Sheet1!A:A)))),,Sheet1!D1:1&"~ "&Sheet1!A2:A&"|"&INDIRECT("Sheet1!D2:"&ROWS(Sheet1!A2:A))),Sheet1!A2:A<>"")),"Select * WHERE Col1 Is Not Null"),"|"),"Select Col1, SUM(Col2) GROUP BY Col1 LABEL SUM(Col2) ''")&"~ "),,2)),"~ ",0,1))
This separate-sheet approach and formula allows for the original data to extend indefinitely past Col N.
I would like to create a table in google sheets, listing some elements (in column) and at the same time counting other elements, currently grouped in a string separated by semicolons.
The example is the following:
initial dataset
desired outcome
I have tried some queries but for the moment I have been able only to count the semicolon-separated elements, without being able to group them for "markets".
Any help is greatly appreciated!
Thank you in advance!
try:
=INDEX(QUERY(IFERROR(SPLIT(FLATTEN(source!A2:A&"×"&SPLIT(source!B2:B, ";")), "×")),
"select Col1,count(Col1) where Col2 is not null group by Col1 pivot Col2"))
Hi everyone,
I have 3 tables with different number of columns where some columns are actually repeated in these 3 tables (as shown in the screenshot above).
Table 1: Col A to Col D
Table 2: Col F to Col H
Table 3: Col J to Col L
I want to merge these 3 tables together to reduce the number of columns in my google sheet. The screenshot below is how I process these 3 tables before merging.
I'm using QUERY to create extra columns for each table so that the number of columns and the position are aligned between 3 tables. After that, I use QUERY again to append the 3 processed tables as shown in the screenshot below:
However, this method is very tedious when I have 10 tables or more. Is there any other easier ways or tricks to use so that I can achieve the same expected output as shown in the 3rd screenshot?
This is my sheet:
https://docs.google.com/spreadsheets/d/1H1gJAhp1RVax2fy8D-uEtFxdjb-zAHutkPFv5WZT_TY/edit#gid=0
Any help will be greatly appreciated!
You would need a really complicated formula to get the desired output which is a combination of multiple formula's
I added a new tab in you Google Sheet called "Solution" with this formula included
=QUERY(ARRAYFORMULA({
IFERROR( ArrayFormula(SPLIT(FLATTEN(Sheet1!B3:D3&"~"&Sheet1!A4:A&"~"&Sheet1!B4:D),"~")),"");
IFERROR( ArrayFormula(SPLIT(FLATTEN(Sheet1!G3:H3&"~"&Sheet1!F4:F&"~"&Sheet1!G4:H),"~")),"");
IFERROR( ArrayFormula(SPLIT(FLATTEN(Sheet1!K3:L3&"~"&Sheet1!J4:J&"~"&Sheet1!K4:L),"~")),"")}),
"SELECT Col2, SUM(Col3) WHERE Col2 is not null GROUP BY Col2 PIVOT Col1 LABEL Col2 'Student Name' ")
Steps:
Unpivot each table =IFERROR( ArrayFormula(SPLIT(FLATTEN(Sheet1!B3:D3&"~"&Sheet1!A4:A6&"~"&Sheet1!B4:D6),"~")),"")
Combine all tables into one table: =ARRAYFORMULA({__Unpivot1__; __Unpivot2__, __Unpivot3__})
Pivot above data in Step2: =Query(__Step2__, "SELECT Col2, SUM(Col3) GROUP BY Col2 PIVOT Col1 LABEL Col2 'Student Name' ")
Add WHERE Col2 is not null in the query to remove any possible blank rows
I've been trying to sum the same condition in different columns (and different sheets) to get a total.
For example, I have a list of items in 3 different columns with 3 different sums.
See here:
I solved this with an SQL query within Google sheets but I'm looking for a more simple/ elegant solution.
This is what I have:
=UNIQUE({QUERY(A2:I8; "SELECT SUM(C) where B LIKE 'bananas'");
QUERY(A2:I8; "SELECT SUM(F) where E LIKE 'bananas'");
QUERY(A2:I8; "SELECT SUM(I) where H LIKE 'bananas'")})
And the result looks like this:
Does someone know a shorter version that fits in one cell?
=SUM(QUERY({A:C;D:F;G:I}; "where Col2 = 'bananas'"; 0))
=QUERY({A:C;D:F;G:I}; "select sum(Col3) where Col2 contains 'banana' label sum(Col3)''")
=SUM(FILTER({C:C;F:F;I:I}; {B:B;E:E;H:H}="bananas"))
As a game designer, I am working on Google Sheets to make collections of animals with specific outfits. I need to make sure I use all combinations are evenly used.
I could just use for-loop, but scripting here would add unnecessary complexity for the whole process and it would be nice to just do it in the Sheets like where I make the data needed for our game. I tried different combinations of MATCH, INDEX and COUNTIFS-functions. Dabbled a bit with Pivot Tables and QUERY, but my brain is stuck and I could use some help.
My data looks something like this:
Collection1:[Animal1][Outfit1][Animal1][Outfit7][...]
Collection2:[Animal6][Outfit3][Animal2][Outfit18][...]
[...]
So rows for collections with animal FOLLOWING the outfit it has in the next cell.
I'd like to have a sheet with Rows of animals with Columns for Outfits and Counts for those Outfits for that Animal.
I can't brain this and any hints would help. Sheets can be so unintuitive for me.
cell J1:
=QUERY({B:C;D:E;F:G},
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1) 'animal count'")
cell J6:
=QUERY({B:C;D:E;F:G},
"select Col2,count(Col2)
where Col1 is not null
group by Col2
label count(Col2) 'outfit count'")
cell J12:
=QUERY({B:C;D:E;F:G},
"select Col1,count(Col1)
where Col1 is not null
group by Col1
pivot Col2")