有了足够的数据,现在可以更轻松地了解您正在处理什么以及您想要什么。
- 您似乎需要重复“项目”和“经理”列。所以选择它们,然后取消透视其他列
- 然后将属性列拆分为月份和我所说的“ValueType”列(预算、实际、最新)
- 在 ValueType 列上没有聚合的数据透视
- 然后进行一些清理,对列进行排序和重新排列。
探索应用步骤并阅读 M-Code 中的 cmets 以更好地理解算法
编辑: 编辑原始代码以制作列更具动态性,并且还允许更多“类型”的值(例如:预算、实际、最后、其他)
let
Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
//Get list of monthcolumns for setting data type and Count
//if columns will only be Project, Manager, and then various Date columns, you can use the simpler function
//if not, you'll need a different Selection function
monthCols = List.Select(Table.ColumnNames(Source), each
not (_ = "Project" or _="Manager")),
typer = {{"Project", Int64.Type},{"Manager", Text.Type}} &
List.Transform(monthCols, each {_, Int64.Type}),
//How many value types and months
v = List.Transform(monthCols,each Text.Split(_," "){0}),
months = List.Distinct(v,Comparer.OrdinalIgnoreCase),
numMonths = List.Count(months),
vt=List.Transform(monthCols,each Text.Split(_," "){1}),
valueTypes= List.Distinct(vt,Comparer.OrdinalIgnoreCase),
#"Changed Type" = Table.TransformColumnTypes(Source,typer),
//Unpivot all except Project and Manager columns
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Project", "Manager"}, "Attribute", "Value"),
//Split the Attribute (budget date/type) column to separate the Month and the Type
#"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute",
Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Month", "ValueType"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Month", type text}, {"ValueType", type text}}),
trimAndProper= Table.TransformColumns(#"Changed Type1",{
{"Month", each Text.Trim(Text.Proper(_))},
{"ValueType", each Text.Trim(Text.Proper(_))}}),
//Add Index of Month Number for sorting later on
monthNumber = Table.AddColumn(trimAndProper, "monthNumber",
each Date.Month(Date.FromText([Month] & " 2021"))),
//Pivot on ValueType
#"Pivoted Column" = Table.Pivot(monthNumber, List.Distinct(monthNumber[ValueType]), "ValueType", "Value"),
//Add another Index - Integer/Division for sorting
#"Added Index1" = Table.AddIndexColumn(#"Pivoted Column", "Index.1", 0, 1, Int64.Type),
#"Inserted Integer-Division" = Table.AddColumn(#"Added Index1", "Integer-Division",
each Number.IntegerDivide([Index.1], numMonths), Int64.Type),
#"Removed Columns" = Table.RemoveColumns(#"Inserted Integer-Division",{"Index.1"}),
//Sort, reorder and rename
#"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Integer-Division", Order.Ascending}, {"monthNumber", Order.Ascending}}),
#"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows",{"monthNumber", "Integer-Division"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns1",{"Project", "Month"} & valueTypes & {"Manager"})
in
#"Reordered Columns"
原始数据
结果