在 excel 中将数据从列转换为行
Transform data from columns to rows in excel
我需要将多行和多列中的数据转换为唯一的行,但我需要遵循特定的规则。当前数据格式的示例如下:
拆分应基于样式、颜色和独特的 upc,但我需要将一些字段复制到每个独特的 upc 以获得样式和颜色。我还需要证明亲子关系。
下面的示例是我希望如何显示数据。
我试过在电源查询中这样做...但完全卡住了!
提前感谢您的任何建议。
这似乎适用于粘贴到主页的 powerquery...高级编辑器..假设您的初始 table 是范围 Table1 并且类似于您提供的示例结构
取消透视,从属性中删除数字,分组并为后面的透视添加索引,透视。剩下的只是自定义列和填充
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"style", "colour"}, "Attribute", "Value"),
#"removed numbers" = Table.TransformColumns(#"Unpivoted Other Columns",{{"Attribute", each Text.Remove(_, List.Transform({48..57}, each Character.FromNumber(_))), type text}}),
#"Grouped Rows" = Table.Group(#"removed numbers", {"style", "colour", "Attribute"}, {{"data", each Table.AddIndexColumn(_, "Index", 2, 1), type table}}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Value", "Index"}, {"Value", "Index"}),
#"Added Custom" = Table.AddColumn(#"Expanded data", "Custom", each if [Attribute]="description" then 1 else [Index]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Value"),
#"Added Custom1" = Table.AddColumn(#"Pivoted Column", "parent", each if [description]=null then "child" else "parent"),
#"Filled Down" = Table.FillDown(#"Added Custom1",{"description"}),
#"Removed Columns1" = Table.RemoveColumns(#"Filled Down",{"Custom"})
in #"Removed Columns1"
在旋转之前对该列使用索引和转换的替代版本
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"style", "color", "desc"}, "Attribute", "Value"),
#"removed numbers" = Table.TransformColumns(#"Unpivoted Other Columns",{{"Attribute", each Text.Remove(_, List.Transform({48..57}, each Character.FromNumber(_))), type text}}),
#"Added Index" = Table.AddIndexColumn(#"removed numbers", "Index", 1, .5),
#"Rounded Down" = Table.TransformColumns(#"Added Index",{{"Index", Number.RoundDown, Int64.Type}}),
#"Pivoted Column" = Table.Pivot(#"Rounded Down", List.Distinct(#"Rounded Down"[Attribute]), "Attribute", "Value"),
#"Added Custom1" = Table.AddColumn(#"Pivoted Column", "parent", each "child"),
#"Add parent" = Table.Combine({Table.AddColumn(Table.Distinct(Table.SelectColumns(#"Added Custom1",{"style", "color", "desc"})), "parent", each "parent"), #"Added Custom1"}),
#"Removed Columns" = Table.RemoveColumns(#"Add parent",{"Index"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"style", "color", "upc", "size", "desc", "parent"}),
#"Sorted Rows" = Table.Sort(#"Reordered Columns",{{"style", Order.Ascending}, {"color", Order.Ascending}, {"upc", Order.Ascending}})
in #"Sorted Rows"
这是另一种 Power Query 方法,它使用自定义函数来启用在有多个项目的情况下创建没有聚合的数据透视表 Table。
检查 M 代码和应用步骤中的注释,以及自定义函数中的引用,以了解其工作原理:
进入自定义功能,select到
New Query => Blank Query
.
- 将查询从(可能)
Query1
重命名为 fnPivotAll
M代码
//Rename Table3 to your actual table name
let Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
//Unpivot all except the style and color columns
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"style", "colour"}, "Attribute", "Value"),
//remove digits from the UPC and SIZE attributes
remDigits = Table.TransformColumns(#"Unpivoted Other Columns",{
{"Attribute", each Text.Remove(_, List.Transform({48..57}, each Character.FromNumber(_))), type text}}),
//Pivot on Attribute Column
//Custom function to use when there are multiple values for the column
pivot = fnPivotAll(remDigits,"Attribute","Value"),
//Fill in the blank descriptions
#"Filled Down" = Table.FillDown(pivot,{"description"}),
//Group (by style, colour and description) to add a description row to each grouped table
#"Grouped Rows" = Table.Group(#"Filled Down", {"style", "colour", "description"}, {
{"All", each _, type table [style=text, colour=text, upc=number, size=any, description=text]},
{"addRow", each Table.InsertRows(_, 0, {[style=[style]{0}, colour=[colour]{0}, upc=null, size=null, description=[description]{0}]})}
}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"style", "colour", "description", "All"}),
//expand the grouped table
#"Expanded addRow" = Table.ExpandTableColumn(#"Removed Columns", "addRow", {"style", "colour", "upc", "size", "description"}, {"style", "colour", "upc", "size", "description"}),
//Add column for Parent or child
#"Added Custom" = Table.AddColumn(#"Expanded addRow", "Parent", each if [upc] = null then "Parent" else "Child")
in
#"Added Custom"
自定义函数
named fnPivotAll -- 重命名查询
//credit: Cam Wallace https://www.dingbatdata.com/2018/03/08/non-aggregate-pivot-with-multiple-rows-in-powerquery/
(Source as table,
ColToPivot as text,
ColForValues as text)=>
let
PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColToPivot))),
#"Pivoted Column" = Table.Pivot(Source, PivotColNames, ColToPivot, ColForValues, each _),
TableFromRecordOfLists = (rec as record, fieldnames as list) =>
let
PartialRecord = Record.SelectFields(rec,fieldnames),
RecordToList = Record.ToList(PartialRecord),
Table = Table.FromColumns(RecordToList,fieldnames)
in
Table,
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Values", each TableFromRecordOfLists(_,PivotColNames)),
#"Removed Other Columns" = Table.RemoveColumns(#"Added Custom",PivotColNames),
#"Expanded Values" = Table.ExpandTableColumn(#"Removed Other Columns", "Values", PivotColNames)
in
#"Expanded Values"
我需要将多行和多列中的数据转换为唯一的行,但我需要遵循特定的规则。当前数据格式的示例如下:
拆分应基于样式、颜色和独特的 upc,但我需要将一些字段复制到每个独特的 upc 以获得样式和颜色。我还需要证明亲子关系。
下面的示例是我希望如何显示数据。
我试过在电源查询中这样做...但完全卡住了!
提前感谢您的任何建议。
这似乎适用于粘贴到主页的 powerquery...高级编辑器..假设您的初始 table 是范围 Table1 并且类似于您提供的示例结构
取消透视,从属性中删除数字,分组并为后面的透视添加索引,透视。剩下的只是自定义列和填充
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"style", "colour"}, "Attribute", "Value"),
#"removed numbers" = Table.TransformColumns(#"Unpivoted Other Columns",{{"Attribute", each Text.Remove(_, List.Transform({48..57}, each Character.FromNumber(_))), type text}}),
#"Grouped Rows" = Table.Group(#"removed numbers", {"style", "colour", "Attribute"}, {{"data", each Table.AddIndexColumn(_, "Index", 2, 1), type table}}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Value", "Index"}, {"Value", "Index"}),
#"Added Custom" = Table.AddColumn(#"Expanded data", "Custom", each if [Attribute]="description" then 1 else [Index]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Value"),
#"Added Custom1" = Table.AddColumn(#"Pivoted Column", "parent", each if [description]=null then "child" else "parent"),
#"Filled Down" = Table.FillDown(#"Added Custom1",{"description"}),
#"Removed Columns1" = Table.RemoveColumns(#"Filled Down",{"Custom"})
in #"Removed Columns1"
在旋转之前对该列使用索引和转换的替代版本
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"style", "color", "desc"}, "Attribute", "Value"),
#"removed numbers" = Table.TransformColumns(#"Unpivoted Other Columns",{{"Attribute", each Text.Remove(_, List.Transform({48..57}, each Character.FromNumber(_))), type text}}),
#"Added Index" = Table.AddIndexColumn(#"removed numbers", "Index", 1, .5),
#"Rounded Down" = Table.TransformColumns(#"Added Index",{{"Index", Number.RoundDown, Int64.Type}}),
#"Pivoted Column" = Table.Pivot(#"Rounded Down", List.Distinct(#"Rounded Down"[Attribute]), "Attribute", "Value"),
#"Added Custom1" = Table.AddColumn(#"Pivoted Column", "parent", each "child"),
#"Add parent" = Table.Combine({Table.AddColumn(Table.Distinct(Table.SelectColumns(#"Added Custom1",{"style", "color", "desc"})), "parent", each "parent"), #"Added Custom1"}),
#"Removed Columns" = Table.RemoveColumns(#"Add parent",{"Index"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"style", "color", "upc", "size", "desc", "parent"}),
#"Sorted Rows" = Table.Sort(#"Reordered Columns",{{"style", Order.Ascending}, {"color", Order.Ascending}, {"upc", Order.Ascending}})
in #"Sorted Rows"
这是另一种 Power Query 方法,它使用自定义函数来启用在有多个项目的情况下创建没有聚合的数据透视表 Table。
检查 M 代码和应用步骤中的注释,以及自定义函数中的引用,以了解其工作原理:
进入自定义功能,select到
New Query => Blank Query
.- 将查询从(可能)
Query1
重命名为fnPivotAll
M代码
//Rename Table3 to your actual table name
let Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
//Unpivot all except the style and color columns
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"style", "colour"}, "Attribute", "Value"),
//remove digits from the UPC and SIZE attributes
remDigits = Table.TransformColumns(#"Unpivoted Other Columns",{
{"Attribute", each Text.Remove(_, List.Transform({48..57}, each Character.FromNumber(_))), type text}}),
//Pivot on Attribute Column
//Custom function to use when there are multiple values for the column
pivot = fnPivotAll(remDigits,"Attribute","Value"),
//Fill in the blank descriptions
#"Filled Down" = Table.FillDown(pivot,{"description"}),
//Group (by style, colour and description) to add a description row to each grouped table
#"Grouped Rows" = Table.Group(#"Filled Down", {"style", "colour", "description"}, {
{"All", each _, type table [style=text, colour=text, upc=number, size=any, description=text]},
{"addRow", each Table.InsertRows(_, 0, {[style=[style]{0}, colour=[colour]{0}, upc=null, size=null, description=[description]{0}]})}
}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"style", "colour", "description", "All"}),
//expand the grouped table
#"Expanded addRow" = Table.ExpandTableColumn(#"Removed Columns", "addRow", {"style", "colour", "upc", "size", "description"}, {"style", "colour", "upc", "size", "description"}),
//Add column for Parent or child
#"Added Custom" = Table.AddColumn(#"Expanded addRow", "Parent", each if [upc] = null then "Parent" else "Child")
in
#"Added Custom"
自定义函数
named fnPivotAll -- 重命名查询
//credit: Cam Wallace https://www.dingbatdata.com/2018/03/08/non-aggregate-pivot-with-multiple-rows-in-powerquery/
(Source as table,
ColToPivot as text,
ColForValues as text)=>
let
PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColToPivot))),
#"Pivoted Column" = Table.Pivot(Source, PivotColNames, ColToPivot, ColForValues, each _),
TableFromRecordOfLists = (rec as record, fieldnames as list) =>
let
PartialRecord = Record.SelectFields(rec,fieldnames),
RecordToList = Record.ToList(PartialRecord),
Table = Table.FromColumns(RecordToList,fieldnames)
in
Table,
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Values", each TableFromRecordOfLists(_,PivotColNames)),
#"Removed Other Columns" = Table.RemoveColumns(#"Added Custom",PivotColNames),
#"Expanded Values" = Table.ExpandTableColumn(#"Removed Other Columns", "Values", PivotColNames)
in
#"Expanded Values"