表格 A 列是分类,后面是 2N 个 key-value 列
A | B | C | D | E | F | G | |
1 | Country | Label1 | Count1 | Label2 | Count2 | Label3 | Count3 |
2 | US | A | 10 | B | 9 | C | 8 |
3 | US | D | 9 | C | 8 | A | 7 |
4 | US | C | 8 | D | 7 | B | 6 |
5 | US | A | 7 | C | 6 | B | 5 |
6 | CA | A | 10 | B | 9 | C | 8 |
7 | CA | D | 9 | C | 8 | A | 7 |
8 | CA | C | 8 | D | 7 | B | 6 |
9 | IN | A | 10 | C | 9 | B | 8 |
10 | IN | D | 9 | A | 8 | B | 7 |
11 | IN | A | 8 | D | 7 | B | 6 |
需要对分类、key 分组,对 value 求和,结果是 3 列。注意,计算结果的分组列要保持原始顺序。
A | B | ||
1 | Country | Label | Total |
2 | US | A | 24 |
3 | US | B | 20 |
4 | US | C | 30 |
5 | US | D | 16 |
6 | CA | A | 17 |
7 | CA | B | 15 |
8 | CA | C | 24 |
9 | CA | D | 16 |
10 | IN | A | 26 |
11 | IN | C | 9 |
12 | IN | B | 21 |
使用 SPL XLL,输入公式并下拉:
=spl("=E(?).groupc@r(Country;;Label,Count).groups@u(Country,Label;sum(Count):Total)",A1:G11)
函数 E 按表格形式读入数据。groupc@r 以相同列数为组进行列转行,groups 分组汇总。