PowerBi转置-表格

时间:2018-09-21 13:28:15

标签: datatable powerbi transpose powerquery powerbi-desktop

我有一个Excel文件,该文件需要从多列转换为表格格式,但是在Power Query中合并行时遇到了麻烦。

样本数据

Sample Data

预期结果

Expected Outcome

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在加载到PowerQuery中之前,请在excelsheet中插入一行,并使用定界符将标题连接到value列中。如果使用office365,则可以使用TEXTJOIN函数执行此操作。结果看起来像这样(我没有复制所有数据):

enter image description here

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

  1. 删除前3行
  2. 将第一行用作标题
  3. 选择前3列
  4. 取消透视其他列(“转换”标签上的下拉菜单unpivot columns
  5. 选择[属性]列
  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"

结果:

enter image description here