我有一个 CSV 文件,其中包含有关某些生产批处理的信息。当加载到 Excels PowerQuery 编辑器中时,表格如下所示:
批处理日期 RawMaterial1 RawMaterial2 RawMaterial3 Amount1 Amount2 Amount3
123 01.01.2020 铁 Chrome 镍 70 19 11
234 01.02.2020 铁 Chrome 镍 72 17 9
为了使该表更具可读性,我正在寻找一种方法将其部分转置以将其转换为如下格式:
批处理日期 RawMaterials 数量
123 01.01.2020 铁 70
Chrome 19
倪11
234 01.02.2020 铁 72
Chrome 17
倪11
有没有办法单独使用 PowerQueryM 来实现这一点?
最佳答案
这似乎有效
取消透视除前两列之外的所有列
复制数据列
将列类型更改为数字以强制在非数字列上出错
将所有错误更改为可识别的内容,例如 999999999999
根据它过滤成两个表,并为每个表添加一个索引
将两个表合并在一起
添加新列,使用索引查看 Batch 是否与前一行相同以消除重复
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Date", "Batch"}, "Attribute", "Value"),
#"Duplicated Column" = Table.DuplicateColumn(#"Unpivoted Other Columns", "Value", "Value - Copy"),
#"Changed Type" = Table.TransformColumnTypes(#"Duplicated Column",{{"Value - Copy", type number}}),
#"Replaced Errors" = Table.ReplaceErrorValues(#"Changed Type", {{"Value - Copy", 999999999999999}}),
#"Filtered Rows" = Table.SelectRows(#"Replaced Errors", each ([#"Value - Copy"] = 999999999999999)),
#"Filtered Rows2" = Table.SelectRows(#"Replaced Errors", each ([#"Value - Copy"] <> 999999999999999)),
Index1 = Table.AddIndexColumn(#"Filtered Rows", "Index", 0, 1),
Index2 = Table.AddIndexColumn(#"Filtered Rows2", "Index", 0, 1),
#"Merged Queries" = Table.NestedJoin(Index2,{"Index"},Index1,{"Index"},"Index3",JoinKind.LeftOuter),
#"Expanded Index3" = Table.ExpandTableColumn(#"Merged Queries", "Index3", {"Value"}, {"Value.1"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Index3",{"Attribute", "Value - Copy"}),
#"Added Custom" = Table.AddColumn(#"Removed Columns", "Batch.1", each if [Index] = 0 then [Batch] else if #"Removed Columns"{[Index]-1}[Batch] = [Batch] then null else [Batch]),
#"Added Custom2" = Table.AddColumn(#"Added Custom", "Date.1", each if [Index] = 0 then [Date] else if #"Removed Columns"{[Index]-1}[Batch] = [Batch] then null else [Date]),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom2",{"Batch", "Date", "Index"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns1",{"Batch.1", "Date.1", "Value.1", "Value"})
in #"Reordered Columns"
关于excel - 部分转置表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61654618/