excelexcel-formula

excel - Find all rows that have 2 out of 3 columns in common, combine with the uncommon column of each to make 4 column rows


I am new to Excel. I have a spreadsheet containing 140 rows of 3 columns each, and trying to find the rows which share 2 of its columns with another row, and add the unique columns from each row to them. I can't seem to wrap my head around how to go about this.

For example:

Person 1 Person 2 Person 3
Amy Brian CJ
Amy Brian Dylan
Amy Dylan Erica
Brian CJ Dylan
CJ Erica Fiona
Dylan Erica Fiona

Each row has its cells sorted alphabetically. I would like to be able to create a new table containing all possible 4 person groups.

Person 1 Person 2 Person 3 Person 4
Amy Brian CJ Dylan
Amy Brian Dylan Erica
Amy Dylan Erica Fiona
CJ Dylan Erica Fiona

Solution

  • enter image description here

    Formula in E2:

    =LET(_data,A2:C7,TEXTSPLIT(TEXTAFTER(UNIQUE(BYROW(DROP(REDUCE(0,SEQUENCE(ROWS(_data)),LAMBDA(_main,_iter,LET(_index,INDEX(_data,_iter,),_4th,BYROW(FILTER(_data,BYROW(_data,LAMBDA(_row,SUM(COUNTIF(_index,_row))=2))),LAMBDA(_row,FILTER(_row,COUNTIF(_index,_row)=0))),IF(@ISERR(_4th),_main,VSTACK(_main,IF({1,0,0,0},_4th,HSTACK(0,_index))))))),1),LAMBDA(_row,TEXTJOIN(0,0,,SORT(_row,,,1))))),0,{1,2,3,4}),0))

    If need be I can add some explanation.

    ------------------------------------

    Edit: As per request, I'll add an explanation. Hopefully it helps.

    Explanation:

    I hope this made sense. Just in case, here is a very simplified visual (over two screenshots to make it fit) of the 6 iterations within the accumulator and what happens at the end outside the accumulator to arrive at the finale results.

    enter image description here

    enter image description here