我有一张表格,格式如下:
注意。下面的示例只是表格的汇总版本,实际版本包含 2021 年和 2022 年每个月的类别和值,然后还有更多类别和 2021 年和 2022 年的值。
A列 | B列 | C 列 | D列 | E 列 | ... | N 列 |
---|---|---|---|---|---|---|
Col1 | Col2 | Col3 | 类别 1 | Col4 | ... | 列 |
父母/孩子姓名 | ID | 父母/孩子 | 2021 年 1 月 | 2021 年 2 月 | ... | 2022 年 12 月 |
杰森 | 1 | 家长 | 1000 | 2000 | ... | 1500 |
提米 | 2 | 孩子 | 600 | 1000 | ... | 600 |
莎拉 | 3 | 孩子 | 400 | 1000 | ... | 900 |
我正在尝试进行一些转换以实现以下结果:
父母/孩子姓名 | ID | 类别 | 父母/孩子 | 年 | 月 | 价值 |
---|---|---|---|---|---|---|
杰森 | 1 | 类别 1 | 家长 | 2021 | 一月 | 1000 |
提米 | 2 | 类别 1 | 孩子 | 2021 | 一月 | 600 |
莎拉 | 3 | 类别 1 | 孩子 | 2021 | 一月 | 400 |
杰森 | 1 | 类别 1 | 家长 | 2021 | 二月 | 2000 |
提米 | 2 | 类别 1 | 孩子 | 2021 | 二月 | 1000 |
莎拉 | 3 | 类别 1 | 孩子 | 2021 | 二月 | 1000 |
... | ... | ... | ... | ... | ... | ... |
N 人 | 身份证号码 | N类 | 父母/孩子 N | N年 | N月 | 值 N |
我尝试过旋转、移调等方法,但我无法达到我想要的结果。我对使用 power query/M 代码还很陌生,但我似乎找不到相关的东西来帮助我实现我想要的结果。
这应该做你想做的。 “技巧”是取消透视“日期”列
注意:由于您只显示了一个类别,因此不清楚它会如何传播,所以我忽略了它。您将需要修改代码以更好地说明更改。我怀疑除了下面的代码外,您还需要为每个类别做一个
Table.Group
,然后将类别输出为单个列。
let
//change next line to reflect actual data source
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
//Remove top row and promote next row to headers
#"Removed Top Rows" = Table.Skip(Source,1),
#"Promoted Headers" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true]),
//Unpivot the "Date" columns
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers",
{"Parent/ Child Name", "ID", "Parent/ Child"}, "Attribute", "Value"),
//Set data type of Date columns to datetime
#"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Attribute", type datetime}}),
//Sort ascending by Date and then by ID
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Attribute", Order.Ascending}, {"ID", Order.Ascending}}),
//Add Custom column for Year and Month
#"Added Custom" = Table.AddColumn(#"Sorted Rows", "Year", each Date.Year([Attribute]), Int64.Type),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Month", each DateTime.ToText([Attribute], "MMM"), type text),
//Remove unneeded columns and set desired order of remaining columns
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Attribute"}),
#"Reordered Columns"= Table.ReorderColumns(#"Removed Columns",
{"Parent/ Child Name", "ID", "Parent/ Child", "Year", "Month", "Value"}),
//Set all data types
#"Set Types" = Table.TransformColumnTypes(#"Reordered Columns",
List.Zip({Table.ColumnNames(#"Reordered Columns"), {type text, Int64.Type, type text, Int64.Type, type text, Int64.Type}}))
in
#"Set Types"
在此向您发送包含[类别]列的M代码。
//在下面的第一个源语句中用替换更改数据源
let
Source = Excel.Workbook(File.Contents("C:\Users\zhenger\Desktop\powerquery sample.xlsx"), null, true),
table_Sheet = Source{[Item="table",Kind="Sheet"]}[Data],
#"Transposed Table" = Table.Transpose(table_Sheet),
#"Filled Down" = Table.FillDown(#"Transposed Table",{"Column1"}),
#"Added Custom" = Table.AddColumn(#"Filled Down", "Custom", each "-"),
#"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Column1", "Custom", "Column2", "Column3", "Column4", "Column5"}),
#"Inserted Merged Column" = Table.AddColumn(#"Reordered Columns", "Merged", each Text.Combine({[Column1], [Custom], Text.From([Column2], "zh-CN")}, ""), type text),
#"Removed Columns" = Table.RemoveColumns(#"Inserted Merged Column",{"Column1", "Custom", "Column2"}),
#"Reordered Columns1" = Table.ReorderColumns(#"Removed Columns",{"Merged", "Column3", "Column4", "Column5"}),
#"Transposed Table1" = Table.Transpose(#"Reordered Columns1"),
#"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table1", [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"-Parent/ Child Name", type text}, {"-ID", Int64.Type}, {"-Parent/ Child", type text}, {"Category 1-2021/1/1", Int64.Type}, {"Category 1-2021/2/1", Int64.Type}, {"Category 1-2021/3/1", Int64.Type}, {"Category 1-2021/4/1", Int64.Type}, {"Category 1-2021/5/1", Int64.Type}, {"Category 1-2021/6/1", Int64.Type}, {"Category 1-2021/7/1", Int64.Type}, {"Category 1-2021/8/1", Int64.Type}, {"Category 1-2021/9/1", Int64.Type}, {"Category 1-2021/10/1", Int64.Type}, {"Category 1-2021/11/1", Int64.Type}, {"Category 1-2021/12/1", Int64.Type}, {"Category 1-2022/1/1", Int64.Type}, {"Category 1-2022/2/1", Int64.Type}, {"Category 1-2022/3/1", Int64.Type}, {"Category 1-2022/4/1", Int64.Type}, {"Category 1-2022/5/1", Int64.Type}, {"Category 1-2022/6/1", Int64.Type}, {"Category 1-2022/7/1", Int64.Type}, {"Category 1-2022/8/1", Int64.Type}, {"Category 1-2022/9/1", Int64.Type}, {"Category 1-2022/10/1", Int64.Type}, {"Category 1-2022/11/1", Int64.Type}, {"Category 1-2022/12/1", Int64.Type}, {"Category 2-2021/1/1", Int64.Type}, {"Category 2-2021/2/1", Int64.Type}, {"Category 2-2021/3/1", Int64.Type}, {"Category 2-2021/4/1", Int64.Type}, {"Category 2-2021/5/1", Int64.Type}, {"Category 2-2021/6/1", Int64.Type}, {"Category 2-2021/7/1", Int64.Type}, {"Category 2-2021/8/1", Int64.Type}, {"Category 2-2021/9/1", Int64.Type}, {"Category 2-2021/10/1", Int64.Type}, {"Category 2-2021/11/1", Int64.Type}, {"Category 2-2021/12/1", Int64.Type}, {"Category 2-2022/1/1", Int64.Type}, {"Category 2-2022/2/1", Int64.Type}, {"Category 2-2022/3/1", Int64.Type}, {"Category 2-2022/4/1", Int64.Type}, {"Category 2-2022/5/1", Int64.Type}, {"Category 2-2022/6/1", Int64.Type}, {"Category 2-2022/7/1", Int64.Type}, {"Category 2-2022/8/1", Int64.Type}, {"Category 2-2022/9/1", Int64.Type}, {"Category 2-2022/10/1", Int64.Type}, {"Category 2-2022/11/1", Int64.Type}, {"Category 2-2022/12/1", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"-Parent/ Child Name", "-ID", "-Parent/ Child"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByEachDelimiter({"-"}, QuoteStyle.Csv, false), {"Attribute.1", "Attribute.2"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", type text}, {"Attribute.2", type date}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Attribute.1", "Category"}, {"Attribute.2", "date"}}),
#"Inserted Year" = Table.AddColumn(#"Renamed Columns", "Year", each Date.Year([date]), Int64.Type),
#"Inserted Month" = Table.AddColumn(#"Inserted Year", "Month", each Date.Month([date]), Int64.Type),
#"Reordered Columns2" = Table.ReorderColumns(#"Inserted Month",{"-Parent/ Child Name", "-ID", "-Parent/ Child", "Category", "date", "Year", "Month", "Value"})
in
#"Reordered Columns2"