Is it possible to expand data in excel/googlesheets? - google-sheets

Here's an simplified example of what I am trying to do.
cat 2
dog 3
Translated to:
cat
cat
dog
dog
dog
Is that possible with just a function in a cell or do I have to use something like a macro? How would I do that?

In Google sheets, assuming dog and cat are in A1 and A2 and 2 and 3 are in B1 and B2, this will do it:
=transpose(split(rept(A1&" ",B1)&rept(A2&" ",B2)," "))

In Google sheets, assuming "dog 2" and "cat 3" are in A1 and A2, this will do it:
=transpose(QUERY({(split(rept(split(A1," ")&" ",query(split(A1," "), "SELECT Col2"))," ")),(split(rept(split(A2," ")&" ",query(split(A2," "), "SELECT Col2"))," "))},,))
Credit:
G4mo in StackOverflow How to make a range repeat n-times in Google SpreadSheet

In Excel, you can use either VLOOKUP() or a combination of INDEX() and MATCH().
With data in cols A and B, in C1 enter 1 and in C2 enter:
=C1+B1
and copy downward. In D1 enter:
=IF(ROWS($1:1)>SUM(B:B),"",INDEX(A:A,MATCH(ROWS($1:1),C:C,1)))
and copy downwards:
The helper column D translates the repetition factor in column B into match index.
(Placing a helper column to the left of the data would allow the use of VLOOKUP())

Related

Array of IMPORTRANGEs in Google Sheets

