Skip to content

Commit 9b5a68a

Browse files
authored
Merge pull request #1841 from syncfusion-content/988313-PivotCalculatedField
988313-FAQ on can PivotTables share calculated field names
2 parents e20723f + 9fb016c commit 9b5a68a

File tree

1 file changed

+204
-0
lines changed

1 file changed

+204
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
---
2+
title: Calculated field naming across PivotTables | Syncfusion
3+
description: This page explains whether multiple PivotTables can use calculated fields with the same name in Syncfusion .NET Excel library (XlsIO).
4+
platform: document-processing
5+
control: XlsIO
6+
documentation: UG
7+
---
8+
9+
# Can PivotTables have the same name for multiple calculated fields?
10+
11+
In **Microsoft Excel**, calculated fields are scoped to the pivot cache, not to individual PivotTables.
12+
13+
This means:
14+
15+
* **If multiple PivotTables share the same cache**
16+
* Calculated fields with the same name cannot be defined. Excel reuses the existing field from the cache, and its formula cannot be redefined independently.
17+
* Calculated fields with different names can be defined, and they will be available to all PivotTables that share that cache.
18+
19+
* **If PivotTables use separate caches (even if the source data is identical)**
20+
* Calculated fields with the same name or with different names can be defined. Each cache maintains its own calculated field collection, so there is no conflict.
21+
22+
**Syncfusion XlsIO** follows the same cache scoped behavior:
23+
* With a shared cache, calculated field names must be unique.
24+
* With separate caches, calculated field names can be identical or different.
25+
26+
The following examples illustrate calculated field naming across PivotTables in C# (cross-platform and Windows-specific) and VB.NET.
27+
28+
{% tabs %}
29+
{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/FAQ/Pivot%20Tables%20Calculated%20Field%20Names/.NET/PivotTablesCalculatedFieldNames/PivotTablesCalculatedFieldNames/Program.cs,180" %}
30+
using (ExcelEngine excelEngine = new ExcelEngine())
31+
{
32+
IApplication application = excelEngine.Excel;
33+
application.DefaultVersion = ExcelVersion.Xlsx;
34+
IWorkbook workbook = application.Workbooks.Create(3);
35+
IWorksheet dataSheet = workbook.Worksheets[0];
36+
IWorksheet pivotSheet1 = workbook.Worksheets[1];
37+
IWorksheet pivotSheet2 = workbook.Worksheets[2];
38+
39+
//Add sample data
40+
dataSheet.Range["A1"].Text = "Product";
41+
dataSheet.Range["B1"].Text = "Sales";
42+
dataSheet.Range["C1"].Text = "Cost";
43+
44+
dataSheet.Range["A2"].Text = "Laptop";
45+
dataSheet.Range["B2"].Number = 5000;
46+
dataSheet.Range["C2"].Number = 3000;
47+
48+
dataSheet.Range["A3"].Text = "Tablet";
49+
dataSheet.Range["B3"].Number = 3000;
50+
dataSheet.Range["C3"].Number = 2000;
51+
52+
dataSheet.Range["A4"].Text = "Phone";
53+
dataSheet.Range["B4"].Number = 4000;
54+
dataSheet.Range["C4"].Number = 2500;
55+
56+
//CASE 1: Shared pivot cache - calculated field names must be unique
57+
IPivotCache sharedCache = workbook.PivotCaches.Add(dataSheet["A1:C4"]);
58+
59+
IPivotTable pivot1 = pivotSheet1.PivotTables.Add("Pivot1", pivotSheet1["A1"], sharedCache);
60+
pivot1.Fields[0].Axis = PivotAxisTypes.Row;
61+
pivot1.DataFields.Add(pivot1.Fields[1], "Total Sales", PivotSubtotalTypes.Sum);
62+
pivot1.CalculatedFields.Add("Profit", "Sales - Cost");
63+
64+
IPivotTable pivot2 = pivotSheet1.PivotTables.Add("Pivot2", pivotSheet1["F1"], sharedCache);
65+
pivot2.Fields[0].Axis = PivotAxisTypes.Row;
66+
pivot2.DataFields.Add(pivot2.Fields[2], "Total Cost", PivotSubtotalTypes.Sum);
67+
pivot2.CalculatedFields.Add("Margin", "Sales - Cost");
68+
69+
//CASE 2: Separate pivot caches - identical or different calculated field names are allowed
70+
IPivotTable pivot3 = pivotSheet2.PivotTables.Add("Pivot3", pivotSheet2["A1"],
71+
workbook.PivotCaches.Add(dataSheet["A1:C4"]));
72+
pivot3.Fields[0].Axis = PivotAxisTypes.Row;
73+
pivot3.DataFields.Add(pivot3.Fields[1], "Total Sales", PivotSubtotalTypes.Sum);
74+
pivot3.CalculatedFields.Add("Profit", "Sales - Cost");
75+
76+
IPivotTable pivot4 = pivotSheet2.PivotTables.Add("Pivot4", pivotSheet2["F1"],
77+
workbook.PivotCaches.Add(dataSheet["A1:C4"]));
78+
pivot4.Fields[0].Axis = PivotAxisTypes.Row;
79+
pivot4.DataFields.Add(pivot4.Fields[2], "Total Cost", PivotSubtotalTypes.Sum);
80+
pivot4.CalculatedFields.Add("Profit", "Sales - Cost");
81+
82+
//Saving the workbook
83+
workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx"));
84+
}
85+
{% endhighlight %}
86+
87+
{% highlight c# tabtitle="C# [Windows-specific]" %}
88+
using (ExcelEngine excelEngine = new ExcelEngine())
89+
{
90+
IApplication application = excelEngine.Excel;
91+
application.DefaultVersion = ExcelVersion.Xlsx;
92+
IWorkbook workbook = application.Workbooks.Create(3);
93+
IWorksheet dataSheet = workbook.Worksheets[0];
94+
IWorksheet pivotSheet1 = workbook.Worksheets[1];
95+
IWorksheet pivotSheet2 = workbook.Worksheets[2];
96+
97+
//Add sample data
98+
dataSheet.Range["A1"].Text = "Product";
99+
dataSheet.Range["B1"].Text = "Sales";
100+
dataSheet.Range["C1"].Text = "Cost";
101+
102+
dataSheet.Range["A2"].Text = "Laptop";
103+
dataSheet.Range["B2"].Number = 5000;
104+
dataSheet.Range["C2"].Number = 3000;
105+
106+
dataSheet.Range["A3"].Text = "Tablet";
107+
dataSheet.Range["B3"].Number = 3000;
108+
dataSheet.Range["C3"].Number = 2000;
109+
110+
dataSheet.Range["A4"].Text = "Phone";
111+
dataSheet.Range["B4"].Number = 4000;
112+
dataSheet.Range["C4"].Number = 2500;
113+
114+
//CASE 1: Shared pivot cache - calculated field names must be unique
115+
IPivotCache sharedCache = workbook.PivotCaches.Add(dataSheet["A1:C4"]);
116+
117+
IPivotTable pivot1 = pivotSheet1.PivotTables.Add("Pivot1", pivotSheet1["A1"], sharedCache);
118+
pivot1.Fields[0].Axis = PivotAxisTypes.Row;
119+
pivot1.DataFields.Add(pivot1.Fields[1], "Total Sales", PivotSubtotalTypes.Sum);
120+
pivot1.CalculatedFields.Add("Profit", "Sales - Cost");
121+
122+
IPivotTable pivot2 = pivotSheet1.PivotTables.Add("Pivot2", pivotSheet1["F1"], sharedCache);
123+
pivot2.Fields[0].Axis = PivotAxisTypes.Row;
124+
pivot2.DataFields.Add(pivot2.Fields[2], "Total Cost", PivotSubtotalTypes.Sum);
125+
pivot2.CalculatedFields.Add("Margin", "Sales - Cost");
126+
127+
//CASE 2: Separate pivot caches - identical or different calculated field names are allowed
128+
IPivotTable pivot3 = pivotSheet2.PivotTables.Add("Pivot3", pivotSheet2["A1"],
129+
workbook.PivotCaches.Add(dataSheet["A1:C4"]));
130+
pivot3.Fields[0].Axis = PivotAxisTypes.Row;
131+
pivot3.DataFields.Add(pivot3.Fields[1], "Total Sales", PivotSubtotalTypes.Sum);
132+
pivot3.CalculatedFields.Add("Profit", "Sales - Cost");
133+
134+
IPivotTable pivot4 = pivotSheet2.PivotTables.Add("Pivot4", pivotSheet2["F1"],
135+
workbook.PivotCaches.Add(dataSheet["A1:C4"]));
136+
pivot4.Fields[0].Axis = PivotAxisTypes.Row;
137+
pivot4.DataFields.Add(pivot4.Fields[2], "Total Cost", PivotSubtotalTypes.Sum);
138+
pivot4.CalculatedFields.Add("Profit", "Sales - Cost");
139+
140+
//Saving the workbook
141+
workbook.SaveAs("Output.xlsx");
142+
}
143+
{% endhighlight %}
144+
145+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
146+
Using excelEngine As New ExcelEngine()
147+
Dim application As IApplication = excelEngine.Excel
148+
application.DefaultVersion = ExcelVersion.Xlsx
149+
150+
Dim workbook As IWorkbook = application.Workbooks.Create(3)
151+
Dim dataSheet As IWorksheet = workbook.Worksheets(0)
152+
Dim pivotSheet1 As IWorksheet = workbook.Worksheets(1)
153+
Dim pivotSheet2 As IWorksheet = workbook.Worksheets(2)
154+
155+
' Add sample data
156+
dataSheet.Range("A1").Text = "Product"
157+
dataSheet.Range("B1").Text = "Sales"
158+
dataSheet.Range("C1").Text = "Cost"
159+
160+
dataSheet.Range("A2").Text = "Laptop"
161+
dataSheet.Range("B2").Number = 5000
162+
dataSheet.Range("C2").Number = 3000
163+
164+
dataSheet.Range("A3").Text = "Tablet"
165+
dataSheet.Range("B3").Number = 3000
166+
dataSheet.Range("C3").Number = 2000
167+
168+
dataSheet.Range("A4").Text = "Phone"
169+
dataSheet.Range("B4").Number = 4000
170+
dataSheet.Range("C4").Number = 2500
171+
172+
'CASE 1: Shared pivot cache - calculated field names must be unique
173+
Dim sharedCache As IPivotCache = workbook.PivotCaches.Add(dataSheet.Range("A1:C4"))
174+
175+
Dim pivot1 As IPivotTable = pivotSheet1.PivotTables.Add("Pivot1", pivotSheet1.Range("A1"), sharedCache)
176+
pivot1.Fields(0).Axis = PivotAxisTypes.Row
177+
pivot1.DataFields.Add(pivot1.Fields(1), "Total Sales", PivotSubtotalTypes.Sum)
178+
pivot1.CalculatedFields.Add("Profit", "Sales - Cost")
179+
180+
Dim pivot2 As IPivotTable = pivotSheet1.PivotTables.Add("Pivot2", pivotSheet1.Range("F1"), sharedCache)
181+
pivot2.Fields(0).Axis = PivotAxisTypes.Row
182+
pivot2.DataFields.Add(pivot2.Fields(2), "Total Cost", PivotSubtotalTypes.Sum)
183+
pivot2.CalculatedFields.Add("Margin", "Sales - Cost")
184+
185+
'CASE 2: Separate pivot caches - identical or different calculated field names are allowed
186+
Dim pivot3 As IPivotTable = pivotSheet2.PivotTables.Add("Pivot3", pivotSheet2.Range("A1"),
187+
workbook.PivotCaches.Add(dataSheet.Range("A1:C4")))
188+
pivot3.Fields(0).Axis = PivotAxisTypes.Row
189+
pivot3.DataFields.Add(pivot3.Fields(1), "Total Sales", PivotSubtotalTypes.Sum)
190+
pivot3.CalculatedFields.Add("Profit", "Sales - Cost")
191+
192+
Dim pivot4 As IPivotTable = pivotSheet2.PivotTables.Add("Pivot4", pivotSheet2.Range("F1"),
193+
workbook.PivotCaches.Add(dataSheet.Range("A1:C4")))
194+
pivot4.Fields(0).Axis = PivotAxisTypes.Row
195+
pivot4.DataFields.Add(pivot4.Fields(2), "Total Cost", PivotSubtotalTypes.Sum)
196+
pivot4.CalculatedFields.Add("Profit", "Sales - Cost")
197+
198+
'Saving the workbook
199+
workbook.SaveAs("Output.xlsx")
200+
End Using
201+
{% endhighlight %}
202+
{% endtabs %}
203+
204+
A complete working example to demonstrate calculated field naming across PivotTables using C# is present on <a href="https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/FAQ/Pivot%20Tables%20Calculated%20Field%20Names/.NET/PivotTablesCalculatedFieldNames">this GitHub page</a>.

0 commit comments

Comments
 (0)