Google sheets: append data from 2 tabs, with different column order - google-sheets

I have 2 tabs named S1 and S2. They both contain 3 columns of data (A, B and C). I just want to merge their content in a 3rd tab, using functions. Issue is that the order of columns is different in S1 and S2.
S1
S2
S1
| Column A | Column B | Column C |
| -------- | -------- | -------- |
| 1 | A | DeptA |
| 2 | B | DeptB |
| 3 | C | DeptC |
S2
| Column A | Column B | Column C |
| -------- | -------- | -------- |
| 4 | DeptD | D |
| 5 | DeptE | E |
| 6 | DeptF | F |
What I want to get in a 3rd tab is:
| Column A | Column B | Column C |
| -------- | -------- | -------- |
| 1 | A | DeptA |
| 2 | B | DeptB |
| 3 | C | DeptC |
| 4 | D | DeptD |
| 5 | E | DeptE |
| 6 | F | DeptF |
I'm using the following formula: "query({'S1'!A1:C;'S2'!A2:A,'S2'!C2:C,'S2'!B2:B};"Select Col1, Col2,Col3 where Col1 is not null";1)". But I get a formula analysis error.
I have also tried "
={
query({S1!A1:C},"Select Col1,Col2,Col3");
query({S2!A2:C},"Select Col1,Col3,Col2")
}
"
But I also get a formula analysis error
Spreadsheet access: https://docs.google.com/spreadsheets/d/1FdaRSANfcqMkSBE-is8ek8bFWp80vVW8P3a-v0q4MwU/edit?usp=share_link
Thanks for your help

Depending on locale setting its either:
=query({{'S1'!A1:C};{'S2'!A2:A\'S2'!C2:C\'S2'!B2:B}};"Select Col1, Col2, Col3 where Col1 is not null")
OR
=query({{'S1'!A1:C};{'S2'!A2:A,'S2'!C2:C,'S2'!B2:B}},"Select Col1, Col2, Col3 where Col1 is not null")

The issue is in your Locale Settings. You should use semi-colons instead of commas:
={
query({'S1'!A1:C};"Select Col1,Col2,Col3");
query({'S2'!A2:C};"Select Col1,Col3,Col2")
}

Related

Find multiple matches in a dataset, and return all matches in a single row

What array formula would work for this?
Test Sheet: Open
Current Data Structure
Contains a running list of names and when they started, ended training.
| A | B | C |
| John | StartDate1 | EndDate1 |
| Adam | StartDate3 | EndDate3 |
| John | StartDate2 | EndDate2 |
| Ted | StartDate5 | EndDate5 |
| Adam | StartDate4 | EndDate4 |
Expected Results
Unique column of names in column E =UNIQUE(A2:A)
Next to the unique name, display every StartDate & EndDate that matches the unique name.
| E | F | G | H | I |
| John | StartDate1 | EndDate1 | StartDate2 | EndDate2 |
| Adam | StartDate3 | EndDate3 | StartDate4 | EndDate4 |
| Ted | StartDate4 | EndDate4 | | |
What I have tried
=FILTER(B2:C,A2:A = E2)
Does not return on a single row. ❌
Does not work with ARRAYFORMULA. ❌
=TRANSPOSE(FILTER(B2:C,A2:A = E2:E))
Returns all StartDates on a single row, and all End Dates on the next row. ❌
It should return on a single row (StartDate,EndDate,StartDate,EndDate, etc)
Does not work with ARRAYFORMULA. ❌
=ARRAYFORMULA(VLOOKUP(E2:E,A2:C,{2,3}))
Returns the first match only ❌
Works with array formula. ✔️
What am I doing wrong? Is there a better arrayformula that can display every start and end date that matches a unique name in a row?
Thanks for your help!
use:
=INDEX(SPLIT(FLATTEN(QUERY(QUERY(IF(A3:A="",,{A3:A, "×"&B3:B&"×"&C3:C}),
"select max(Col2) where Col2 is not null group by Col2 pivot Col1"),,9^9)), "×"))

Count occurrence by weekday in columns and row

