Skip to content

Instantly share code, notes, and snippets.

@Hugoberry
Last active January 20, 2020 16:10
Show Gist options
  • Save Hugoberry/f1d03b632e895b1d455603dda36201c5 to your computer and use it in GitHub Desktop.
Save Hugoberry/f1d03b632e895b1d455603dda36201c5 to your computer and use it in GitHub Desktop.
[Power Query] return the table at the end of the path eg. xmlPath(XML,{"auditfile","customersSuppliers","customerSupplier"})
(xml as table, path as list)=>
let
isEmptyTable = each Type.Is(Value.Type(_), Table.Type) and List.Count(Table.ToRows(_))=0,
xmlRow2record = (rec, row) => Record.AddField(rec,row{0},if isEmptyTable(row{2}) then null else row{2}),
xmlTableExpandNode = (xmlTable, node) => List.Accumulate(Table.SelectRows(xmlTable, each [Name] = node)[Value],#table(1,{}),(s,c)=>s&c),
head = List.RemoveLastN(path,1),
tail = List.Last(path),
exceptTail = List.Accumulate(head,xml,xmlTableExpandNode),
tailRecords= List.Transform(Table.SelectRows(exceptTail, each [Name]=tail)[Value],each List.Accumulate(Table.ToRows(_),[],xmlRow2record)),
out = Table.FromRecords(tailRecords)
in
out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment