首页 > 解决方案 > Excel / Powerquery:重塑和转置表格的一部分

问题描述

我有一个这样的表,我使用 powerquery 构建了来自不同地方的大量数据。

在此处输入图像描述

现在我被困在尝试转置其中的一部分,同时保持主列不变。我需要将其更改为以下格式,以便通过我无法更改的不同脚本进行进一步处理。

在此处输入图像描述

它可以是简单的 excel 或 powerquery,甚至可以是 VB 脚本。任何可以解决问题的东西。

谢谢

标签: excelvbapowerquery

解决方案


您可以在 Power Query 中执行以下操作:

1) 反透视除客户和月份以外的列

2)以值作为值列的枢轴月份

如下面的代码

let
    Source = Excel.CurrentWorkbook(){[Name="Table7"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Month", type text}, {"Revenue", type number}, {"Product 1", type number}, {"Product 2", type number}, {"Product 3", type number}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Customer", "Month"}, "Attribute", "Value"),
    #"Pivoted Column" = Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[Month]), "Month", "Value", List.Sum)
in
    #"Pivoted Column"

推荐阅读