I am having a google spreadsheet where the columns are specific dates and the rows are products.
| Weekdays | | | 2 | 3 | 4 | 5 | 6 |
| Activity | Streak | Max | 06.11.18 | 07.11.18 | 08.11.18 | 09.11.18 | 10.11.18 |
|-----------|--------|-----|----------|----------|----------|----------|----------|
| Product 1 | 0 | 5 | x | x | x | x | ... |
| Product 2 | 0 | 6 | x | | x | x | ... |
| Product 3 | 0 | 11 | x | x | x | x | ... |
| Product 4 | 0 | 7 | | | | | ... |
See my Data sheet.
On the tab Evaluation I tried to generate the following matrix, which should answer the question on which day of the week an occurrence of each of the products happened.
| Weekdays vs. Products | Product 1 | Product 2 | Product 3 | Product 4 |
|-----------------------|-----------|-----------|-----------|-----------|
| 1 | #N/A | #N/A | #N/A | #N/A |
| 2 | #N/A | #N/A | #N/A | #N/A |
| 3 | #N/A | #N/A | #N/A | #N/A |
| 4 | #N/A | #N/A | #N/A | #N/A |
| 5 | #N/A | #N/A | #N/A | #N/A |
| 6 | #N/A | #N/A | #N/A | #N/A |
| 7 | #N/A | #N/A | #N/A | #N/A |
I tried simply a =COUNTIF(Data!$2:$2;$B3;C$2;Data!A3), however as you can see above I get #N/A.
Any suggestions how to count for each weekday the occurrence of the product?
Appreciate your reply!
PS.: Find my example google spreadsheet here: google spreadsheet
Try
=countif(filter(filter(Data!$D$3:$BA;Data!$A$3:$A=C$13); Data!$D$1:$BA$1=$B14); "x")
and fill down and to the right as needed.
In Tabellenblatt3, I also added an alternitve approach, using a single formula. In cell B3 you'll find
=ArrayFormula(substitute(query(split(transpose(split(textjoin(;1;query(if(Data!D3:BA10="x";substitute(Data!A3:A10; " ";"/")&"_"&Data!D1:BA1&"_"&Data!D3:BA10;);;rows(Data!A3:A10)));" ")); "_"); "Select Col2, count(Col3) group by Col2 pivot Col1 label Col2 'Weekday'"); "/"; " "))
Note that this formula can break (because of the character limit in textjoin()) for large data sets.

Query a count of unique attributes in a table

I have a table like this:
+--------+-------+--------+-------+
| attr1 | attr2 | attr3 | attr4 |
+--------+-------+--------+-------+
| purple | wine | clear | 10.0 |
| red | wine | solid | 20.0 |
| red | beer | cloudy | 10.0 |
| purple | ale | clear | 34.0 |
| blue | ale | solid | 16.0 |
+--------+-------+--------+-------+
that i want to transform like this:
+--------+-------+-------+-------+-------+
| | attr1 | attr2 | attr3 | attr4 |
+--------+-------+-------+-------+-------+
| purple | 2 | | | |
| red | 2 | | | |
| blue | 1 | | | |
| wine | | 2 | | |
| beer | | 1 | | |
| ale | | 2 | | |
| clear | | | 2 | |
| solid | | | 2 | |
| cloudy | | | 1 | |
| 10.0 | | | | 2 |
| 20.0 | | | | 1 |
| 34.0 | | | | 1 |
| 16.0 | | | | 1 |
+--------+-------+-------+-------+-------+
This pivoted or cross-table will show me the count of each attribute value in their respective columns.
How do i use the Google Query language to display such a cross-table?
Well if the data were laid out in two columns it would be straightforward e.g. for something like this
Attrib Column
Red 1
Red 1
Green 1
Blue 1
Beer 2
Ale 2
Ale 2
you could use a query like
=query(A:B,"select A,count(A) where A<>'' group by A pivot B")
So the problem is to organise OP#s data into two columns.
This can be done by what is by now a fairly standard split/join/transpose technique
=ArrayFormula(split(transpose(split(textjoin("|",true,if(A2:D="","",A2:D&" "&column(A2:D))),"|"))," "))
Giving
You could either run the query on the result of this or combine the two like this
=ArrayFormula(query({"Attrib","Number";split(transpose(split(textjoin("|",true,if(A2:D="","",column(A2:D)&"-"&A2:D&" "&column(A2:D))),"|"))," ")},"Select Col1,count(Col1) group by Col1 pivot Col2"))
I have joined the column number to the attribute e.g. 1-blue so that it sorts into the right order. If you don't like it, you could get rid of it using regexreplace.
Edit
Slightly shorter formula - I didn't need to put the headers in separately:
=ArrayFormula(query(split(transpose(split(textjoin("|",true,if(A2:D="","",column(A2:D)&"-"&A2:D&" Attr"&column(A2:D))),"|"))," "),
"Select Col1,count(Col1) group by Col1 pivot Col2",0))
Edit 2
I was being a bit thick there, should have used first row of OP's data as attribute labels instead of column numbers
=ArrayFormula(query(split(transpose(split(textjoin("|",true,if(A2:D="","",column(A2:D)&"-"&A2:D&" "&A1:D1)),"|"))," "),
"Select Col1,count(Col1) group by Col1 pivot Col2",0))
Edit 3
Should have chosen a better pair of delimiters
=ArrayFormula(query(split(transpose(split(textjoin("😊",true,if(A2:D="","",column(A2:D)&"-"&A2:D&"🍺"&A1:D1)),"😊")),"🍺"),
"Select Col1,count(Col1) group by Col1 pivot Col2",0))

List of the most frequently occurring words in the row

