【问题标题】:Pivot columns with multiple instance (rows) of attribute具有多个属性实例(行)的数据透视列
【发布时间】:2021-04-22 15:42:21
【问题描述】:

我进行了广泛的搜索,但没有找到这个特定案例的答案,也无法调整其中的一些解决方案。

首先,我的数据是每个产品的一长串属性及其值,结构如下:

Structured Initial Data

请注意,有些产品的每个属性只有一个值,但是(这是我的问题)有些产品的同一属性有不同的值。

当我在 PowerQuery 中对表进行透视时,我会遇到产品具有相同属性的多个实例的错误。

我正在寻找的结果表的结构如下:

Structured Final Data

感谢您的帮助!

【问题讨论】:

  • 可以做到,但为什么呢?初始数据结构更可取
  • 数据需要采用这种格式才能导入到ERP中

标签: excel pivot-table powerquery


【解决方案1】:

看看这是否适合你

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Sorted Rows" = Table.Sort(Source,{{"Products", Order.Ascending}, {"Attributes", Order.Ascending}}),
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"Products"}, {{"data", each _, type table}}),
#"Added Index1" = Table.AddIndexColumn(#"Grouped Rows", "Index", 0, 1),
#"Expanded data" = Table.ExpandTableColumn(#"Added Index1", "data", {"Attributes", "Values"}, {"Attributes", "Values"}),  
mGroup = Table.Group(#"Expanded data" , {"Attributes","Products"}, {{"GRP", each Table.AddIndexColumn(_, "Index2", 1, 1), type table}}),
#"Expanded GRP" = Table.ExpandTableColumn(mGroup, "GRP", {"Values", "Index", "Index2"}, {"Values", "Index", "Index2"}),
#"Added Custom" = Table.AddColumn(#"Expanded GRP", "Row#", each [Index]+[Index2]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index", "Index2"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attributes]), "Attributes", "Values"),
#"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Row#"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns1",{"Products", "Each", "Pack"})
in #"Reordered Columns"

它按产品分组并添加索引。然后它对产品和属性进行分组并添加另一个索引。这两者的总和是一个唯一的行号,您可以使用它来进行透视

【讨论】:

  • 哇,效果很好,谢谢你的回答。我尝试分组,但我从未想过添加索引。谢谢!
猜你喜欢
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多