Repeating a list N times Google Sheets - google-sheets

I think it's best I provide an example for this one.
Input:
a 2
b 3
c 4
d 1
Output Goal:
a
a
b
b
b
c
c
c
c
d
So I want to repeat the value in Cell A1 n times (n is located in column B1)
I considered using the following function then transposing and stacking the array:
=SPLIT(REPT(A1&",",B1),",")
However, I was hoping there is a single formula that can solve this.

Try the following formula:
=transpose(split(concatenate(arrayformula(rept(A1:A4&",",B1:B4))),","))

=TRANSPOSE(SPLIT(JOIN(",", ARRAYFORMULA(REPT(SPLIT(
INDIRECT("A1:A"&COUNTA(A1:A)), ",")&",",
INDIRECT("B1:B"&COUNTA(B1:B))))), ","))

Related

Workaround Argument must be a range error when using SUMIF + FILTER

Col A
Col B
Col C
a
back
1
a
back
1
b
b
draw
1
c
back
1
c
d
draw
1
d
draw
1
e
draw
1
In Column E I put the values from Column A using UNIQUE and sorting with SORT:
=SORT(UNIQUE(A:A))
In Column F I tried to put a single formula in the first row, to sum each of the total values in Col C according to some filters:
=ARRAYFORMULA(IF(E1:E="","",SUMIF(
FILTER(A1:A,(B1:B<>"draw")*(C1:C<>"")),
E1:E,
FILTER(C1:C,(B1:B<>"draw")*(C1:C<>""))
)))
But I get the error:
Argument must be a range.
Expected Result:
Col E
Col F
a
2
c
1
Is there a way to make the filters become ranges or how should I proceed to avoid this error?
try:
=QUERY(A1:C, "select A,sum(C) where not B matches 'draw|^$' group by A label sum(C)''")

Google Sheet Query: Return All Row Data only in Distinct Columns

I currently have a query returning all data where Particular columns match certain conditions, but I would also like to prevent the return of duplicate data from column1.
A
B
C
D
1
x
y
z
1
x
z
x
2
r
z
w
3
q
y
z
4
q
t
q
4
q
u
r
So far:
=QUERY(A:D, "SELECT * WHERE B = 'x' OR 'q'")
Returns:
A
B
C
D
1
x
y
z
1
x
z
x
3
q
y
z
4
q
t
q
4
q
u
r
How can I also remove the duplciates in column A? So it displays the below:
A
B
C
D
1
x
y
z
3
q
y
z
4
q
t
q
There are many ways to go about this. Given your exact post information, which runs A:D with no headers, here is one way:
=ArrayFormula(UNIQUE(IFERROR(VLOOKUP(FILTER(A:A,ISNUMBER(SEARCH(B:B,"q|x"))),A:D,SEQUENCE(1,COLUMNS(A:D)),FALSE))))
FILTER forms a limited array of only those items in A:A where the corresponding value in B:B exists within the SEARCH criteria string.
VLOOKUP looks up and returns all four columns of values for each of those, which will return the first row of values encountered for every equal value in the FILTERed A:A values. This will result in exact duplicate information for each matching row.
I used SEQUENCE to return an array asking for the return of columns 1,2,3,4 from the requested range. This parameter could have been entered as {1,2,3,4}. But I find SEQUENCE to be more flexible, for instance if in your actual sheet you want the return of 12 columns and not only 4.
UNIQUE eliminates the duplicates.
And, of course, ArrayFormula is necessary because we are processing a range of values.
try:
=ARRAY_CONSTRAIN(SORTN(QUERY({A:D, A:A&B:B},
"where Col2 matches 'x|q'"), 9^9, 2, 5, 1), 9^9, 4)

Mix data of multiple columns into one column

