[英]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 C
到Column 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:按着这些次序:
A1:B17
and from the ribbon choose Data
> From Table/Range
under "Get & Transform Data"A1:B17
,并从色带选择Data
> From Table/Range
下的“获取并转换数据”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
.All Rows
”并单击“ OK
。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.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.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.