Google sheets - QUERY, IMPORTRANGE and append text to results - google-sheets

I have a google sheet (sheetA) that contains master data. I am importing this data into another google sheet (Sheet B) using "IMPORTRANGE" function along with "QUERY"
=SORT(QUERY(IMPORTRANGE("url for sheet A","Crown DB!A2:E"),"SELECT Col1 WHERE not(Col5='SS')"))
Suppose, following is an example output I get after runnning the above formula
item1
item2
item3
item4
I want to append text to these returned values so that I can obtain two new values for each returned value:
item1 - var1
item1 - var2
item2 - var1
item2 - var2
item3 - var1
item3 - var2
item4 - var1
item4 - var2
If it were a single variant, I can just append "- Var1" to the above formula:
SORT(QUERY(IMPORTRANGE("url for sheetA","Crown DB!A2:E"),"SELECT Col1 WHERE not(Col5='SS')")) & "- Var1"
How can I modify the formula to get append multiple variants (>=2) for each item returned using IMPORTRANGE? The number of variants for each item is the same.

Simplest approach is by appending the values inside an array formula and flatten it. Then use SORT afterwards.
Formula:
=ARRAYFORMULA(SORT(FLATTEN(
QUERY({A2:E},"SELECT Col1 WHERE not(Col5='SS') and not(Col1='')")
& {" - Val1", " - Val2"})))
Output (2 variants):
Output (3 variants):
Note:
Used {A2:E} to show the full formula easier in testing. Change {A2:E} into the IMPORTRANGE in your case.
not(Col1='') is important to skip rows with blank Col1's.
Appending an n-array with an m-array in the ARRAYFORMULA will result into n x m dimension of array. Using FLATTEN on that will combine them all in one column. Then we use SORT afterwards.
Final formula should be:
=ARRAYFORMULA(SORT(FLATTEN(QUERY(
IMPORTRANGE("url for sheetA","Crown DB!A2:E"),
"SELECT Col1 WHERE not(Col5='SS') and not(Col1='')"
) & {" - Val1", " - Val2"})))

Related

Data cleaning and splitting text manually into columns

