excel - 部分转置表

标签 excel transpose powerquery m

我有一个 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/

相关文章:

excel - 如何在 Excel 2010 中使用 VBA 获取目录中最后修改的文件

vba - 创建宏以向上移动列中的数据?

r - 避免使用 pivot longer 进行硬编码以一次对多个列进行 pivot

excel - 将 Qlikview 十六进制日期转换为普通日期

azure-table-storage - 如何在 Power Query 中高效筛选 Azure 表存储数据

python - openpyxl 趋势线和 R 平方值

vba - 带有排除过滤器的打开文件对话框 (Excel VBA)

mysql - 将 mysql 行转置为带有数据的自定义列

python - 将一列转换为行和列

excel - 在一个单元格中获取多个值并使其可以运行数据透视表