简体   繁体   English

Excel:如何收集一列中与另一列中的重复项相关联的唯一值?

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

With data such as this:有了这样的数据:

Column A     Column B
1               98
1               12
1               21 
1               31   
2               37
2               40
3               48 
4               34
4               88
4               74 
4               99
7               82
7               19
7               29
7               50 
7               95
7               85

where all values in Column B are unique, and Column A has multiple duplicates.其中Column B中的所有值都是唯一的,而Column A有多个重复项。 How can I group the A values in one column, and display concatenated values of B in another, like this:如何将 A 值分组在一列中,并在另一列中显示 B 的连接值,如下所示:

Column C   Column D
1             98,12,21,31
2             37,40
3             48
4             34,88,74,99
7             82,19,29,50,95,85

I've tried with a combination of concatenate and index and match, but it all just turns into a mess.我尝试过将连接、索引和匹配结合起来,但一切都变得一团糟。

Any suggestions would be great?有什么建议会很棒吗?

First, you need to extract unique value from Column A to Column C .首先,您需要从Column A Column CColumn C Column A提取唯一值。 You can do it by using Advance Filter method or you can use below formula.您可以使用Advance Filter方法来完成,也可以使用以下公式。

=IFERROR(INDEX($A$2:$A$18,MATCH(0,INDEX(COUNTIF($C$1:C1,$A$2:$A$18),0,0),0)),"")

After extracting unique values you have to use TEXTJOIN() formula to aggregate values from Column B to Column D .提取唯一值后,您必须使用TEXTJOIN()公式将值从Column B Column D聚合到Column D You have TEXTJOIN() formula in your excel version then you can use it like below您的 excel 版本中有TEXTJOIN()公式,然后您可以像下面一样使用它

=TEXTJOIN(", ",TRUE,IF($A$2:$A$18=C2,$B$2:$B$18,""))

Otherwise you have to user VBA custom function to write TextJoin() formula.否则,您必须使用VBA自定义函数来编写TextJoin()公式。 For TEXTJOIN() custom function you can have look to this post.对于TEXTJOIN()自定义函数,您可以查看这篇文章。 Post Link 发布链接

在此处输入图片说明

Let me add two additional methods to the answer by @Harun24HR.让我在@Harun24HR 的答案中添加两个额外的方法。 Both options assume you don't have headers as per your sample data.这两个选项都假定您没有根据示例数据的标题。


Option 1) : Dynamic Array Functions选项 1) :动态数组函数

When one has access to dynamic array functions you may use the following:当您可以访问动态数组函数时,您可以使用以下内容:

In C1 :C1

=UNIQUE(A1:A17)

This UNIQUE function will spill an array of unique values from defined range into column C.UNIQUE函数会将定义范围内的唯一值数组溢出到 C 列中。

In D1 :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.FILTER将从 B 列中提取 A 列匹配的所有值,而TEXTJOIN会将这些值连接到您想要的字符串中。

Drag down...拖累...


Option 2) : PowerQuery选项 2)PowerQuery

Would you want to experiment with PowerQuery/GetAndTransform then you don't need any formulas nor VBA for that matter.您想尝试使用PowerQuery/GetAndTransform那么您不需要任何公式或VBA Follow these steps:按着这些次序:

  • Select A1:B17 and from the ribbon choose Data > From Table/Range under "Get & Transform Data"选择A1:B17 ,并从色带选择Data > From Table/Range下的“获取并转换数据”
  • Choose to import data without headers.选择导入没有标题的数据。 A new window will open.将打开一个新窗口。
  • From the ribbon click Transform > Group By .从功能区中单击Transform > Group By依据。 Within that menu choose to group by Column1, choose a new column name, eg: "Grouped" and then choose All Rows from the Operation dropdown and click OK .在该菜单中选择按 Column1 分组,选择一个新列名称,例如:“分组”,然后从“操作”下拉列表中选择“ All Rows ”并单击“ 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") .现在在功能区上单击Add Column > Custom Column并输入以下公式: Table.Column([Grouped], "Column2") This should add a third column that holds a list of values.这应该添加包含值列表的第三列。
  • Remove Grouped from the table.从表中删除Grouped 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.选择Extract Values ,然后选择逗号作为分隔符。

There might be a translation-error in the M-code below, but this should be it:下面的 M 代码中可能存在翻译错误,但应该是这样:

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.如果我没记错的话,可以从 Excel-2010 中使用 PowerQuery,因此您不需要访问像TEXTJOIN这样的高级公式来执行此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 Excel 中的唯一值使用唯一颜色对列中的重复项进行颜色格式化? - How to color format duplicates in a column with unique colors for unique values in Excel? 如何根据另一列中的重复项修改列并在 Excel 中保留唯一值 - How to amend a column based on duplicates in another and leave a unique value in Excel 在 excel 中,如何根据另一列中的条件返回一列中唯一值的数量 - In excel how can I return the number of unique values in one column based on criteria in another column 如何将一个 Excel 工作表中特定列的单元格值作为新的唯一列附加到另一个 Excel 工作表中 - How to append cell values of a particular column in one excel sheet as new unique columns into another excel sheet 如何提取一列中而不是另一列中的唯一值? Excel - How do i pull out unique values that are in one column but not another? Excel 在Excel中,通过VBA或公式/函数的组合,根据另一列中的值从一列中删除重复项 - In Excel, remove duplicates from one column based on the values in another column, either through VBA or a combination of formulas/functions Excel 如果一列包含唯一值,而另一列包含一个真值,则返回这些唯一值的所有真值 - Excel If one column contains unique values, and another column contains one true value, return all true values for those unique values Excel - 如何在不使用数组公式和辅助列的情况下将唯一值从一列提取到另一列? - Excel - How can I extract unique values from one column to an another column without using array formulas and helper columns? Excel - 在一列中查找重复项,然后将数量总和到另一列中? - Excel - Find duplicates in one column then sum quantities into another column? 在列中显示唯一值以及与每个值关联的另一个值 - Showing unique values in a column along with another value associated with each
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM