Google Spreadsheets: How do you concat strings in an aggregation function

喜你入骨 提交于 2021-02-18 22:11:25

问题


Say I have a table:

A, 1
B, 1
C, 2
D, 1
E, 2

How do I view the table grouping by the 2nd column and aggregating by the first with a comma separated concat function ie:

1, "A,B,D"
2, "C,E"

In both defining a pivot table and using the QUERY syntax, it seems that the only aggregation functions available are numerical aggregations like MIN, MAX, SUM, etc. Can I define my own aggregation function?


回答1:


You have to add a "Calculated Field" to the pivot table, and then select "Summarise by > Custom". This will make the column names in your formula refer to an array of values (instead of a single value). Then you can type a formula like:

= JOIN(", ", MyStringColumn)

More specifically, if you have the following table:

Create a pivot table by going to "Data > Pivot table", with the following configuration. Ensure "Summarize by" is set to "Custom"!




回答2:


Another option: if the data is in A2:B, then, say, in D2:

=UNIQUE(B2:B)

and then in E2:

=JOIN(",",FILTER(A$2:A,B$2:B=D2))

which is filled down as required.

There are one-formula, auto-expanding solutions, although they get quite convoluted.




回答3:


You're right, there's no easy way with pivot tables. This though, will do the trick. Inspired by this brilliant answer here.

First, have a header row and run a sort on column A to group by category.

So far, in your example, we have

   |     A     |     B
---+-----------+-----------
 1 |  CATEGORY | ATTRIBUTE
 2 |     1     |     A
 3 |     1     |     B
 4 |     1     |     D
 5 |     2     |     C
 6 |     2     |     E

In column C, let's prep the concatenated strings. Start in cell C2 with the following formula, and fill out vertically.

=IF(A2<>A1, B2, C1 & "," & B2)

...looking good...

   |     A     |     B     |     C
---+-----------+-----------+-----------
 1 |  CATEGORY | ATTRIBUTE |  STRINGS
 2 |     1     |     A     |     A
 3 |     1     |     B     |    A,B
 4 |     1     |     D     |   A,B,D
 5 |     2     |     C     |     C
 6 |     2     |     E     |    C,E

In column D, let's validate the rows we want to select in a later step, with the following formula, starting in cell D2 and filling out. Basically we are marking the final category rows that carry the full concatenated strings.

=A2<>A3

...almost there now

   |     A     |     B     |     C    |     D
---+-----------+-----------+----------+-----------
 1 |  CATEGORY | ATTRIBUTE |  STRINGS | VALIDATOR
 2 |     1     |     A     |     A    |   FALSE
 3 |     1     |     B     |    A,B   |   FALSE
 4 |     1     |     D     |   A,B,D  |   TRUE
 5 |     2     |     C     |     C    |   FALSE
 6 |     2     |     E     |    C,E   |   TRUE

Now, lets copy column C and D and paste special as values in the same place. Then add a filter on the whole table and filter out column D for the rows labeled TRUE. Now, remove the filter, delete columns B and D and row 1.

   |     A     |     B       
---+-----------+-----------
 1 |     1     |    A,B,D  
 2 |     2     |     C,E   

Done. Get ice cream. Watch Road House.



来源:https://stackoverflow.com/questions/24974106/google-spreadsheets-how-do-you-concat-strings-in-an-aggregation-function

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