Power Query – 列作为单独不相关的列表(从未透视的数据)

huangapple go评论106阅读模式
英文:

Power Query - Columns as separate unrelated list (from unpivoted data)

问题

如何将一个简单的未旋转的列表转换成如下的列表,其中各行之间没有关联,每列几乎是一个独立的列表?旋转操作不适用 - 这是否可能?

艺术 生物学 英语
Emma Fred John
Sam John
Mike

谢谢。

英文:

If I have a simple unpivoted list like this:

Names Subjects
John English
Sam Art
Fred Biology
John Biology
Emma Art
Mike Biology

How can I create a list like this where the rows are unrelated to each other and each column is almost a separate list? Pivoting doesn't work - is this possible?

Art Biology English
Emma Fred John
Sam John null
null Mike null

Thanks

答案1

得分: 5

你需要在旋转之前对数据进行分组并添加索引。

  1. ![图片描述](链接1)
  2. ![图片描述](链接2)
  3. ![图片描述](链接3)
  4. ![图片描述](链接4)
  5. ![图片描述](链接5)
  6. ![图片描述](链接6)
  7. let
  8. Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
  9. #"Changed Type" = Table.TransformColumnTypes(Source,{{"Names", type text}, {"Subjects", type text}}),
  10. #"Grouped Rows" = Table.Group(#"Changed Type", {"Subjects"}, {{"All", each _, type table [Names=nullable text, Subjects=nullable text]}}),
  11. #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([All], "Index")),
  12. #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"All"}),
  13. #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Names", "Index"}, {"Names", "Index"}),
  14. #"Pivoted Column" = Table.Pivot(#"Expanded Custom", List.Distinct(#"Expanded Custom"[Subjects]), "Subjects", "Names"),
  15. #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
  16. in
  17. #"Removed Columns1"

图片链接:
[链接1]: https://i.stack.imgur.com/S8bce.png
[链接2]: https://i.stack.imgur.com/Z1zai.png
[链接3]: https://i.stack.imgur.com/a8d2D.png
[链接4]: https://i.stack.imgur.com/TvlzZ.png
[链接5]: https://i.stack.imgur.com/C0j3L.png
[链接6]: https://i.stack.imgur.com/JrBPi.png

英文:

You need to group and add an index before pivoting.

Power Query – 列作为单独不相关的列表(从未透视的数据)

Power Query – 列作为单独不相关的列表(从未透视的数据)

Power Query – 列作为单独不相关的列表(从未透视的数据)

Power Query – 列作为单独不相关的列表(从未透视的数据)

Power Query – 列作为单独不相关的列表(从未透视的数据)

Power Query – 列作为单独不相关的列表(从未透视的数据)

  1. let
  2. Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
  3. #"Changed Type" = Table.TransformColumnTypes(Source,{{"Names", type text}, {"Subjects", type text}}),
  4. #"Grouped Rows" = Table.Group(#"Changed Type", {"Subjects"}, {{"All", each _, type table [Names=nullable text, Subjects=nullable text]}}),
  5. #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([All], "Index")),
  6. #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"All"}),
  7. #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Names", "Index"}, {"Names", "Index"}),
  8. #"Pivoted Column" = Table.Pivot(#"Expanded Custom", List.Distinct(#"Expanded Custom"[Subjects]), "Subjects", "Names"),
  9. #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
  10. in
  11. #"Removed Columns1"

答案2

得分: 3

以下是您要的代码部分的翻译:

  1. 只是作为一个练习,不进行分组:
  2. let
  3. Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
  4. Ans = let s1=Table.Column(Source, "Subjects"),
  5. s2=List.Sort(List.Distinct(s1)),
  6. n1=Table.Column(Source, "Names"),
  7. x=List.Numbers(0,List.Count(n1)),
  8. n2=List.Transform(s2,(y)=>List.Accumulate(x,{},(s,c)=>if s1{c}=y then List.Combine({s,{n1{c}}}) else s)),
  9. m=List.Numbers(0,List.Max(List.Transform(n2,(z)=>List.Count(z)))),
  10. r1=List.Transform(m,(q)=>Record.FromList(List.Accumulate(s2,{},(s,c)=>List.Combine({s,{try n2{List.PositionOf(s2,c)}{q} otherwise null}})),s2)),
  11. t1=Table.FromRecords(r1)
  12. in t1
  13. in
  14. Ans

请注意,我已经将HTML实体编码(如")转换为原始字符。

英文:

Just as an exercise, without grouping:

  1. let
  2. Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
  3. Ans = let s1=Table.Column(Source, "Subjects"),
  4. s2=List.Sort(List.Distinct(s1)),
  5. n1=Table.Column(Source, "Names"),
  6. x=List.Numbers(0,List.Count(n1)),
  7. n2=List.Transform(s2,(y)=>List.Accumulate(x,{},(s,c)=>if s1{c}=y then List.Combine({s,{n1{c}}}) else s)),
  8. m=List.Numbers(0,List.Max(List.Transform(n2,(z)=>List.Count(z)))),
  9. r1=List.Transform(m,(q)=>Record.FromList(List.Accumulate(s2,{},(s,c)=>List.Combine({s,{try n2{List.PositionOf(s2,c)}{q} otherwise null}})),s2)),
  10. t1=Table.FromRecords(r1)
  11. in t1
  12. in
  13. Ans

Power Query – 列作为单独不相关的列表(从未透视的数据)

huangapple
  • 本文由 发表于 2023年6月12日 19:06:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定