Using google sheets how can i extract a columns from this data
"aboutYouAnswers" is the first category
there are a set of questions in this category people are answering to from a website, so all the numbers are question numbers ("0"."1" etc)
All the text within the quotes are the answers i would like to convert into individual columns to compare all answers together.
{"aboutYouAnswers":{"0":"eating pizza","1":"lowering the communities IQ","2":"Avax Apes + Origins","3":"ate 100 pizzas in an hour"}}
image of test data and how it is formatted
Option 1
=ArrayFormula(IF(G3:G="",,
BYROW(G3:G, LAMBDA(rg,
LAMBDA(l, TEXTJOIN(CHAR(10), 1, {
CONCAT(FILTER(l, ISODD(MATCH(l,l,0))=TRUE)&" ",
FILTER(l, ISEVEN(MATCH(l,l,0))=TRUE))}))
((REGEXREPLACE(FLATTEN(SPLIT(REGEXEXTRACT(rg, "\:{(.+)\}}"), ",:")), """","")))))))
Option 2
=ArrayFormula(LAMBDA(s, SPLIT(QUERY(FLATTEN(IF(s="",,SPLIT(s, ""&CHAR(10)&""))), " Select Col1 "),"♦"))
(QUERY({
IF(G3:G="",,
BYROW(G3:G, LAMBDA(rg,
LAMBDA(l, TEXTJOIN(CHAR(10), 1, {
CONCAT(FILTER(l, ISODD(MATCH(l,l,0))=TRUE)&"♦",
FILTER(l, ISEVEN(MATCH(l,l,0))=TRUE))}))
((REGEXREPLACE(FLATTEN(SPLIT(REGEXEXTRACT(rg, "\:{(.+)\}}"), ",:")), """",""))))))}, "Select Col1 Where Col1 <>'' ")))
Used formulas help
ARRAYFORMULA - LAMBDA - SPLIT - QUERY - FLATTEN - IF - CHAR - BYROW - TEXTJOIN - CONCAT - FILTER - ISODD - MATCH - ISEVEN - REGEXREPLACE - REGEXEXTRACT

Google Sheets formula to merge multiple columns into one cell, including headers / label

I want to combine multiple columns in a Google sheet into one cell, including headers / labels. My input sheet looks like this:
Name
Description
Col 1
Col 2
Col 3
Foo
val 1
val 2
val 3
Bar
val x
val y
val z
And I want to concatenate the values in each column into a single cell, including the headers for each value. Desired output:
Name
Description
Foo
Col 1val 1Col 2val 2Col 3val 3
Bar
Col 1val xCol 2val yCol 3val z
This formula concatenates the columns into one, however it doesn't include the headers (note that char(10) produces a linebreak):
=TEXTJOIN(char(10),true,B2:E2)
I can produce a similar result using REDUCE (I can't believe I'm writing a reducer in a Google Sheet):
=REDUCE("", B2:E2, LAMBDA(accumulator, current_value, CONCATENATE(accumulator, current_value, char(10))))
However the lambda function doesn't seem to provide the index of the current iteration, so I'm not sure how to grab the column name above it.
Use this formula
=ARRAYFORMULA(
QUERY({"Description";
TRIM(SUBSTITUTE(FLATTEN(QUERY(TRANSPOSE(
IF(B2:D="",,","&B1:D1&","&B2:D&",")),"",9^9)),",",CHAR(10)))},
" Where Col1 is not null" ) )
try the old way:
=ARRAYFORMULA(TRIM(REGEXREPLACE(TRIM(FLATTEN(QUERY(TRANSPOSE(
IF(B2:D="",,"×"&B1:D1&"×"&B2:D&"×")),,9^9))), "×", CHAR(10))))
or in one cell if that's what you want:
=INDEX(TRIM(REDUCE(, B1:D1&CHAR(10)&B2:D3&CHAR(10),
LAMBDA(a, c, a&c&CHAR(10)))))
Using your current TEXTJOIN() formula you can just include the Column Headers with the dollar sign ($) which means absolute reference so it will not change when copied/dragged down to rows below.
Try:
=TEXTJOIN(char(10),true,C$1,C2,char(10),D$1,D2,char(10),E$1,E2)
Drag down to rows.
Result:
Refer to this link for more info on Absolute Reference: https://www.lido.app/tutorials/absolute-reference-on-google-sheets

ARRAYFORMULA is only populating the first row

I have financial data that I am trying to summarize in a format that can be used by a line chart.
The example spreadsheet is here.
In my source data on the left, I have an entry per Date, Symbol, Account. I need to transform this data so there is a row per Date and a column for each Symbol. I will SUM Total Value regardless of account.
I found a way to pull a unique Date (see H2), and then transpose unique Symbols into columns (see I1).
I also found a way to use SUMIFS to get the aggregation I want (take a look at cell I2), but I can't figure out how to use ARRAYFORMULA to apply this value to all rows in column I.
I know I can drag my formula from I2 down to I3,I4,I.. etc, but this sheet is part of a larger project so I'd like it to auto-populate as dates are added to H.
From what I've read ARRAYFORMULA should apply the formula to multiple rows. What am I missing?
Thanks
use:
=QUERY(A1:F, "select A,sum(F) where A is not null group by A pivot B", 1)
Use formulas like this
=ARRAYFORMULA(IF(H2:H="",,SUMIFS($F$2:$F, $A$2:$A, $H2, $B$2:$B, I$1)))
Add IF(H2:H="",,
Explanation
if the range is empty "" do nothing ,, else Your formula
Your Example
Cells
Formulas
I2
=ARRAYFORMULA(IF(H2:H="",,SUMIFS($F$2:$F, $A$2:$A, $H2:H, $B$2:$B, I$1)))
J2
=ARRAYFORMULA(IF(H2:H="",,SUMIFS($F$2:$F, $A$2:$A, $H2:H, $B$2:$B, J$1)))
K2
=ARRAYFORMULA(IF(H2:H="",,SUMIFS($F$2:$F, $A$2:$A, $H2:H, $B$2:$B, K$1)))

Splitting data separated by a | in a cell to new rows

I have a Spreadsheet (see link at the bottom of the page) that has 1 row and 3 columns.
I want to take the data contained and split it out, resulting in a row by row breakdown.
Is anyone aware of how this could be done using a formula? It would save me a bunch of time doing it manually!
DemoSheet - This shows what the input and the desired outputs are
EDIT:
The Input sheet shows the data as I have it, using metasyntactic variables as examples (real data will vary, but will always follow the same formatting).
For every email address in the email column, I need to do the following
Get the list of managers and members and have it output as per the Desired Output 1 sheet. So for each entry in ColA, a row entry for each of the data in B and C, as if they were concatenated, split by " | " and transposed vertically.
Repeat the above process but only for managers (as per the the Desired Output 2 sheet).
Is this what you need?
Output1:
=arrayformula({"Email","Members";
query(
array_constrain(
{
flatten(split(rept("|"&Input!A2:A,len(regexreplace(Input!B2:B&" | "&Input!C2:C,"[^\|]",))+1),"|")),
trim(flatten(split(Input!B2:B&"|"&Input!C2:C,"|")))
},
max(if(Input!B2:B<>"",len(regexreplace(Input!B2:B&" | "&Input!C2:C,"[^\|]",))+1,))*counta(Input!B2:B),2),
"where Col1 is not null",0)
})
Output2:
=arrayformula({"Email","Manager";
query(
array_constrain(
{
flatten(split(rept("|"&Input!A2:A,len(regexreplace(Input!B2:B,"[^\|]",))+1),"|")),
trim(flatten(split(Input!B2:B,"|")))
},
max(if(Input!B2:B<>"",len(regexreplace(Input!B2:B,"[^\|]",))+1,))*counta(Input!B2:B),2),
"where Col1 is not null",0)
})
You could optionally wrap unique() within the arrayformula if it is likely that you'll get duplicates in the dataset.

Google Sheet pulling data from another spreadsheet if match

So I'm stuck with something. I have two spreadsheets, column A on each are similar but not identical, some values are on one spreadsheet but not the other.
Is it possible for me to pull data from spreadsheet 2, based on if column A has a matching value?
So spreadsheet 2 will have something like:
A B
item1 100
item2 200
item3 300
and spreadsheet 1 will have something like:
A B
item1 NULL
item2 NULL
item4 NULL
I want to populate the B columns on spreadsheet 1 based on whether they are on spreadsheet 2 (in this instance, it would populate items 1 and 2)
I've tried VLOOKUP and If statements but don't seem to be getting anywhere.
The VLOOKUP function will do this for you, providing that you set the optional is_sorted parameter to FALSE, so that closest matches will not be returned.
Here's an example. First, our source sheet, Sheet1.
On Sheet2, we use VLOOKUP to pull info from Sheet1, using this formula (from B1, in this case):
=vlookup(A1,Sheet1!$A$1:$B,2,false)
^ -------------- ^ ^
| | | +-- is_sorted
| | +------- return value from col 2
| +------------------- Range for search
+------------------------- search_key
Ok, but that #N/A for item4 is not pretty. We can suppress it by wrapping the VLOOKUP in IFERROR. When the optional second argument of IFERROR is omitted, it will return a blank cell if the first argument evaluates to an error:
=IFERROR(vlookup(A1,Sheet1!$A$1:$B,2,false))
In your example, the data is coming from a separate spreadsheet, not just a different sheet within the current spreadsheet. No problem - VLOOKUP can be combined with IMPORTRANGE as its data source.
=IFERROR(vlookup(A1,IMPORTRANGE("<sheet-id>","Sheet1!A1:B"),2,false))
Due to recent changes in Google Sheets, the formula from AdamL and Mogsdad only seems to work when you connect the 2 sheets together.
Remove parts from the formula that don't belong to the importrange..
=IMPORTRANGE("<URL other sheet>";"Sheet1!A2:C")
You get a REF# error but when you hover over the error you can confirm a connection.
I confirmed it works for all cells so you can copy paste the complete formula.
=IFERROR(vlookup(B128;IMPORTRANGE("<URL other sheet>";"SHeet1!A2:C");3;false);)
Following the official documentation you need the complete URL from the other sheet..

Resources