水平数据处理查询

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

data horizontally power query

问题

我有以下的数据集示例,我想将它们横向排列。我尝试按“sol_ped”、“pos”和“name”分组,然后使用Table.Column创建一个自定义列,但它只允许我添加一个字段,例如“prec”,而我需要两个或更多字段。
有什么办法可以做到吗?

示例数据集

sol_ped pos name prec cat
11 1 one 11.01 a
11 1 one 14.00 b
11 1 one 7.00 c
11 2 two 8.12 d

期望的结果

sol_ped pos name prec1 cat1 prec2 cat2 prec3 cat3 prec4 cat4
11 1 one 11.01 a 14.00 b 7.00 c 0.00 na
11 2 two 8.12 d 0.00 na 0.00 na 0.00 na
英文:

I have the following example of a data set, I want to have them horizontally. I tried grouping by "sol_ped", "pos", and "name" , then creating a custom column with Table.Column but it only lets me add one field for example "prec" and I need two or more fields.
Any idea how I can do?

sample dataset

sol_ped pos name prec cat
11 1 one 11.01 a
11 1 one 14.00 b
11 1 one 7.00 c
11 2 two 8.12 d

desired result

sol_ped pos name prec1 cat1 prec2 cat2 prec3 cat3 prec4 cat4
11 1 one 11.01 a 14.00 b 7.00 c 0.00 na
11 2 two 18.02 d 18.12 na na na na na

答案1

得分: 0

在Power Query中,你可以尝试以下操作:

  1. let
  2. Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
  3. #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"sol_ped", "pos", "name"}, "Attribute", "Value"),
  4. #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"sol_ped", "pos", "name"}, {
  5. {"data", each
  6. Table.AddColumn(
  7. Table.AddColumn(
  8. Table.AddIndexColumn(_, "Index", 0, 1, Int64.Type),
  9. "Count", each Number.IntegerDivide([Index], 2), Int64.Type),
  10. "MergeName", each [Attribute] & Text.From([Count] + 1))
  11. , type table }}),
  12. #"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Value", "MergeName"}, {"Value", "MergeName"}),
  13. #"Pivoted Column" = Table.Pivot(#"Expanded data", List.Distinct(#"Expanded data"[MergeName]), "MergeName", "Value"),
  14. #"Change Type" = Table.TransformColumnTypes(#"Pivoted Column", List.Transform(List.Select(Table.ColumnNames(#"Pivoted Column"), each Text.StartsWith(_, "prec")), each {_, type number})
  15. in
  16. #"Change Type"

水平数据处理查询

  1. 请注意,这是一段Power Query的代码,用于对数据进行转换和处理。
  2. <details>
  3. <summary>英文:</summary>
  4. In powerquery you can try
  5. let Source = Excel.CurrentWorkbook(){[Name=&quot;Table1&quot;]}[Content],
  6. #&quot;Unpivoted Other Columns&quot; = Table.UnpivotOtherColumns(Source, {&quot;sol_ped&quot;, &quot;pos&quot;, &quot;name&quot;}, &quot;Attribute&quot;, &quot;Value&quot;),
  7. #&quot;Grouped Rows&quot; = Table.Group(#&quot;Unpivoted Other Columns&quot;, {&quot;sol_ped&quot;, &quot;pos&quot;, &quot;name&quot;}, {
  8. {&quot;data&quot;, each
  9. Table.AddColumn(
  10. Table.AddColumn(
  11. Table.AddIndexColumn(_, &quot;Index&quot;, 0, 1, Int64.Type)
  12. , &quot;Count&quot;, each Number.IntegerDivide([Index], 2), Int64.Type)
  13. ,&quot;MergeName&quot;, each [Attribute]&amp;Text.From([Count]+1 ))
  14. , type table }}),
  15. #&quot;Expanded data&quot; = Table.ExpandTableColumn(#&quot;Grouped Rows&quot;, &quot;data&quot;, {&quot;Value&quot;, &quot;MergeName&quot;}, {&quot;Value&quot;, &quot;MergeName&quot;}),
  16. #&quot;Pivoted Column&quot; = Table.Pivot(#&quot;Expanded data&quot;, List.Distinct(#&quot;Expanded data&quot;[MergeName]), &quot;MergeName&quot;, &quot;Value&quot;),
  17. #&quot;Change Type&quot;= Table.TransformColumnTypes(#&quot;Pivoted Column&quot;,List.Transform(List.Select(Table.ColumnNames(#&quot;Pivoted Column&quot;), each Text.StartsWith(_,&quot;prec&quot;)),each {_, type number}))
  18. in #&quot;Change Type&quot;
  19. [![enter image description here][1]][1]
  20. [1]: https://i.stack.imgur.com/bTmUl.png
  21. </details>

huangapple
  • 本文由 发表于 2023年2月24日 03:21:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549415.html
匿名

发表评论

匿名网友

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

确定