答案1
在 Power Query 中,您将对 TAG 列进行透视,并以 VALUE 列作为值和no aggregation
。
但是,如果您在数据集上执行此操作,则会返回错误值,因为同一个 Pivot 值有多个条目。
可以通过添加索引列并按顺序对每个单独的标签条目进行编号来避免这种情况。
为了做到这一点,并且允许标签不总是按照相同的顺序排列,您可以
- 按标签分组
- 为每个子表添加索引列
- 展开表格
- 以 TAG 为中心,无聚合
- 删除索引列
在下面的代码中,检查注释和应用步骤以便更好地理解。
如果此方法在您的数据集上太慢,则可以发布一个可能执行得更快的自定义函数。
let
//replace next lines to reflect your actual data source
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"TAG", type text}, {"VALUE", type text}}),
//Group By Tage
#"Grouped Rows" = Table.Group(#"Changed Type", {"TAG"}, {
//add an Index column to each subtable
{"with Index", each Table.AddIndexColumn(_,"Index",0,1), type table [TAG=nullable text, VALUE=nullable text, Index=Int64.Type]}}),
//Expand the sub tables
#"Expanded with Index" = Table.ExpandTableColumn(#"Grouped Rows", "with Index", {"VALUE", "Index"}),
//Pivot with no aggregation
#"Pivoted Column" = Table.Pivot(#"Expanded with Index", List.Distinct(#"Expanded with Index"[TAG]), "TAG", "VALUE"),
//Remove the Index column
#"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
#"Removed Columns"
编辑以显示用于执行非聚合数据透视的自定义函数
自定义函数
粘贴到空白查询中并按照代码注释重命名
//credit: Cam Wallace https://www.dingbatdata.com/2018/03/08/non-aggregate-pivot-with-multiple-rows-in-powerquery/
//Rename: fnPivotAll
(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"
主要代码
let
//replace next lines to reflect your actual data source
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"TAG", type text}, {"VALUE", type text}}),
//call custom pivot function
pivot = fnPivotAll(#"Changed Type","TAG","VALUE")
in
pivot