自动扩展具有特定名称的所有“表列”

时间:2018-06-22 11:10:00

标签: excel powerbi powerquery m

我是编码的初学者,对“ M”建模语言不熟悉。我有一个XML文件,要用于在查询编辑器中加载数据。在查询中,我只需要在下面展开具有特定名称的表列:

  1. 视图
  2. viewfolder
  3. 属性:名称

我找到了以下post,其中他们给出了function by Chris Webb来扩展所有列表(以下代码)。

= (TableToExpand as table, optional ColumnNumber as number) =>
    let
     ActualColumnNumber = if (ColumnNumber=null) then 0 else ColumnNumber,
     ColumnName = Table.ColumnNames(TableToExpand){ActualColumnNumber},
     ColumnContents = Table.Column(TableToExpand, ColumnName),
     ColumnsToExpand = List.Distinct(List.Combine(List.Transform(ColumnContents, each if _ is table then Table.ColumnNames(_) else {}))),
     NewColumnNames = List.Transform(ColumnsToExpand, each ColumnName & "." & _),
     CanExpandCurrentColumn = List.Count(ColumnsToExpand)>0,
     ExpandedTable = if CanExpandCurrentColumn then Table.ExpandTableColumn(TableToExpand, ColumnName, ColumnsToExpand, NewColumnNames) else TableToExpand,
     NextColumnNumber = if CanExpandCurrentColumn then ActualColumnNumber else ActualColumnNumber+1,
     OutputTable = if NextColumnNumber>(Table.ColumnCount(ExpandedTable)-1) then ExpandedTable else ExpandAll(ExpandedTable, NextColumnNumber)
    in
     OutputTable

但是如何仅扩展所需的列表/表?

我已将示例文件上传到following location。下面是查询的屏幕截图

Query Editor Screenshot update

1 个答案:

答案 0 :(得分:1)

您应该能够简单地在旧的ColumnsToExpand行上放置一个过滤器。

也就是说,

 TableColumns = //This is the old ColumnsToExpand definition renamed.
     List.Distinct(List.Combine(List.Transform(ColumnContents, each if _ is table then Table.ColumnNames(_) else {}))),
 ColumnsToExpand =
     List.Select(TableColumns, each (_ = "view" or _ = "viewfolder" or _ = "Attribute:name")),

或者像这样一行,

 ColumsToExpand = List.Select(List.Distinct(List.Combine(List.Transform(ColumnContents, each if _ is table then Table.ColumnNames(_) else {}))), each (_ = "view" or _ = "viewfolder" or _ = "Attribute:name")),