如何将具有相同 ID 的行合并到列表中

How to combine rows with the same ID into a list

如何使下面的 table 成为一个列表。

Id   Name
1    Tim
1    George
2    Rachael
3    Mark
3    Blake

我想要这样的结果

Id    Name
1     Tim,George
2     Rachael
3     Mark,Blake

有什么想法吗?

尝试以下方法,它可能会解决您的问题。

假设您现有的 table 名称是 yourTable,要创建的新 table 是 groupedNames。在数据视图中,单击新建 table 并粘贴以下内容:

groupedNames = calculatetable
(
    addcolumns(
        summarize(yourTable ,yourTable[Id ]),
        "Names",calculate(CONCATENATEX(yourTable,[ Name ],","))
    )   
)

我喜欢 DAX 答案,这是 Power Query 解决方案,稍微冗长一些,但我都是从工具栏上完成的。粘贴进去可以清楚看到步骤

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQrJzFWK1YGw3VPzi9JTwVwjIDcoMTkjNQfMNQZyfROLsuEcp5zEbKDKWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Id = _t, Name = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", Int64.Type}, {"Name", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Id"}, {{"allNames", each _, type table [Id=number, Name=text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each [allNames][Name]),
#"Extracted Values" = Table.TransformColumns(#"Added Custom", {"Custom", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
#"Removed Columns" = Table.RemoveColumns(#"Extracted Values",{"allNames"}) 
in
#"Removed Columns"

这可以通过用 Text.Combine 替换分组依据中的现有聚合函数一步完成。

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQrJzFWK1YGw3VPzi9JTwVwjIDcoMTkjMTUHzDcG8n0Ti7LhHKecxGyg0lgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Id = _t, Name = _t]),
    #"Grouped Rows" = Table.Group(Source, {"Id"}, {{"Names", each Text.Combine([Name],","), type text}})
in
    #"Grouped Rows"

为此,我使用 GUI 在 Id 列上进行分组,并告诉它为我的新 Names 聚合列取 Name 列的最大值。

这会生成步骤

= Table.Group(Source, {"Id"}, {{"Names", each List.Max([Name]), type text}})

您需要做的就是将 List.Max 换成 Text.Combine

我试过那个解决方案,这是我的版本,但有超过 2 列。

 #"Grouped Rows" = Table.Group(#"Pivoted Column", {"id", "primaryEmailAddress", "identifier", "fullName", "employerName", "employerIdentifier", "city", "stateProvince", "postalCode", "country", "role.id", "role.name", "type.id", "type.name",
                                                      "isDeleted", "lastMeeting", "emptyCustomField", "CS1", "CS2"}, {{"alternativeEmailAddresses.id", each Text.Combine([alternativeEmailAddresses.id],", "), type nullable text}, {"alternativeEmailAddresses.address", 
                                                      each Text.Combine([alternativeEmailAddresses.emailAddress],","), type nullable text}}),