PowerBi 转置 - Table

PowerBi Transpose - Table

我有一个 excel 文件需要从多列转置为 table 格式,但在 Power Query 中合并行时遇到问题。

示例数据

预期结果

非常感谢您的提前帮助。

在加载到 PowerQuery 之前,在您的 Excel 表中插入一行并使用分隔符连接值列中的 headers。如果你使用 office365,你可以使用 TEXTJOIN 函数来执行此操作。结果看起来像这样(我没有复制你所有的数据):

将此表格导入 PowerQuery 并执行以下步骤:

  1. 删除前 3 行
  2. 将第一行用作headers
  3. Select前3列
  4. 逆透视其他列(转换选项卡上的下拉菜单 unpivot columns
  5. Select [属性]列
  6. 按分隔符(分号)拆分列

脚本看起来像这样。

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type any}, {"Column5", type any}}),
    #"Removed Top Rows" = Table.Skip(#"Changed Type",3),
    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Actual;jun;FY-2017", Int64.Type}, {"Actual;jul;FY-2017", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"Column1", "Column2", "Column3"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Attribute.1", "Attribute.2", "Attribute.3"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", type text}, {"Attribute.2", type text}, {"Attribute.3", type text}})
in
    #"Changed Type2"

结果: