How can I get all non-empty columns and their contents? - google-sheets

I would like to know how I can filter out empty columns when using data from one part of the sheet in another without having to specify each column name since more columns can be added.
I found this site and tried out the formula there but that seems like sometimes it will include the column (meaning it has a non-empty value) but then it does not include that value so the column looks blank but shouldn't be.
=ArrayFormula(Query(transpose(Query(TRANSPOSE({Query({'Test Data'!A1:Z1;Query({if('Test Data'!A2:Z<>"",1,0)},"Select "&JOIN(",","Sum(Col"&column('Test Data'!A1:Z1)&")"))},"Offset 1",1);'Test Data'!A2:Z}),"Select * Where Col2>0")),"Select * Offset 1",1))
I currently have this:
| | english | math | science |
|:-----------|------------:|:------------:|:-----------:|
| 8:30 | bob,jill | | |
| 9:40 | | | |
| 10:15 | | | mike |
I would like it to this (its okay for a row to be empty):
| | english | science |
|:-----------|------------:|:-----------:|
| 8:30 | bob,jill | |
| 9:40 | | |
| 10:15 | | mike |
any help would be appreciated.

the best way of doing this would be to re-pivot it again like:
=ARRAYFORMULA(QUERY(SPLIT(TRANSPOSE(SPLIT(CONCATENATE(
IF(A2:A<>"", "♠"&A2:A&"♦"&IF(B2:Z<>"", B2:Z, "♥")&"♦"&B1:Z1, )), "♠")), "♦"),
"select Col1,max(Col2) where Col2 <> '♥' group by Col1 pivot Col3"))
if you want to keep all times you will need:
=ARRAYFORMULA({QUERY(SPLIT(TRANSPOSE(SPLIT(CONCATENATE(
IF(A2:A<>"", "♠"&A2:A&"♦"&IF(B2:E<>"", B2:E, "♥")&"♦"&B1:E1, )), "♠")), "♦"),
"select Col1,max(Col2) where Col2 <> '♥' group by Col1 pivot Col3 limit 0");
{A2:A, IFERROR(VLOOKUP(A2:A, QUERY(SPLIT(TRANSPOSE(SPLIT(CONCATENATE(
IF(A2:A<>"", "♠"&A2:A&"♦"&IF(B2:E<>"", B2:E, "♥")&"♦"&B1:E1, )), "♠")), "♦"),
"select Col1,max(Col2) where Col2 <> '♥' group by Col1 pivot Col3"),
TRANSPOSE(ROW(INDIRECT("A2:A"&COLUMNS(QUERY(SPLIT(TRANSPOSE(SPLIT(CONCATENATE(
IF(A2:A<>"", "♠"&A2:A&"♦"&IF(B2:E<>"", B2:E, "♥")&"♦"&B1:E1, )), "♠")), "♦"),
"select Col1,max(Col2) where Col2 <> '♥' group by Col1 pivot Col3 limit 0"))))), 0))}})

Related

Combine duplicate rows in column as comma separated values - Google Query

If i have 2 columns viz., ID & Name, ID column containing duplicates, and if i want to group by ID to get unique ID's but name column should be a comma-separated list, can this be possible in Google Query?
| ID | Name |
===============
| 1001 | abc |
---------------
| 1001 | def |
---------------
| 1002 | kjg |
---------------
| 1003 | aof |
---------------
| 1003 | lmi |
---------------
| 1004 | xyz |
---------------
into
| ID | Name |
====================
| 1001 | abc, def |
--------------------
| 1002 | kjg |
--------------------
| 1003 | aof, lmi |
--------------------
| 1004 | xyz |
--------------------
try:
=ARRAYFORMULA({QUERY(QUERY({A2:B, B2:B},
"select Col1,max(Col2)
where Col1 is not null
group by Col1
pivot Col3"),
"select Col1
offset 1", 0), REGEXREPLACE(TRIM(
TRANSPOSE(QUERY(TRANSPOSE(QUERY(QUERY({A2:B&",", B2:B},
"select max(Col2)
where Col1 is not null
and Col2 <> ','
group by Col1
pivot Col3"),
"offset 1", 0)),,999^9))), ",$", )})
however, this may not work for massive datasets due to TRIM (which is needed to remove empty spaces) and REGEXREPLACE (which is needed to remove the end comma) limitations. otherwise, without it, the formula can handle anything:
=ARRAYFORMULA({QUERY(QUERY({A2:B, B2:B},
"select Col1,max(Col2)
where Col1 is not null
group by Col1
pivot Col3"),
"select Col1
offset 1", 0),
TRANSPOSE(QUERY(TRANSPOSE(QUERY(QUERY({A2:B&",", B2:B},
"select max(Col2)
where Col1 is not null
and Col2 <> ','
group by Col1
pivot Col3"),
"offset 1", 0)),,999^9))})
I looked through Query specification. I could not find a solution. So I made some formulas that do the job (because I found this task interesting).
D2 contains =unique(a2:a)
E2 contains =join(", ",transpose(filter($B$2:$B,$A$2:$A=D2)))and it's copied down.
I had to copy formulas down (far from beautiful formula)
Hope you find it helpful.
Reference
UNIQUE
JOIN
TRANSPOSE
FILTER
Here is an answer using QUERY.
=ARRAYFORMULA(REGEXREPLACE(TRIM(SPLIT(TRANSPOSE(SPLIT(
CONCATENATE(TRANSPOSE(QUERY({"♦"&A2:A&"♠", B2:B&", "},
"select max(Col2) where Col2 is not null group by Col2 pivot Col1", 0))),
"♦")), "♠")), ",$", ))
This comes directly from this question.
Player0 has answers with just amazing formulas that are able to reorganise data in a huge variety of ways.
if you could live with the end-comma present in the output you can try:
=ARRAYFORMULA({QUERY(QUERY({A2:B, B2:B},
"select Col1,max(Col3)
where Col1 is not null
and Col3 <> ','
group by Col1
pivot Col2"),
"select Col1 offset 1", 0),
TRANSPOSE(QUERY(TRANSPOSE(IFERROR(VLOOKUP(QUERY(QUERY({A2:B, B2:B},
"select Col1,max(Col3)
where Col1 is not null
and Col3 <> ','
group by Col1
pivot Col2"),
"select Col1 offset 1", 0),
QUERY(QUERY({A2:B, B2:B&","},
"select Col1,max(Col3)
where Col1 is not null
and Col3 <> ','
group by Col1
pivot Col2"),
"offset 1", 0),
SPLIT(TRANSPOSE(QUERY(TRANSPOSE(IF(QUERY(QUERY({A2:B, B2:B&","},
"select max(Col3)
where Col1 is not null
and Col3 <> ','
group by Col1
pivot Col2"),
"offset 1", 0)="",,COLUMN(B2:XXX)&",")),,999^99)), ","), 0))),,999^99))})
(tho this was never tested on an ultra-massive dataset but in theory, it should handle anything too)

How to transpose this data in Google Sheets

I have data in google sheet I just want to fetch that data in my other sheet but in transpose
Here is ex:
Column A | Column B | Column C
=================================
site1.com | Name 1 | Name 2
site2.com | Name 3 | Name 4
site3.com | Name 5 | Name 6
Want to data like this
Column A | Column B | Column C
=================================
site1.com | site2.com | site3.com
Name 1 | Name 3 | Name 5
Name 2 | Name 4 | Name 6
I don't want to enter formula manually in every row so is arryformula can do this automatic.
I'm trying this but not able to what I want.
=ARRAYFORMULA(TRANSPOSE(Sheet1!$B2:B300 & Sheet1!$L2:L300))
try:
=ARRAYFORMULA({TRANSPOSE(SORT(FILTER(A:A, A:A<>"")));
SUBSTITUTE(TRANSPOSE(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(
IF(ISNUMBER(QUERY(QUERY({A:B; A:A, C:C},
"select count(Col1) where Col1 is not null group by Col1 pivot Col2"),
"offset 1", 0)), SUBSTITUTE(QUERY(QUERY({A:B; A:A, C:C},
"select count(Col1) where Col1 is not null group by Col1 pivot Col2"),
"limit 0"), " ", "♦"), )),,999^99)), " ")), "♦" , " ")})
or you can do just:
=TRANSPOSE(A1:C3)
or:
=TRANSPOSE(INDIRECT("A1:C"&COUNTA(A:A)))
UPDATE:
=QUERY(TRANSPOSE(INDIRECT("Sheet1!A2:L"&COUNTA(Sheet1!A2:A)+1)), "offset 1")

Google Sheets function to group and concat rows

Sample sheet: https://docs.google.com/spreadsheets/d/1AeP0sxDi0-3aaesUdCNTKfricIimjTMFaKO-FX9_g50/edit?usp=sharing
I am trying to find a formula that will group a table on a column and concat the values from all the rows in another column.
For example, if this is my table:
| name | value |
|-------|---------|
| one | alpha |
| two | bravo |
| three | charlie |
| one | delta |
| two | echo |
| four | foxtrot |
| two | golf |
| three | hotel |
| four | india |
This is what I want the formula to output:
| one | alpha, delta |
| two | bravo, echo, golf |
| three | charlie, hotel |
| four | foxtrot, india |
I wish I could share some formula that gets me close but I can't find anything. I thought maybe this formula but, as you can see from the sample sheet, it does not work.
=ARRAYFORMULA(JOIN(", ", TRANSPOSE(FILTER(B2:B, A2:A = {UNIQUE(A2:A)}))))
My thought was, get a unique list of values in the name column, and then use arrayformula to get a list of values in the value column where the name column equals each value in the unique list. :/
try:
=ARRAYFORMULA(REGEXREPLACE(TRIM(SPLIT(TRANSPOSE(
QUERY(QUERY({A2:A&"♦", B2:B&","},
"select max(Col2)
where Col1 !=''
group by Col2
pivot Col1"),,999^99)), "♦")), ",$", ))
or:
=ARRAYFORMULA(IFNA(VLOOKUP(UNIQUE(A2:A),
REGEXREPLACE(TRIM(SPLIT(TRANSPOSE(
QUERY(QUERY({A2:A&"♦", B2:B&","},
"select max(Col2)
where Col1 !=''
group by Col2
pivot Col1"),,999^99)), "♦")), ",$", ), {1, 2}, 0)))

Counting number of columns a value appears in (Google Sheets)

I have a Google Sheet where I want to know the number of unique columns that a value appears in. For example, given the following sheet:
| A | B | C | D |
+-------+-------+-------+-------+
| Joe | Lisa | Lisa | Lisa |
| Joe | Lisa | Jenny | Lisa |
| Joe | Jenny | Jenny | John |
| Joe | Jenny | Katie | John |
| Joe | Jenny | Katie | John |
I would want something that counts Joe appearing in 1 column, Lisa appearing in 3, Jenny appearing in 2, Katie appearing in 1, and John appearing in one, i.e.
| Name | Count |
+-------+-------+
| Joe | 1 |
| Lisa | 3 |
| Jenny | 2 |
| Katie | 1 |
| John | 1 |
What's the best way to do this?
Assuming the data has no spaces in it, try:
=ArrayFormula(QUERY(SPLIT(UNIQUE(TRANSPOSE(SPLIT(JOIN(" ", QUERY(A1:D&"_"&COLUMN(A1:D1),,ROWS(A1:A)))," "))), "_"), "Select Col1, count(Col2) where Col2 > 0 group by Col1 label count(Col2)''"))
If the source data has spaces, try:
=ArrayFormula(SUBSTITUTE(QUERY(SPLIT(UNIQUE(TRANSPOSE(SPLIT(JOIN(" ", QUERY(SUBSTITUTE(A1:D, " ", "~")&"_"&COLUMN(A1:D1),,ROWS(A1:A)))," "))), "_"), "Select Col1, count(Col2) where Col2 > 0 group by Col1 label count(Col2)''"), "~", " "))
EDIT
As Tom has noticed, I've missed the task, and the correct formula is:
=QUERY(
QUERY(
{TRANSPOSE(SPLIT(TEXTJOIN("#",1,FILTER(COLUMN(A:D)*row(A:D)^0,A:A<>"")),"#")),
TRANSPOSE(SPLIT(TEXTJOIN("#",1,A:D),"#"))},
"select Col1, Col2, count(Col2) group by Col1, Col2"),
"select Col2, count(Col3) group by Col2 label Col2 'Name',count(Col3) 'Count'")
Credit: #tom-sharpe
My original formula counted max times name is in a row:
=QUERY(
QUERY(
{TRANSPOSE(SPLIT(TEXTJOIN("#",1,FILTER(COLUMN(A:D)^0*row(A:D),A:A<>"")),"#")),
TRANSPOSE(SPLIT(TEXTJOIN("#",1,A:D),"#"))},
"select Col1, Col2, count(Col2) group by Col1, Col2"),
"select Col2, max(Col3) group by Col2 label Col2 'Name'")

Select GROUP BY column without selecting the Sum() Column

Say I have searched multiple sheets with a query and gotten the following table in Google Sheets: (its nested, so it will never actually be displayed)
| Item | Amount | ID |
------------------------------
| cat | 3 | 1 |
------------------------------
| dog | 2 | 2 |
------------------------------
| dog | 4 | 2 |
------------------------------
| bird | 1 | 3 |
------------------------------
| bird | 2 | 3 |
------------------------------
| dog | 1 | 2 |
------------------------------
Obviously, If I want to get the Sum of Dogs and Birds from this table I could do something like this:
(-not exact syntax, just an example)
"SELECT Col1, sum(Col2) WHERE Col1 = 'dog' or 'bird' GROUP BY Col1 ORDER BY Sum(Col2)"
And I should get something like the following:
| Item | Amount |
---------------------
| dog | 7 |
---------------------
| bird | 3 |
---------------------
BUT - Is there a way I can return ONLY Col1 (As in still do the Grouping and ordering) so that If I was to put it side by side with a result that showed all Columns it would still line up correctly?
OK, Solved it!!! Quite Simple Really....
All I have to do is to add another layer of nesting.
SO:
=QUERY(table, "SELECT Col1, sum(Col2) WHERE Col1 = 'dog' or 'bird' GROUP BY Col1 ORDER BY Sum(Col2))"
Becomes:
=QUERY(
QUERY(table, "SELECT Col1, sum(Col2) WHERE Col1 = 'dog' or 'bird' GROUP BY Col1 ORDER BY Sum(Col2)"),
"SELECT Col1"
)
Again, Syntax not exact, but its the Logic I was after :)
:)

Resources