动态展开记录

时间:2018-01-24 12:12:20

标签: powerquery m

我想动态扩展记录,因为我得到的来源不固定。

这是有效的,但它已经修复,我必须说明应该扩展哪些元素。

let
        Quelle = Sage.Contents(),
        records = Quelle{[Name="Kontakte"]}[Data],
        #"SelectItems" = Table.SelectColumns(records,{"$items"}),
        #"$items1" = #"SelectItems"{0}[#"$items"],
        #"ToTable" = Table.FromList(#"$items1", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"ExpandedColumn" = Table.ExpandRecordColumn(#"ToTable", "Column1", {"id", "displayed_as", "$path"}, {"Column1.id", "Column1.displayed_as", "Column1.$path"})
    in
        #"ExpandedColumn"

Webservice响应:

{
"$total":3,"$page":1,"$next":null,"$back":null,"$itemsPerPage":20,
"$items":[
{"id":"1","displayed_as":"Test","$path":"/contacts/Test"},
{"id":"2","displayed_as":"Test2","$path":"/contacts/Test2"},
{"id":"3","displayed_as":"Test3","$path":"/contacts/Test3"}
]}

" $项"是动态的。这取决于联系人背后有多少信息。

"id", "displayed_as", "$path"

也可能是

"id", "displayed_as", "$path", "city", "zip", "street", "number"

"id", "displayed_as", "$path", "city", "zip", "street", "number", "phone", "mobile"

2 个答案:

答案 0 :(得分:0)

您只需添加两个变量并编辑展开行:

SelectAllColumns = Table.ColumnNames(#"$items1"), //Create a list with the columns names
SelectAllColumns2 = List.Transform(SelectAllColumns, each "a."&_), //Create a second list with columns names changed like a.columnname
#"ExpandedColumn" = Table.ExpandRecordColumn(#"ToTable", "Column1", SelectAllColumns, SelectAllColumns2) //Replace these lists inside your last line

我认为最终的结果将是:

let
    Quelle = Sage.Contents(),
    records = Quelle{[Name="Kontakte"]}[Data],
    #"SelectItems" = Table.SelectColumns(records,{"$items"}),
    #"$items1" = #"SelectItems"{0}[#"$items"],

    SelectAllColumns = Table.ColumnNames(#"$items1"),
    SelectAllColumns2 = List.Transform(SelectAllColumns, each "a."&_),

    #"ToTable" = Table.FromList(#"$items1", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"ExpandedColumn" = Table.ExpandRecordColumn(#"ToTable", "Column1", SelectAllColumns, SelectAllColumns2)
in
    #"ExpandedColumn"

答案 1 :(得分:0)

假设M中的上一步被命名为Removed Other Columns,并且要扩展的列被命名为Data,那么我用以下代码替换了#"Expanded Data"的静态行: / p>

#"Expanded Data" =Table.ExpandTableColumn(#"Removed Other Columns", "Data", List.Union(List.Transform(#"Removed Other Columns"[Data], each Table.ColumnNames(_))))

它动态地适应任何列名。