动态逆透视列表中的每一列,powerquery

时间:2021-04-04 02:47:02

标签: powerquery

我正在尝试对列表中提供的每一列进行动态逆透视。

(Table as table, ColumnNameList as list) as table =>
    List.Accumulate(
        List.Zip(ColumnNameList, {1..List.Count(ColumnNameList)}),
        Table,
        (state, column) => Table.Unpivot(state, column{0}, "Option" & column{1} & "Name", "Option" & column{1} & "Value")
    )

此代码不起作用,我该怎么做?

1 个答案:

答案 0 :(得分:0)

我最终创建了一个函数来提供唯一的列名,有点像如果现有列已经有名称,Power query 可视化编辑器将如何自动创建名称。

不过,一旦你通过了多个同名的人,就无法测试它是如何工作的。

(Table as table, ColumnName as text) =>
    if Table.HasColumns(Table, ColumnName) then
        let
            NewName =
                if Text.At(ColumnName, 3) <> "." then
                    ColumnName & ".01"
                else
                    Text.Start(
                        ColumnName,
                        Text.Length(ColumnName) - 2
                    )
                    & Text.PadStart(
                        Text.From(
                            Number.FromText(Text.End(ColumnName, 2))
                            + 1
                        ),
                        2,
                        "0"
                    )
        in
            @FnTableNewColumnName(Table, NewName)
    else
        ColumnName

然后我改变了我正在处理的函数,为两个额外的参数调用命名函数。

(Table as table, ColumnNameList as list) as table =>
    List.Accumulate(
        ColumnNameList,
        Table,
        (state, column) =>
            Table.Unpivot(
                state,
                { column },
                FnTableNewColumnName(state, "attribute"),
                FnTableNewColumnName(state, "value")
            )
    )