Search code examples
filtergoogle-sheetsgoogle-sheets-formulaarray-formulastextjoin

ArrayFormula - If cell contains match, combine other cells with TEXTJOIN


I have a Google Sheet that contains names of characters, together with corresponding values for the group name, "selected" and attack power. It looks like this:

Sheet1

| NAME     | GROUP NAME | SELECTED  | ATTACK POWER |
|:---------|:-----------|----------:|-------------:|
| guile    | Team Red   |         1 |          333 | 
|----------|------------|-----------|--------------|
| blanka   | Team Red   |         1 |           50 |
|----------|------------|-----------|--------------|
| sagat    | Team Red   |           |          500 |
|----------|------------|-----------|--------------|
| ruy      | Team Blue  |         1 |          450 |
|----------|------------|-----------|--------------|
| vega     | Team Blue  |         2 |          150 |

Sheet2

In my second sheet, I have two columns. Group name, which contains names of each team from Sheet1 and names, which contains my current ArrayFormula:

=ARRAYFORMULA(TEXTJOIN(CHAR(10); 1; 
 REPT('Sheet1'!A:A; 1*('Sheet1'!B:B=A2))))

Using this formula I can combine all characters into one cell (with textjoin, repeated with row breaks) based on the value in Group name. The result looks like the following:

| GROUP NAME | NAME                      |
|:-----------|:--------------------------|
| Team Red   | guile                     |         
|            | blanka                    |
|            | sagat                     |
|------------|---------------------------|
| Team Blue  | ruy                       |
|            | vega                      |
|------------|---------------------------|

The problem is that I only want to combine the characters with having a selected value of 1. End-result should instead look like this:

| GROUP NAME | NAME                      |
|:-----------|:--------------------------|
| Team Red   | guile                     |         
|            | blanka                    |
|------------|---------------------------|
| Team Blue  | ruy                       |
|------------|---------------------------|

I tried the following setup using a IF-statement, but it just returns a string of FALSE:

=ARRAYFORMULA(TEXTJOIN(CHAR(10); 1; 
 REPT(IF('Sheet1'!C:C="1";'Sheet1'!A:A); 1*('Sheet1'!B:B=A2))))

Can this be one?


Solution

  • paste in F2 cell:

    =UNIQUE(FILTER(B:B, C:C=1))
    

    paste in G2 cell and drag down:

    =TEXTJOIN(CHAR(10), 1, FILTER(A:A, B:B=F2, C:C=1))
    

    0


    or G2 cell be like:

    =ARRAYFORMULA(TEXTJOIN(CHAR(10), 1, 
     REPT(FILTER(Sheet1!A:A, Sheet1!C:C=1), 1*(FILTER(Sheet1!B:B, Sheet1!C:C=1)=F2))))
    

    enter image description here