[英]Sorting an Excel Pivot Table by a calculated field
我有以下 Excel 电子表格:
A B C D
1 Product Category Sale Margin
2 Product A Apparel 500 45%
3 Product A Apparel 400 30%
4 Product A Shoes 600 55%
5 Product B Apparel 300 20%
6 Product C Beauty 100 40%
7 Product C Shoes 200 65%
8 Product D Apparel 450 25%
9 Product D Beauty 700 50%
10 Product D Beauty 250 35%
基于这些数据,我创建了一个数据透视表。 在此数据透视表中,我添加了以下计算字段以获得利润:
Profit = Sale * Margin
这导致以下数据透视表:
Sum of Profit
Product A 1.950
Product B 60
Product C 315
Product D 1.540
现在,我想将此数据透视表降序排序,如下所示:
Sum of Profit
Product A 1.950
Product D 1.540
Product C 315
Product B 60
似乎对于计算字段,数据透视表工具中的排序选项不可用。
您对我如何实现这种排序有任何其他想法吗?
注意:我知道我可以在源数据的E 列中添加利润计算。 但是,与上面的简化示例相比,我的原始文件稍微复杂一些,因此计算字段是不可避免的。
右键单击计算字段中的单元格 --> 排序 --> 从大到小排序。
或者您可以尝试以下代码对计算字段进行排序。
Sub SortCalculatedField()
Dim ws As Worksheet
Dim pt As PivotTable
Set ws = Sheets("Sheet1") 'This is the sheet which contains Pivot Table
Set pt = ws.PivotTables(1)
pt.PivotFields("Product").AutoSort xlDescending, "Sum of Profit", pt.PivotColumnAxis.PivotLines(1), 1
End Sub
如果行部分(分类数据)中有多个字段,则数据透视表似乎无法以通常的方式进行排序。 我已经在多个数据透视表布局中尝试过它,它甚至在“传统”数据透视表布局中也是如此。 如果有必要拥有多个分类元素,我相信串联将是一种解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.