【问题标题】:Power Query - Data Transformation from a single column to a whole tablePower Query - 从单列到整个表的数据转换
【发布时间】:2018-10-08 18:27:01
【问题描述】:

我有一个要求,我有一张这样的桌子 -

包含 2 列的实际表格

Column1                                              Column2
ColAValue $$ ColBValue $$                            New Row
ColCValue                                            Above Row
ColCValue2                                           Above Row
$$ ColDValue                                         Above Row
ColAValue $$ ColBValue $$ ColCValue $$ ColDValue     New Row
ColAValue $$ ColBValue $$ ColCValue                  New Row
$$ ColDValue                                         Above Row

我知道根据要求,我的数据集中会有 4 列,离开第 2 列

我需要使用查询编辑器将转换后的表作为新表。

这是我的预期输出,

OutTable 有 4 列

基本上,列值由分隔符 $$ 按顺序标识,如果 column2 表示新行,则它是新记录,否则,它必须将当前行附加为新列值。

如何在查询编辑器中将输入表转换为输出表?

最终输出的数据类型无关紧要。

初始步骤是将上方行中的行值带入 带有分隔符的新行并将其作为单行。

【问题讨论】:

    标签: powerbi powerquery m


    【解决方案1】:

    这里的关键是创建一个分组列,将每一行分配给其结果输出行号。您可以通过在Column2 中使用“新行”查找最后一行的索引来做到这一点。

    首先,创建一个索引列(在“添加列”选项卡下)。

    现在您可以通过采用上述最大索引来创建分组自定义列。公式可能如下所示:

    List.Max(
        Table.SelectRows(#"Prev Step Name",
            (here) => [Index] >= here[Index] and here[Column2] = "New Row"
        )[Index]
    )
    

    您的表格现在应该如下所示:

    现在我们使用 Group By(在 Home 标签下),按 Group 列分组并聚合 Column1

    但是我们要将聚合从 List.Max 更改为 Text.Combine 以便这一步的代码是

    = Table.Group(#"Added Custom", {"Group"},
          {{"Concat", each Text.Combine([Column1]," "), type text}})
    

    现在表格应该是这样的:

    从这里,您可以使用 " && " 作为分隔符来按分隔符拆分列(在主页选项卡下)。

    根据需要更改任何列名,如果您不再需要 Group 列,则删除它,结果应该是您需要的输出。


    整个查询的M代码:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7PcQxLzClNVVBRUQBynGAcJR0lv9RyhaD8cqVYHbA6Z7AUUNwxKb8sFVPGCEMKYqQLTn3YbVaAm6iAZgCagwhpR9OB2zWxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
        #"Reordered Columns" = Table.ReorderColumns(#"Added Index",{"Index", "Column1", "Column2"}),
        #"Added Custom" = Table.AddColumn(#"Reordered Columns", "Group", each List.Max(Table.SelectRows(#"Reordered Columns", (here) => [Index] >= here[Index] and here[Column2] = "New Row")[Index]), Int64.Type),
        #"Grouped Rows" = Table.Group(#"Added Custom", {"Group"}, {{"Concat", each Text.Combine([Column1]," "), type text}}),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Grouped Rows", "Concat", Splitter.SplitTextByDelimiter(" $$ ", QuoteStyle.Csv), {"COL1", "COL2", "COL3", "COL4"}),
        #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Group"})
    in
        #"Removed Columns"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多