PowerQuery: How can I concatenate grouped values?

天大地大妈咪最大 提交于 2019-11-26 09:29:32

问题


If I have the following table (shown in the image below), how can I write a grouped query that would concatenate the grouped results?

For this example, I\'d want to group by the LetterColumn and concatenate the NumberColumn

So the desired results would be:


回答1:


If your table is Source, and if NumberColumn has the number type, then this will work:

= Table.Group(Source, {"LetterColumn"}, {{"Column", each Text.Combine(List.Transform(_[NumberColumn], (x) => Number.ToText(x)), ","), type text}})

Table.Group does a group by operation, which creates a table made up of all of the rows with the same value in LetterColumn. _[NumberColumn] gives a list of the values in the NumberColumn column in this new table. The List.Transform part turns the numbers into text values, and Text.Combine joins those numbers together, with a comma separating each value.

If you need the surrounding quotes as well, you can do this:

= Table.Group(Source, {"LetterColumn"}, {{"Column", each """" & Text.Combine(List.Transform(_[NumberColumn], (x) => Number.ToText(x)), ",") & """", type text}})

"""" represents the " character, and & combines two text values.




回答2:


You can use the GUI to do it this way:

  1. Select your LetterColumn and then Transform / GroupBy:

  2. Select Add Column / Custom Column:

  3. Click opposing arrows at top right of new AllData column to Extract Values from new AllData column:

  4. Remove AllData column.



来源:https://stackoverflow.com/questions/44058355/powerquery-how-can-i-concatenate-grouped-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!