如何按两列分组并列出另外两列的唯一值?

问题描述 投票:0回答:1

我有这个 csv:

car     color  code city
ferrari pink   01   LA
ferrari red    02   LA
lambo   yellow 09  Texas
lambo   orange 08  Texas

我想在powerbi中进行这种分组,按汽车和城市分组,列出代码和颜色的唯一值并计算唯一行数。 预期产出:

car     color            code      city  count
ferrari [pink, red]      [01,02]   LA    2
lambo   [yellow, orange] [09,08]   Texas 2

也许 CONCATENEX ?但真的不知道

powerbi powerquery powerbi-desktop data-cleaning m
1个回答
1
投票

给你。

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkstKkosylTSUSrIzMsGUoZA7OOoFKuDLFeUmgIkjRBSOYm5SflAbmVqTk5+OZBhCcQhqRWJxSjS+UWJeempQIYFQjoWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [car = _t, color = _t, code = _t, city = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"car", type text}, {"color", type text}, {"code", type text}, {"city", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"car", "city"}, {{"count", each Table.RowCount(_), Int64.Type}, {"color", each "["& Text.Combine( _[color],",")&"]", type text },{"code", each "["& Text.Combine( _[code],",")&"]", type text }}),
    #"Reordered Columns" = Table.ReorderColumns(#"Grouped Rows",{"car", "color", "code", "city", "count"})
in
    #"Reordered Columns"
© www.soinside.com 2019 - 2024. All rights reserved.