I'm looking for a way to show (in the Google Spreadsheet) the most frequently occurring word in the row, but if it isn't one word I want to display all of them separated by semicolon which have the same count of occurrence.
Explanation:
For example, I want to fill the last column with values as below:
+---+------+------+------+------+------+-------------------+
| | A | B | C | D | E | F |
+---+------+------+------+------+------+-------------------+
| 1 | Col1 | Col2 | Col3 | Col4 | Col5 | Expected response |
| 2 | A | A | C | D | E | A |
| 3 | A | A | B | B | B | B |
| 4 | A | A | B | B | E | A, B |
| 5 | A | B | C | D | E | A, B, C, D, E |
+---+------+------+------+------+------+-------------------+
Here's what I have achieved (formula for cell F2):
=INDEX(A2:E2; MODE(MATCH(A2:E2; A2:E2; 0)))
but it doesn't work for 4th and 5th row as I expect.
This works in Office 365 Excel, but probably will not in Excel online, as it is an array formula.
=TEXTJOIN(", ",TRUE,INDEX(A2:E2,,N(IF({1},MODE.MULT(IF(((MATCH(A2:E2,A2:E2,0)=COLUMN(A2:E2))*(COUNTIF(A2:E2,A2:E2)=MAX(COUNTIF(A2:E2,A2:E2)))),COLUMN(A2:E2)*{1;1}))))))
Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} Around the formula.
EDIT:
To do it with Google Sheets as you now want:
=join(", ",filter(A2:E2,column(A2:E2)=match(A2:E2,A2:E2,0),countif(A2:E2,A2:E2)=max(countif(A2:E2,A2:E2))))
F2:
=JOIN(",",SORTN(TRANSPOSE(A2:E2),1,1,ARRAY_CONSTRAIN(FREQUENCY(MATCH(A2:E2,A2:E2,0),COLUMN(A2:E2)),COUNTA(A2:E2),1),0))
See syntax # https://support.google.com/docs/table/25273

Count occurrences of words from multiple columns

I have a spreadsheet like this, where the values A-E are the same options coming from a form:
+------+------+------+
| Opt1 | Opt2 | Opt3 |
+------+------+------+
| A | A | B |
| B | C | A |
| C | C | B |
| A | E | C |
| D | B | E |
| B | E | D |
+------+------+------+
I want to make a ranking, showing the most chosen options for each option. I already have this, where Rank is the ranking of the option and number is the count of the option:
+------+------+------+
| Rank | Opt1 | Numb |
+------+------+------+
| 1 | A | 2 |
| 1 | B | 2 |
| 3 | C | 1 |
| 3 | D | 1 |
+------+------+------+ (I have 3 of these, one for each option)
I want to do now a summary of the 3 options, making the same ranking but joining the options. It would be something like:
+------+------+------+
| Rank |Opt123| Numb |
+------+------+------+
| 1 | B | 5 |
| 2 | A | 4 |
| 2 | C | 4 |
| 4 | E | 3 |
| 5 | D | 2 |
+------+------+------+
The easiest way to do this would be getting the data from the three ranking tables or from the original three data columns?
And how would I do this?
I already have the formula to get the names of the options, the count and ranking, but I don't know how to make them work with multiple columns.
What I have (the F column is one of the data columns):
Column B on another sheet:
=SORT(UNIQUE(FILTER('Form Responses'!F2:F;NOT(ISBLANK('Form Responses'!F2:F)))); RANK(COUNTIF('Form Responses'!F2:F; UNIQUE(FILTER('Form Responses'!F2:F;NOT(ISBLANK('Form Responses'!F2:F))))); COUNTIF('Form Responses'!F2:F; UNIQUE(FILTER('Form Responses'!F2:F;NOT(ISBLANK('Form Responses'!F2:F))))); TRUE); FALSE)
Column C:
=ArrayFormula(COUNTIF('Form Responses'!F2:F; FILTER(B2:B;NOT(ISBLANK(B2:B)))))
Column A:
=ARRAYFORMULA(SORT(RANK(FILTER(C2:C;NOT(ISBLANK(C2:C))); FILTER(C2:C;NOT(ISBLANK(C2:C))))))
Edited:
Merge cols:
=TRANSPOSE(split(join(",",D2:D,E2:E),","))
merges 2 cols, not very clean, but works. (Same as here Stacking multiple columns on to one?)
Full formula:
=SORT(UNIQUE(FILTER(TRANSPOSE(split(join(",",D2:D,E2:E),","));NOT(ISBLANK(TRANSPOSE(split(join(",",D2:D,E2:E),",")))))); RANK(COUNTIF(TRANSPOSE(split(join(",",D2:D,E2:E),",")); UNIQUE(FILTER(TRANSPOSE(split(join(",",D2:D,E2:E),","));NOT(ISBLANK(TRANSPOSE(split(join(",",D2:D,E2:E),","))))))); COUNTIF(TRANSPOSE(split(join(",",D2:D,E2:E),",")); UNIQUE(FILTER(TRANSPOSE(split(join(",",D2:D,E2:E),","));NOT(ISBLANK(TRANSPOSE(split(join(",",D2:D,E2:E),","))))))); TRUE); FALSE)
The transpose could be done after the sort.

Resources