经常遇到此类问题,您必须先unpivot 和group,然后才能获得所需的数据透视结果。
M 码
let
Source = Excel.CurrentWorkbook(){[Name="Table6"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Type of Date", "Project"}, "Attribute", "Value"),
//create the New column headers by merging
#"Inserted Merged Column" = Table.AddColumn(#"Unpivoted Other Columns", "Op Type", each Text.Combine({[Attribute], [Type of Date]}, " "), type text),
#"Removed Columns" = Table.RemoveColumns(#"Inserted Merged Column",{"Type of Date", "Attribute"}),
//Group by "Project" and Pivot each grouped table
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Project"}, {
{"Pivot", each Table.Pivot(_, List.Distinct(_[#"Op Type"]), "Op Type", "Value")}
}),
//remove unneeded columns and expand the Pivoted column
#"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"Project"}),
#"Expanded Pivot" = Table.ExpandTableColumn(#"Removed Columns1", "Pivot",
{"Project", "Operation 1 Planned", "Operation 2 Planned", "Operation 3 Planned", "Operation 4 Planned", "Operation 1 Actual", "Operation 2 Actual", "Operation 3 Actual", "Operation 4 Actual"}, {"Project", "Operation 1 Planned", "Operation 2 Planned", "Operation 3 Planned", "Operation 4 Planned", "Operation 1 Actual", "Operation 2 Actual", "Operation 3 Actual", "Operation 4 Actual"}),
//type the date columns
dateCols = List.RemoveFirstN(Table.ColumnNames(#"Expanded Pivot"),1),
dateTypes = List.Zip({dateCols,List.Repeat({type date}, List.Count(dateCols))}),
typed = Table.TransformColumnTypes(#"Expanded Pivot",dateTypes)
in
typed
数据
结果
编辑
我注意到结果表中的列名顺序不是您在示例中显示的。我在代码中添加了几行以使顺序正确。
试试这个代码:
let
Source = Excel.CurrentWorkbook(){[Name="Table6"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Type of Date", "Project"}, "Attribute", "Value"),
//create the New column headers by merging
#"Inserted Merged Column" = Table.AddColumn(#"Unpivoted Other Columns", "Op Type", each Text.Combine({[Attribute], [Type of Date]}, " "), type text),
#"Removed Columns" = Table.RemoveColumns(#"Inserted Merged Column",{"Type of Date", "Attribute"}),
//Group by "Project" and Pivot each grouped table
//create list of the Pivot Column Names in the desired order
//Extract a unique list of the names, then sort them => {op1Act, op1Plan, op2Act, op2Plan, ...}
//Split the list (List.Alternate) taking every other
// starting with either the first or second entry => {op1Act,op2Act,...}, and {op1Plan,op2Plan...}
//Zip those two lists together with the second list first; then Combine them into a single list
pivotColNames = List.Combine(
List.Zip({
List.Alternate(List.Sort(List.Distinct(#"Removed Columns"[Op Type])),1,1),
List.Alternate(List.Sort(List.Distinct(#"Removed Columns"[Op Type])),1,1,1)
})),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Project"}, {
{"Pivot", each Table.Pivot(_, pivotColNames, "Op Type", "Value")}
}),
//remove unneeded columns and expand the Pivoted column
#"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"Project"}),
//create column name list for the expanded pivot table, in the desired order
x = List.Combine({{"Project"},pivotColNames}),
#"Expanded Pivot" = Table.ExpandTableColumn(#"Removed Columns1", "Pivot",
x,x),
//type the date columns
dateCols = List.RemoveFirstN(Table.ColumnNames(#"Expanded Pivot"),1),
dateTypes = List.Zip({dateCols,List.Repeat({type date}, List.Count(dateCols))}),
typed = Table.TransformColumnTypes(#"Expanded Pivot",dateTypes)
in
typed
新代码的结果