Excel: How to gather unique values in one column that are associated with duplicates in another column?

Let me add two additional methods to the answer by @Harun24HR. Both options assume you don’t have headers as per your sample data.


Option 1) : Dynamic Array Functions

When one has access to dynamic array functions you may use the following:

In C1:

=UNIQUE(A1:A17)

This UNIQUE function will spill an array of unique values from defined range into column C.

In D1:

=TEXTJOIN(",",TRUE,FILTER(B$1:B$17,A$1:A$17=C1))

Whereas FILTER will extract all values from column B where column A matches it is TEXTJOIN that will concatenate these values into your desired string.

Drag down…

Or, in a single go, exploiting TOCOL():

=LET(x,UNIQUE(TOCOL(A:A,1)),HSTACK(x,MAP(x,LAMBDA(y,TEXTJOIN(",",,FILTER(B:B,A:A=y))))))

Option 2) : PowerQuery

Would you want to experiment with PowerQuery/GetAndTransform then you don’t need any formulas nor VBA for that matter. Follow these steps:

  • Select A1:B17 and from the ribbon choose Data > From Table/Range under “Get & Transform Data”
  • Choose to import data without headers. A new window will open.
  • From the ribbon click Transform > Group By. Within that menu choose to group by Column1, choose a new column name, e.g.: “Grouped” and then choose All Rows from the Operation dropdown and click OK.
  • You’ll notice an extra column. Now on the ribbon click Add Column > Custom Column and enter the following formula: Table.Column([Grouped], "Column2"). This should add a third column that holds a list of values.
  • Remove Grouped from the table. Then click on the icon to the right of the newly added column name, and you’ll have two options. Choose Extract Values, then choose a comma as your delimiter.

There might be a translation-error in the M-code below, but this should be it:

let
    Source = Excel.CurrentWorkbook(){[Name="Tabel1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Column1"}, {{"Grouped", each _, type table [Column1=number, Column2=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.Column([Grouped], "Column2")),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Removed Columns" = Table.RemoveColumns(#"Extracted Values",{"Grouped"})
in
    #"Removed Columns"

PowerQuery is available from Excel-2010 if I’m not mistaken so you wouldn’t need access to advanced formulas like TEXTJOIN to perform this.

Leave a Comment