首页 > 解决方案 > 按另一列中指示的数字多次重复 Power BI 中的行

问题描述

我遇到的问题的想法是,在 Power BI 中,我有一个如下表:

col1 col2
entry1 1
entry2 2
entry3 1

我想创建一个表格:

col1
entry1
entry2
entry2
entry3

也就是说,您按不同列中指定的数字复制每一行。在我的实际情况下,我的表还有许多其他列,其值也应该在每一行中重复。

我希望能够使用电源查询来做到这一点。

谢谢

标签: powerbidaxpowerquerym

解决方案


您可以使用公式将自定义列添加到表中

List.Repeat( { [col1] }, [col2] )

这会在每一行中生成一个包含列表的列,其中列表的元素被[col1]列出[col2]了很多次。

从那里,您可以使用表格上的按钮将该列表展开为行。

完整的 M 代码如下所示:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSs0rKao0VNJRMlSK1YFyjYBcIwTXGCIbCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [col1 = _t, col2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"col1", type text}, {"col2", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Repeat({[col1]},[col2])),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom")
in
    #"Expanded Custom"

扩展行

从这里,您可以选择col1Custom删除其他列(如果您选择)。


推荐阅读