根据 Power Query 中的一列更改 table 的结构
Changing structure of table depending on one colulmn in PowerQuery
我最近正在为 table 的 Power Query 转换而苦苦挣扎。我一直在尝试来自 Internet 的不同提示,由于旋转,我已经接近了,但由于我缺乏与 PowerQuery 工具相关的知识,我不得不放弃。
所以我有一个 table,其中第一列说明给定操作的计划日期和实际日期。它看起来像这样:
当前table的结构
我想重组 table 以先删除 table 并添加到 header“行动 1 计划”、“行动 1 实际” 等等等。所以我的愿望是完成以下情况:
需要table的结构
我一直在尝试不同的事情,但我仍然无法做到这一点。我希望有人能给我一些关于如何完成这项工作的提示。如果有任何建议,我将不胜感激。
经常遇到此类问题,您必须先 unpivot 和 group 才能获得所需的 pivot 结果。
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
数据
结果
编辑
我注意到结果中的列名顺序 table 不是您在示例中显示的顺序。我在代码中添加了几行以使顺序正确。
试试这个代码:
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
新代码的结果
我最近正在为 table 的 Power Query 转换而苦苦挣扎。我一直在尝试来自 Internet 的不同提示,由于旋转,我已经接近了,但由于我缺乏与 PowerQuery 工具相关的知识,我不得不放弃。 所以我有一个 table,其中第一列说明给定操作的计划日期和实际日期。它看起来像这样:
当前table的结构
我想重组 table 以先删除 table 并添加到 header“行动 1 计划”、“行动 1 实际” 等等等。所以我的愿望是完成以下情况:
需要table的结构
我一直在尝试不同的事情,但我仍然无法做到这一点。我希望有人能给我一些关于如何完成这项工作的提示。如果有任何建议,我将不胜感激。
经常遇到此类问题,您必须先 unpivot 和 group 才能获得所需的 pivot 结果。
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
数据
结果
编辑 我注意到结果中的列名顺序 table 不是您在示例中显示的顺序。我在代码中添加了几行以使顺序正确。
试试这个代码:
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
新代码的结果