通过另一列中指示的数字多次重复 Power BI 中的行
Duplicate Rows in Power BI multiple times by a number indicated in another column
我遇到的问题是,在 Power BI 中我有一个 table 像:
col1 col2
entry1 1
entry2 2
entry3 1
我想创建一个 table 的形式:
col1
entry1
entry2
entry2
entry3
也就是说,您按不同列中指定的数字复制每一行。在我的实际情况下,我的 table 有许多其他列,其值也应在每一行中重复。
我希望能够使用强大的查询来做到这一点。
谢谢
您可以使用公式
向您的 table 添加自定义列
List.Repeat( { [col1] }, [col2] )
这会生成一个列,每行都有一个列表,其中列表的元素被 [col1]
列出 [col2]
次。
从那里,您可以使用 table 上的按钮将该列表扩展成行。
完整的 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"
在这里,您可以选择 col1
或 Custom
并删除其他列(如果您选择)。
我遇到的问题是,在 Power BI 中我有一个 table 像:
col1 col2
entry1 1
entry2 2
entry3 1
我想创建一个 table 的形式:
col1
entry1
entry2
entry2
entry3
也就是说,您按不同列中指定的数字复制每一行。在我的实际情况下,我的 table 有许多其他列,其值也应在每一行中重复。
我希望能够使用强大的查询来做到这一点。
谢谢
您可以使用公式
向您的 table 添加自定义列List.Repeat( { [col1] }, [col2] )
这会生成一个列,每行都有一个列表,其中列表的元素被 [col1]
列出 [col2]
次。
从那里,您可以使用 table 上的按钮将该列表扩展成行。
完整的 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"
在这里,您可以选择 col1
或 Custom
并删除其他列(如果您选择)。