For this sample table:
A
B
C
D
E
1
Range!A1:C5
URL1
= Formula here
2
URL2
3
URL3
4
...
I have this working formula in C1:
={IMPORTRANGE(B1, A1); IMPORTRANGE(B2, A1); IMPORTRANGE(B3, A1)}
A1 contains the range to pull from the target Sheets.
Links to the target Sheets are found in column B.
(This is a simplified example. My actual Sheet has 21 entries in column B.)
Question:
If I will have to add/remove URLs in column B, I have to adjust the formula in C1 and add/remove IMPORTRANGEs.
I would like to have a formula that automatically adjusts to the entries in column B.
Solutions tried:
I tried this, but it did not work:
={JOIN("", ARRAYFORMULA(IF(LEN(B1:B), "IMPORTRANGE(""" & B1:B & """, """ & A1 & """); ", "")))}
The JOIN function returns a text that should be identical to what the array { ... } parses as a formula. But it doesn't. Wrapping it with INDIRECT also does not work.
Any suggestions on how I could make this work?
if by "working formula" you mean valid formula that returns data then only thing you can do is hardcode it like this:
=QUERY({
IFERROR(IMPORTRANGE(B1, A1), {"","",""});
IFERROR(IMPORTRANGE(B2, A1), {"","",""});
IFERROR(IMPORTRANGE(B3, A1), {"","",""})},
"where Col1 is not null", )
A1:C5 = 3 columns = {"","",""}
you can generate formula dynamically with arrayformula but the result will be always a text string and not active formula. tho if you wanna go this way you will need a script that will convert your text string into active formula. example:
https://stackoverflow.com/a/73119313/5632629
https://stackoverflow.com/a/61819704/5632629

Google Sheet | Excel | Array Formula + CountIf + Partial Text Problem

I'm pretty new with ArrayFormula, have been trying but sometime the formula works, sometimes does not. What I'm trying to do is the combination of ArrayFormula, Countif for searching partial text.
As shown in the worksheet below, there are 10 subjects (column A), each subject has at least one of 4 samples (A,B,C,D) summarized as a string (column B). What I'm trying to do is to find which subject has sample A or B or C or D.
I have tried single formula for each sample, eg cell D3
=IF(COUNTIF($B3,"*"&$D$2&"*")>0,$A3,"")
it returns the correct results. However, when I try arrayformula in cell I3,
=arrayformula(IF(COUNTIF($B3:B,"*"&$D$2&"*")>0,$A3:A,""))
The answers are weird. For example: Subjects (Gamma, Zeta, Eta, Theta) who don't have the sample "A" are shown to have sample "A". And this applies to sample B,C,D too
Not sure what went wrong in here. Here is the link to the worksheet
I wouldn't use Countifs or an array formula. Use filter instead. Put this formula in cell i3.
=Filter(if(REGEXMATCH(B3:B,$D$2),A3:A,""),B3:B<>"")
try:
=INDEX(QUERY(IFERROR(TRIM(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B3:B, ","))="",,
SPLIT(B3:B, ",")&"×"&A3:A)), "×"))),
"select max(Col2) where Col2 is not null group by Col2 pivot Col1"))
or use in row 2 if you want to sort it as in your example:
=INDEX(IFNA(VLOOKUP(A2:A, QUERY(IFERROR(TRIM(SPLIT(FLATTEN(
IF(IFERROR(SPLIT(B3:B, ","))="",,SPLIT(B3:B, ",")&"×"&A3:A)), "×"))),
"select Col2,max(Col2) where Col2 is not null group by Col2
pivot Col1 label Col2'Subjects'"), {2,3,4,5}, 0)))
You can accomplish all four columns of results with a single formula.
Delete all formulas from I3:L3.
Place the following formula into I3:
=ArrayFormula(IF(REGEXMATCH(B3:B,I2:L2),A3:A,))
In plain speech, this read "If anything in B3:B matches a value found in I2:L2, return A3:A in the matching columns(s) at the matching row(s); if not, return null."

search for a specific Column and then lookup the value of the matching row based off a name

I am trying to get a google sheet to search for a specific cell in a table. The headers change so it might be A6 one week and then A9 the other and so on.
Once it's found that row, I want it to search and pull all of that departments names and data for the column its matched with.
I am 23 sheets in and my heads hit a brick wall and I just can figure it out.
You can try:
=QUERY({A:B,INDEX(A:G,0,MATCH(D25,1:1,0))},"SELECT * WHERE Col2='" & LOWER(F25) & "'")
Note - you should remove unnecessary spaces. In sample data, they were in cells C1 and D25.
Try this:
=QUERY(
FILTER(
IFS(
TRIM(1:20) = "", 0,
ISNUMBER(1:20), 1:20,
True, LOWER(TRIM(1:20))
),
1:1 <> ""
),
"SELECT Col1, Col2, Col" & MATCH(TRIM(D25), ARRAYFORMULA(TRIM(1:1)),) & "
WHERE Col2 = '" & LOWER(F25) & "'",
1
)
You can use a combination of CHAR(MATCH...)) and Query formula to get this
=QUERY('Sheet Name'!A1:G20,"SELECT A, B, "&CHAR(MATCH("Log 4",'Sheet Name'!A1:G1)+64)&" WHERE B='w'")
Above formula only works till column Z, but thanks to Kishkin's comment below, you can use it beyond Z like this:
=QUERY('Sheet Name'!A1:G20,"SELECT A, B, `" & =REGEXEXTRACT(ADDRESS(1, MATCH("Log 4",'Sheet Name'!A1:G1), 4), "\D+") & "` WHERE B='w'")
You use SUBSTITUTE instead of REGEXTRACT too. You can refer to this Question.
the CHAR(MATCH...)) gets the column name of the desired Log
you can then use the column name to include that column in Query select statement
In the MATCH formula, you can also dynamically Match for a cell reference instead of specifying "Log 4" manually
Note: This is basically splitting the Query formula and concatenating it with another formula. So the &, ' and " symbols are really important
Sample Screenshot:

Google Spreadsheet adding sheetname

I have my current query below;
=QUERY({Source_1!B2:I;Source_2!B2:I;Source_3!B2:I},"select * where Col3 is not null",0)
I want to have all my data be combine to one to "All_Sheet" tab it works so far.
Now I need in Col A of my tab "All_Sheet" be included to all rows the Sheet Name like Source_1, Source_2 and so on.
Try this:
Add a calculated column
First build a range that contains the sheet name you want to reference. I did it by concatenating a range that is empty and the sheet name:
{N2:N&"Sheet1",Source_1!B2:I}
The above by itself won't work, because it needs to be told that it is an array. So we wrap it in ARRAYFORMULA():
ARRAYFORMULA({N2:N&"Sheet1",Source_1!B2:I})
Which gives a range that looks like this:
Then build up the rest of your query like this:
Sheet 1 | B2 Value | C2 Value | D2 Value | ...
Build our Query
Take the above and build up your multiple ranges and complete your query. Linebreaks for readability
=QUERY(
ARRAYFORMULA(
{
N2:N&"Sheet1",Source_1!B2:I;
N2:N&"Sheet2",Source_2!B2:I;
N2:N&"Sheet3", Source_3!B2:I
}),
"select * where Col3 is not null",0)

Spreadsheet Sorting Function

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'")

Resources