Google Spreadsheet: Concatenate values if UNIQUE ID is equal - join

I am using g-sheet and trying to combine the company name in the case of the unique ID in the interval is equal
UNIQUE ID: 1 1 1 2 2 3
COMPANY NAME: A B C Z E K
Expected output:
1: A, B, C
2: Z, E
3, K

try:
=ARRAYFORMULA(FLATTEN(REGEXREPLACE(TRIM(QUERY(QUERY(
{A2:A&":", B2:B&","},
"select max(Col2)
where not Col1 starts with ':'
group by Col2
pivot Col1"),,9^9)), ",$", )))

Related

How to get unique values in a column, including cells with multiple values seperated by commas, in Google Sheet?

I have a column in a Google Sheet, which in some cases, includes multiple values separated by commas — like this:
Value
A example
B example
C example
D example
A example, E example
A example, F example
G example, D example, C example
I would like to count all occurrences of the unique values in this column, so the count should look like:
Unique value
Occurrences
A example
3
B example
1
C example
2
D example
2
E example
1
F example
1
G example
1
Currently, however, when I use =UNIQUE(A2:A), the result gives this:
Unique value
Occurrences
A example
1
B example
1
C example
1
D example
1
A example, E example
1
A example, F example
1
G example, D example, C example
1
Is there a way I can count all of the instances of letters, whether they appear in individually in a cell or appear alongside other letters in a cell (comma-seperated)?
(This looks like a useful answer in Python, but I'm trying to do this in Google Sheets)
try:
Formula in C1:
=INDEX(QUERY(IFERROR(FLATTEN(SPLIT(A1:A,", ")),""),"Select Col1, count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"))
Or, as per the comments, split on the combination instead:
=INDEX(QUERY(IFERROR(FLATTEN(SPLIT(A1:A,", ",0)),""),"Select Col1, count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"))
2nd EDIT: To order descending by count use:
=INDEX(QUERY(IFERROR(FLATTEN(SPLIT(A1:A,", ",0)),""),"Select Col1, count(Col1) where Col1 is not null group by Col1 Order By count(Col1) desc label count(Col1) ''"))
Assuming data in A1:A7:
In C1:
=SORT(UNIQUE(FLATTEN(ARRAYFORMULA(SPLIT(A1:A7,", ")))))
In D1:
=ARRAYFORMULA(MMULT(0+ISNUMBER(SEARCH(", "&ColumnCSpilledRange&", ",", "&TRANSPOSE(A1:A7)&", ")),ROW(A1:A7)^0))
Replace ColumnCSpilledRange appropriately.

Google sheets "array_agg"... or equivalent? (join aggregation)

Currently, I have this
=QUERY(
QUERY(
'Raw Paste'!C2:E, "select C, count(C) where C is not null group by C order by C label count(C) ''"
), "WHERE Col2 >= 2")
The second QUERY() is so I can filter the aggregate function like an SQL HAVING function...
That will do this:
What I want to do though is next to the count, I want a 3rd column that joins the invoice numbers that are included in the aggregate.
This would be trivial with ARRAY_AGG(C) but google sheets ain't that fancy.
I've considered maybe using INDEX/MATCH somehow but I dunno.. I need to join the strings together where an item appears more than once.
C D
111 PPP
222 OOO
222 QQQ
The output I want:
C D
222 OOO, QQQ
It can be done by using FILTER and JOIN
=IFERROR(JOIN(", ", FILTER(D2:D, C2:C = A3)))
FILTER will look in C2:C for A2 and return the values from D2:D, which get passed to JOIN
=ARRAYFORMULA(REGEXREPLACE(TRIM({QUERY(QUERY(C:D,
"select C,count(C) where C is not null group by C pivot D", 0), "select Col1 offset 1", 0),
TRANSPOSE(QUERY(TRANSPOSE(IF(ISNUMBER(QUERY(QUERY(C:D,
"select count(C) where C is not null group by C pivot D", 0), "offset 1", 0)),
QUERY(QUERY(C:D,
"select count(C) where C is not null group by C pivot D", 0), "limit 0", 1)&",", ))
,,999^99))}), ",$", ))

Concatenate list of IDs based on matching IDs within a comma separated list in a different column

Using google sheets:
I've got two columns.
Column A is a list of ID numbers:
N1
N2
N3
N4
N5
Column B is a comma separated list of other ID numbers within column A related to the ID number on that same row:
N2,N3
N3,N4
(null)
(null)
N1
I'm trying to make a formula in a third column, column C, that will display a comma separated list of the ID numbers from column A that match the ID numbers entered entered in Column B.
Intended result:
A | B | C
N1 | N2,N3 | N4,N5
N2 | N3,N4 | N1
N3 | (null) | N1,N2
N4 | N1 | N2
N5 | N1 | (null)
The closest I could get was this formula here:
=arrayFormula({concatenate(rept(A:A&",",B:B=A2))})
But this will only work if multiple items haven't been entered into column B, so using this only "N4,N5" would be returned in column C rather than the rest shown in the intended result.
Edit(updated Image): I'm now seeing the following, seems there's an error somewhere:
try like this:
=ARRAYFORMULA(IFERROR(VLOOKUP(A1:A,
{QUERY(QUERY(SPLIT(TRIM(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(IFERROR(SPLIT(B1:B, ","))<>"", "♦"&SPLIT(B1:B, ",")&"♠"&A1:A, ))
,,999^99)),,999^99), "♦"))), "♠"),
"select Col1,count(Col1) group by Col1 pivot Col2"), "select Col1 offset 1", 0),
SUBSTITUTE(REGEXREPLACE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(ISNUMBER(
QUERY(QUERY(SPLIT(TRIM(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(IFERROR(SPLIT(B1:B, ","))<>"", "♦"&SPLIT(B1:B, ",")&"♠"&A1:A, ))
,,999^99)),,999^99), "♦"))), "♠"),
"select count(Col1) group by Col1 pivot Col2"), "offset 1", 0)),
QUERY(QUERY(SPLIT(TRIM(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(IFERROR(SPLIT(B1:B, ","))<>"", "♦"&SPLIT(B1:B, ",")&"♠"&A1:A, ))
,,999^99)),,999^99), "♦"))), "♠"),
"select count(Col1) group by Col1 pivot Col2"), "limit 0", 1)&",", ))
,,999^99))), ",$", ), ", ", ",")}, 2, 0)))