I want to combine the content of three columns into one column like in this example:
Col1 Col2 Col3
A 1 x
B 2 Y
So the result is a column with 8 lines like this:
Result
A 1 X
A 1 Y
A 2 X
A 2 Y
B 1 X
B 1 Y
B 2 X
B 2 Y
I need a Google Sheets command to do it, not a Spreadsheet solution because the result values must change automatically when using the sheet.
I have already tried with COMBINE and JOIN without much success.
If, alternatively, you show me how to combine only two of the three columns, I then could combine the third one in a second step.
2 columns:
=ARRAYFORMULA(TRANSPOSE(SPLIT(REPT(CONCATENATE(A1:A&CHAR(9)), COUNTA(B1:B)), CHAR(9)))
&" "&TRANSPOSE(SPLIT(CONCATENATE(REPT(B1:B&CHAR(9), COUNTA(A1:A))), CHAR(9))))
3 columns:
=ARRAYFORMULA(
TRANSPOSE(SPLIT(REPT(CONCATENATE(
TRANSPOSE(SPLIT(REPT(CONCATENATE(A1:A&CHAR(9)), COUNTA(B1:B)), CHAR(9)))&" "&
TRANSPOSE(SPLIT(CONCATENATE(REPT(B1:B&CHAR(9), COUNTA(A1:A))), CHAR(9)))&CHAR(9)),
COUNTA(C1:C)),CHAR(9)))&" "&
TRANSPOSE(SPLIT(CONCATENATE(REPT(C1:C&CHAR(9), COUNTA(
TRANSPOSE(SPLIT(REPT(CONCATENATE(A1:A&CHAR(9)), COUNTA(B1:B)), CHAR(9)))&" "&
TRANSPOSE(SPLIT(CONCATENATE(REPT(B1:B&CHAR(9), COUNTA(A1:A))), CHAR(9)))))), CHAR(9))))
3 columns in two steps:
=ARRAYFORMULA(TRANSPOSE(SPLIT(REPT(CONCATENATE(D1:D&CHAR(9)), COUNTA(C1:C)), CHAR(9)))
&" "&TRANSPOSE(SPLIT(CONCATENATE(REPT(C1:C&CHAR(9), COUNTA(D1:D))), CHAR(9))))
Assuming A is in A1, in Row1 and copied down to suit:
=offset(A$1,int((row()-1)/4),)&" "&offset(B$1,abs(iseven(int((row()-1)/2))-1),)&" "&offset(C$1,mod((row()-1),2),)
Rotate, join, rotate, join then etc. There are no other ways to reach this. If I had such a task, I would try to multiply N matrices N-1 times.
=ARRAYFORMULA(TRANSPOSE(SPLIT(
TEXTJOIN("·",TRUE,SPLIT(
TEXTJOIN("·",TRUE,(A2:A3&" "&TRANSPOSE(B2:B3))),
"·")&" "&C2:C3),
"·"
)))

Improve Query Function in Google Sheets

I've been using the following function:
=query(Sheet1!A2:D," select A, B, C where A matches '"&JOIN("|", A2:A)&"' and D matches 'yes'")
Is there anyway that I can make is so that every row that starts with a match will be added a comma separated list in which each column occupies one cell with no duplicates as shown in sheet3.
https://docs.google.com/spreadsheets/d/1YDxIUnZzzYde9hcexPoDegv4HBuiUwk2wLKSXazu9hE/edit?usp=sharing
Sheet 2 has the function that I used and the result.
It is not entirely clear what you want to do, but try this. In Sheet1 in E2 put this combining Col A and D:
=arrayFormula(A2:A & if(isBlank(D2:D),""," ") & D2:D)
In F2 combine Col C and D with this:
=arrayFormula(B2:B & if(isBlank(C2:C),"",",") & C2:C)
In G2 find the unique values from Col F with this:
=UNIQUE(E2:E)
In H2 put this and drag the formula down:
=join(",",query(E2:F,"select F where E contains '"& G2 &"'"))
Hide Cols E & F

Filter out phone numbers from Google Sheets query using regexreplace?

I have a list of number using this query formula.
=query(uploadData!A:M,"Select B, C, H, I, M where not(C) contains '"&JOIN("|",filter!A:A)&"' and B contains 'Incoming' and not B is null and not H is null ",1)
You can see I'm trying to filter the results so as to NOT include any matching numbers found in filter!A:A. However, it doesn't like the fact that the result and filter sheet use the telephone number format (###) ###-####.
I believe using regexreplace will help resolve this issue but I'm not knowledgeable enough with regexreplace to know how to incorporate it.
Can anyone tell me how I can achieve this?
I'm not sure, google sheets query understand pipes | as OR logic, so you may rty this formula instead:
=query(uploadData!A:M,"Select B, C, H, I, M where not C contains '"&JOIN("' and not C contains '",filter(filter!A2:A,filter!A2:A<>""))&"' and B contains 'Incoming' and not B is null and not H is null ",1)
I used JOIN("' and not C contains '",filter(filter!A2:A,filter!A2:A<>"")):
join + "' and not C contains '" to get all conditions one by one
filter(filter!A2:A,filter!A2:A<>"") to have only not empty cells

Resources