[英]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.