Power Query - 从单列到整体的数据转换 table

Power Query - Data Transformation from a single column to a whole table

我有一个要求,我有一个像这样的 table -

实际 Table 有 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 列

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

这是我的预期输出,

OutTable 有 4 列

基本上,列值按分隔符 $$ 的顺序标识,如果 column2 表示新行,则它是一条新记录,否则,它必须作为新列值追加到当前行。

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

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

The initial step is to bring the row values from Above row into the New row with a delimiter and have it as a single row.

这里的关键是创建一个分组列,将每一行分配给它的结果输出行号。您可以通过在 Column2.

中查找带有 "New Row" 的最后一行的索引来执行此操作

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

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

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

您的 table 现在应该如下所示:

现在我们使用分组依据(在主页选项卡下),按 Group 列分组并聚合 Column1

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

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

现在 table 应该是这样的:

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

根据需要更改任何列名称,如果您不再需要 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"