Dynamically expand ALL lists and records from json

2020-03-30 01:46发布

问题:

I want to expand all lists and records in a json response. Columns are like e.g. (this is dynamically, it also can be 10 records and 5 lists):

Text, Text, [List], [List], Text, [Record], [Record], String, [Record]

I wrote a function for getting all columns with the specific type

Cn.GetAllColumnsWithType = (table as table, typ as type) as list =>
let
    ColumnNames = Table.ColumnNames(table),
    ColumnsOfType = List.Select(ColumnNames, (name) =>
        List.AllTrue(List.Transform(Table.Column(table, name), (cell) => Type.Is(Value.Type(cell), typ))))
in
    ColumnsOfType;

and a function to expand all lists from a table

Cn.ExpandAllListsFromTable = (table as table, columns as list) =>
let
    expandedListTable = List.Accumulate(columns, table, (state, columnToExpand) =>
        Table.ExpandListColumn(state, columnToExpand))
in
    expandedListTable;

all lists are now records and i want to dynamically expand all these records.

I think i need a foreach to iterate through the list (which are only records cause of Cn.GetAllColumnsWithType), Table.ExpandRecordColumn each element with it's Table.ColumnNames and add it to the table but i don't know how to do it. Maybe you can help me out cause it's driving me crazy. Cheers

Edit: I recently opened a thread but there i wanted to expand a specific one like

#"SelectItems" = Table.SelectColumns(records,{"$items"}),
#"$items1" = #"SelectItems"{0}[#"$items"],

but now i want to do it all dynamically.

回答1:

Chris Webb wrote a function to do this for Table-type columns:

http://blog.crossjoin.co.uk/2014/05/21/expanding-all-columns-in-a-table-in-power-query/

I've shared a tweaked version of that that I made for Record-type columns:

https://gist.github.com/Mike-Honey/0a252edf66c3c486b69b



回答2:

Assuming that the previous step in M was named Removed Other Columns, and that the column to expand is named Data, then I replace the line with #"Expanded Data" with the following code:

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

It dynamically adapt to any column names.



标签: powerquery m