calculate difference between 2 column pairs (product/stock)

In a google spreadsheet I have 2 column pairs, EAN + number in stock
Some EAN's are present in both column A and C, some only in column A column and some only in column B.
Eaxmple:
A B C D E F
8573489753888 1 8729029847359 2
8789026119040 1 8434234872389 1
8789026118692 3 8789026118609 2
8729029847359 1 8789026118692 1
I need to find EAN's present in both column A and C and calculate the stock difference (column B and D). The result should be listed in column E and F.
I created a script that does this, but since I keep hitting the max execution time (the list is getting long), I am hoping this could be done without a script as well.
The results should be as follows:
A B C D E F
8573489753888 1 8729029847359 2 8789026118692 2
8789026119040 1 8434234872389 1 8729029847359 -1
8789026118692 3 8789026118609 2
8729029847359 1 8789026118692 1
full diference:
=ARRAYFORMULA(QUERY({A1:B; C1:C, D1:D*-1},
"select Col1,sum(Col2)
where Col1 is not null
group by Col1
label sum(Col2)''", 0))
reverse:
relevant diference:
=ARRAYFORMULA(QUERY({A1:B, IFERROR(VLOOKUP(A1:A, C1:D, 2, 0))},
"select Col1,Col2-Col3
where Col3 is not null
label Col2-Col3''", 0))
reverse:
Nice solution. I tried just with vlookup and it gets really complicated: =IF(ISNA(VLOOKUP(E1;A:B;2;FALSE()));IF(ISNA(VLOOKUP(E1;C:D;2;FALSE()));0;VLOOKUP(E1;C:D;2;FALSE()));VLOOKUP(E1;A:B;2;FALSE())-IF(ISNA(VLOOKUP(E1;C:D;2;FALSE()));0;VLOOKUP(E1;C:D;2;FALSE()))

COUNTIF in Google Sheets with multiple 'not' or 'else' than conditions for responses from Google Forms

I want to count the number of 'other' responses from 2 questions from Google Forms recorded in a Google Sheet.
The 2 questions from Google Forms are for selecting between multiple answers or writing down in text some 'other' response, for example:
Question 1 is 'location' (Site A, Site B, Site C, Site D, other)
Question 2 is 'type of equipment' (PC 1, PC 2, Laptop 1, Laptop 2, other)
So I need to count all the 'other' type of equipment registered that are at 'other' locations.
I've tried different ways to write down the formula (countif, counta, query, etc) but the results give me a 1 when it should give me a zero. I also tried to simplify the formula to write the 'other' types of equipment for 'Site A' but the answer is still 1 weirdly enough.
I'm parting from the answers in :
Countifs in Google Sheets with various 'different than' criteria in same row adds +1 value
Those answered formulas work perfectly for a count of all the 'other' equipment in all the 'other' locations but still gives me a response of '1' for specific locations.
I think the problem is that I'm mixing 2 queries/formulas but I'm not making them work together with an 'and' as I require, or if the value is 0 then it's giving me a 1.
This is my attempt A to isolate 1 location with 'other' types of equipment and it somehow gives a '1' response (I tried the variance with '<>PC1' type:
=COUNTA(QUERY(datos_equipos!$J2:$J,"Site A", datos_equipos!$B$2:$B,
"where not B contains 'PC 1'
and not B contains 'PC 2'
and not B contains 'Laptop 1'
and not B contains 'Laptop 2'
", 0))
These are my attempts at counting both 'other' responses'
In Attempt A I tried to make 2 queries:
=COUNTA({QUERY(datos_equipos!$J2:$J, "where J <> 'Site A' and J <> 'Site B' and J <> 'Site C' and J <> 'Site D'")
& query (datos_equipos!$B$2:$B, "where B <> 'PC 1)'
and B <> 'PC 2'
and B <> 'Laptop 1'
and B <> 'Laptop 2)'", 0)})
Attempt B is the same that A but with the 'different than' <> inside the '' :
=COUNTA({QUERY(datos_equipos!$J2:$J, "where J '<>Site A' and J '<>Site B' and J '<>Site C' and J '<>Site D'")
& query (datos_equipos!$B$2:$B, "where B '<>PC 1'
and B '<>PC 2'
and B '<>Laptop 1'
and B '<>Laptop 2'", 0)})
Attempt C is trying to make a direct count while calling each option and trying to exclude blank cells:
=countifs(datos_equipos!$J2:$J, "<>Site A",
datos_equipos!$J2:$J, "<>Site B",
datos_equipos!$J2:$J, "<>Site C",
datos_equipos!$J2:$J, "<>Site D",
datos_equipos!$B$2:$B, "<>PC 1",
datos_equipos!$B$2:$B, "<>PC 2",
datos_equipos!$B$2:$B, "<>Laptop 1",
datos_equipos!$B$2:$B, "<>Laptop 2",
datos_equipos!$B$2:$B,"<>"
)
And lastly, attempt D., In this case, the answer is 2. Here I tried to query each option:
=COUNTA({QUERY(datos_equipos!$J2:$J, "where J '<>Site A'")
& query (datos_equipos!$J2:$J, "where J '<>Site B'")
& query (datos_equipos!$J2:$J, "where J '<>Site C'")
& query (datos_equipos!$J2:$J, "where J '<>Site D'")
& query (datos_equipos!$B$2:$B, "where B '<>PC 1'" )
& query (datos_equipos!$B$2:$B, "where B 'PC 2'")
& query (datos_equipos!$B$2:$B, "where B '<>Laptop 1'")
& query (datos_equipos!$B$2:$B, "where B '<>Laptop 2'")
, 0})
In sum:
In Google Sheets, I need the number of 'other' equipment registered at 'other' locations. Both are the fields that the user gives instead of the given selectable answers.
I made a test doc for it. So far, the Attempt D in sheet 'repo_equipos_global' in cells N6-I6 works best. As long as there's data that matches, given that the original comes from a form that should not be a problem. [ Link ] (https://docs.google.com/spreadsheets/d/1hnKw6LjG3Vv6-1Yg60RzzXnsh6uzFKYqyu1D36EA1jQ/edit?usp=sharing)
cell C8:
=ARRAYFORMULA(COUNTA(IFERROR(QUERY(QUERY(
LOWER({datos_equipos!B2:B, datos_equipos!J2:J}),
"where Col2 contains '"&LOWER(C2)&"'
or Col2 contains 'site1'", 0),
"select Col1
where not Col1 contains 'PC 1'
and not Col1 contains 'PC 2'
and not Col1 contains 'PC 3'
and not Col1 contains 'Laptop 1'
and not Col1 contains 'Laptop 2'", 0))))
cell F8:
=ARRAYFORMULA(COUNTA(IFERROR(QUERY(QUERY(
LOWER({datos_equipos!B2:B, datos_equipos!J2:J}),
"where not Col2 contains 'site1'
and not Col2 contains 'site2'
and not Col2 contains 'site3'
and not Col2 contains 'site 1'
and not Col2 contains 'site 2'
and not Col2 contains 'site 3'", 0),
"select Col1
where not Col1 contains 'PC 1'
and not Col1 contains 'PC 2'
and not Col1 contains 'PC 3'
and not Col1 contains 'Laptop 1'
and not Col1 contains 'Laptop 2'", 0))))
cell C3 (if you do not differentiate between site 1 and site1 states):
=COUNTIFS(datos_equipos!$B:$B, $A3, datos_equipos!$J:$J, C$2)+
COUNTIFS(datos_equipos!$B:$B, $A3, datos_equipos!$J:$J, SUBSTITUTE(C$2, " ", ""))
demo spreadsheet

Resources