Power Query中的条件列标题

时间:2018-10-08 18:01:19

标签: excel powerquery

由于我的数据具有KEYTEXT格式,其中{{1}有可用的列标题,因此我的标题标题在某些列中丢失了},但不能使用KEY。下面是一个例子。

Image showing the data table

我希望以这样一种方式来获取每列的标题,即使改变数据集的位置,逻辑也能起作用。在excel中,我按照图片2进行操作。

Header Title formula

结果如图3所示

Conditional header formula

当前,我正在使用PowerQuery,并且对该工具还很陌生。有谁知道可以解决的步骤,所以即使数据源的位置发生了变化,列标题仍会保持在Excel中。我会很高兴。

1 个答案:

答案 0 :(得分:1)

以下是可能的解决方案:

在Excel中,将表加载到Power Query,请取消选中My table has headers

在Power Query编辑器中查找Advanced Editor,然后复制并粘贴以下行:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type any}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}, {"Column8", type any}, {"Column9", type any}, {"Column10", type any}}),
    #"Transposed Table" = Table.Transpose(#"Changed Type"),
    #"Added Conditional Column" = Table.AddColumn(#"Transposed Table", "Custom", each if [Column1] = null then [Column2] else [Column1]),
    #"Duplicated Column" = Table.DuplicateColumn(#"Added Conditional Column", "Custom", "Custom - Copy"),
    #"Filled Down" = Table.FillDown(#"Duplicated Column",{"Custom"}),
    #"Added Conditional Column1" = Table.AddColumn(#"Filled Down", "Custom.1", each if [#"Custom - Copy"] = null then [Custom] & " Name" else [Custom]),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Conditional Column1",{"Custom.1", "Column1", "Column2", "Column3", "Column4", "Column5", "Custom", "Custom - Copy"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Column1", "Custom", "Custom - Copy"}),
    #"Transposed Table1" = Table.Transpose(#"Removed Columns"),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table1", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"StudentID", type any}, {"Country", type text}, {"Country Name", type text}, {"Department", type text}, {"Department Name", type text}, {"Mayor", type text}, {"Mayor Name", type text}, {"AGE", type any}, {"WEIGHT", type any}, {"HEIGHT", type any}})
in
    #"Changed Type1"

在Excel中加载为表格:

enter image description here