From 44264e8c216ab5ac4a07e9a3bcf730b6df96e349 Mon Sep 17 00:00:00 2001 From: KarthikaSF4773 Date: Mon, 24 Nov 2025 15:51:55 +0530 Subject: [PATCH 1/3] 988313-PivotCalculatedField --- .../can-pivot-tables-share-calculate-field-names.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Document-Processing/Excel/Excel-Library/NET/faqs/can-pivot-tables-share-calculate-field-names.md diff --git a/Document-Processing/Excel/Excel-Library/NET/faqs/can-pivot-tables-share-calculate-field-names.md b/Document-Processing/Excel/Excel-Library/NET/faqs/can-pivot-tables-share-calculate-field-names.md new file mode 100644 index 000000000..eab0547b9 --- /dev/null +++ b/Document-Processing/Excel/Excel-Library/NET/faqs/can-pivot-tables-share-calculate-field-names.md @@ -0,0 +1,13 @@ +--- +title: Calculated field naming across PivotTables | Syncfusion +description: This page explains whether multiple PivotTables can use calculated fields with the same name using Syncfusion .NET Excel library (XlsIO). +platform: document-processing +control: XlsIO +documentation: UG +--- + +# Can PivotTables share calculated field names? + +According to Microsoft Excel, it is not possible to define a calculated field with the same name when multiple PivotTables refer to the same base pivot cache, and Syncfusion XlsIO follows the same behavior. + +You can either assign different names to the calculated fields when using the same pivot cache, or create each PivotTable from a unique pivot cache, which allows calculated fields with identical names. \ No newline at end of file From ef6e4c4f92cdca8cef3b4f0fdd3627cfe16bc020 Mon Sep 17 00:00:00 2001 From: KarthikaSF4773 Date: Thu, 27 Nov 2025 17:06:37 +0530 Subject: [PATCH 2/3] 988313-PivotCalculatedField --- ...ivot-tables-share-calculate-field-names.md | 197 +++++++++++++++++- 1 file changed, 194 insertions(+), 3 deletions(-) diff --git a/Document-Processing/Excel/Excel-Library/NET/faqs/can-pivot-tables-share-calculate-field-names.md b/Document-Processing/Excel/Excel-Library/NET/faqs/can-pivot-tables-share-calculate-field-names.md index eab0547b9..d143417d7 100644 --- a/Document-Processing/Excel/Excel-Library/NET/faqs/can-pivot-tables-share-calculate-field-names.md +++ b/Document-Processing/Excel/Excel-Library/NET/faqs/can-pivot-tables-share-calculate-field-names.md @@ -1,6 +1,6 @@ --- title: Calculated field naming across PivotTables | Syncfusion -description: This page explains whether multiple PivotTables can use calculated fields with the same name using Syncfusion .NET Excel library (XlsIO). +description: This page explains whether multiple PivotTables can use calculated fields with the same name in Syncfusion .NET Excel library (XlsIO). platform: document-processing control: XlsIO documentation: UG @@ -8,6 +8,197 @@ documentation: UG # Can PivotTables share calculated field names? -According to Microsoft Excel, it is not possible to define a calculated field with the same name when multiple PivotTables refer to the same base pivot cache, and Syncfusion XlsIO follows the same behavior. +In **Microsoft Excel**, calculated fields are scoped to the pivot cache, not to individual PivotTables. -You can either assign different names to the calculated fields when using the same pivot cache, or create each PivotTable from a unique pivot cache, which allows calculated fields with identical names. \ No newline at end of file +This means: + +* **If multiple PivotTables share the same cache** + * Calculated fields with the same name cannot be defined. Excel reuses the existing field from the cache, and its formula cannot be redefined independently. + * Calculated fields with different names can be defined, and they will be available to all PivotTables that share that cache. + +* **If PivotTables use separate caches (even if the source data is identical)** + * 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. + +**Syncfusion XlsIO** follows the same cache scoped behavior: +* With a shared cache, calculated field names must be unique. +* With separate caches, calculated field names can be identical or different. + +The following examples illustrate calculated field naming across PivotTables in C# (cross-platform and Windows-specific) and VB.NET. + +{% tabs %} +{% 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" %} +using (ExcelEngine excelEngine = new ExcelEngine()) +{ + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + IWorkbook workbook = application.Workbooks.Create(3); + IWorksheet dataSheet = workbook.Worksheets[0]; + IWorksheet pivotSheet1 = workbook.Worksheets[1]; + IWorksheet pivotSheet2 = workbook.Worksheets[2]; + + //Add sample data + dataSheet.Range["A1"].Text = "Product"; + dataSheet.Range["B1"].Text = "Sales"; + dataSheet.Range["C1"].Text = "Cost"; + + dataSheet.Range["A2"].Text = "Laptop"; + dataSheet.Range["B2"].Number = 5000; + dataSheet.Range["C2"].Number = 3000; + + dataSheet.Range["A3"].Text = "Tablet"; + dataSheet.Range["B3"].Number = 3000; + dataSheet.Range["C3"].Number = 2000; + + dataSheet.Range["A4"].Text = "Phone"; + dataSheet.Range["B4"].Number = 4000; + dataSheet.Range["C4"].Number = 2500; + + //CASE 1: Shared pivot cache - calculated field names must be unique + IPivotCache sharedCache = workbook.PivotCaches.Add(dataSheet["A1:C4"]); + + IPivotTable pivot1 = pivotSheet1.PivotTables.Add("Pivot1", pivotSheet1["A1"], sharedCache); + pivot1.Fields[0].Axis = PivotAxisTypes.Row; + pivot1.DataFields.Add(pivot1.Fields[1], "Total Sales", PivotSubtotalTypes.Sum); + pivot1.CalculatedFields.Add("Profit", "Sales - Cost"); + + IPivotTable pivot2 = pivotSheet1.PivotTables.Add("Pivot2", pivotSheet1["F1"], sharedCache); + pivot2.Fields[0].Axis = PivotAxisTypes.Row; + pivot2.DataFields.Add(pivot2.Fields[2], "Total Cost", PivotSubtotalTypes.Sum); + pivot2.CalculatedFields.Add("Margin", "Sales - Cost"); + + //CASE 2: Separate pivot caches - identical or different calculated field names are allowed + IPivotTable pivot3 = pivotSheet2.PivotTables.Add("Pivot3", pivotSheet2["A1"], + workbook.PivotCaches.Add(dataSheet["A1:C4"])); + pivot3.Fields[0].Axis = PivotAxisTypes.Row; + pivot3.DataFields.Add(pivot3.Fields[1], "Total Sales", PivotSubtotalTypes.Sum); + pivot3.CalculatedFields.Add("Profit", "Sales - Cost"); + + IPivotTable pivot4 = pivotSheet2.PivotTables.Add("Pivot4", pivotSheet2["F1"], + workbook.PivotCaches.Add(dataSheet["A1:C4"])); + pivot4.Fields[0].Axis = PivotAxisTypes.Row; + pivot4.DataFields.Add(pivot4.Fields[2], "Total Cost", PivotSubtotalTypes.Sum); + pivot4.CalculatedFields.Add("Profit", "Sales - Cost"); + + //Saving the workbook + workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx")); +} +{% endhighlight %} + +{% highlight c# tabtitle="C# [Windows-specific]" %} +using (ExcelEngine excelEngine = new ExcelEngine()) +{ + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + IWorkbook workbook = application.Workbooks.Create(3); + IWorksheet dataSheet = workbook.Worksheets[0]; + IWorksheet pivotSheet1 = workbook.Worksheets[1]; + IWorksheet pivotSheet2 = workbook.Worksheets[2]; + + //Add sample data + dataSheet.Range["A1"].Text = "Product"; + dataSheet.Range["B1"].Text = "Sales"; + dataSheet.Range["C1"].Text = "Cost"; + + dataSheet.Range["A2"].Text = "Laptop"; + dataSheet.Range["B2"].Number = 5000; + dataSheet.Range["C2"].Number = 3000; + + dataSheet.Range["A3"].Text = "Tablet"; + dataSheet.Range["B3"].Number = 3000; + dataSheet.Range["C3"].Number = 2000; + + dataSheet.Range["A4"].Text = "Phone"; + dataSheet.Range["B4"].Number = 4000; + dataSheet.Range["C4"].Number = 2500; + + //CASE 1: Shared pivot cache - calculated field names must be unique + IPivotCache sharedCache = workbook.PivotCaches.Add(dataSheet["A1:C4"]); + + IPivotTable pivot1 = pivotSheet1.PivotTables.Add("Pivot1", pivotSheet1["A1"], sharedCache); + pivot1.Fields[0].Axis = PivotAxisTypes.Row; + pivot1.DataFields.Add(pivot1.Fields[1], "Total Sales", PivotSubtotalTypes.Sum); + pivot1.CalculatedFields.Add("Profit", "Sales - Cost"); + + IPivotTable pivot2 = pivotSheet1.PivotTables.Add("Pivot2", pivotSheet1["F1"], sharedCache); + pivot2.Fields[0].Axis = PivotAxisTypes.Row; + pivot2.DataFields.Add(pivot2.Fields[2], "Total Cost", PivotSubtotalTypes.Sum); + pivot2.CalculatedFields.Add("Margin", "Sales - Cost"); + + //CASE 2: Separate pivot caches - identical or different calculated field names are allowed + IPivotTable pivot3 = pivotSheet2.PivotTables.Add("Pivot3", pivotSheet2["A1"], + workbook.PivotCaches.Add(dataSheet["A1:C4"])); + pivot3.Fields[0].Axis = PivotAxisTypes.Row; + pivot3.DataFields.Add(pivot3.Fields[1], "Total Sales", PivotSubtotalTypes.Sum); + pivot3.CalculatedFields.Add("Profit", "Sales - Cost"); + + IPivotTable pivot4 = pivotSheet2.PivotTables.Add("Pivot4", pivotSheet2["F1"], + workbook.PivotCaches.Add(dataSheet["A1:C4"])); + pivot4.Fields[0].Axis = PivotAxisTypes.Row; + pivot4.DataFields.Add(pivot4.Fields[2], "Total Cost", PivotSubtotalTypes.Sum); + pivot4.CalculatedFields.Add("Profit", "Sales - Cost"); + + //Saving the workbook + workbook.SaveAs("Output.xlsx"); +} +{% endhighlight %} + +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} +Using excelEngine As New ExcelEngine() + Dim application As IApplication = excelEngine.Excel + application.DefaultVersion = ExcelVersion.Xlsx + + Dim workbook As IWorkbook = application.Workbooks.Create(3) + Dim dataSheet As IWorksheet = workbook.Worksheets(0) + Dim pivotSheet1 As IWorksheet = workbook.Worksheets(1) + Dim pivotSheet2 As IWorksheet = workbook.Worksheets(2) + + ' Add sample data + dataSheet.Range("A1").Text = "Product" + dataSheet.Range("B1").Text = "Sales" + dataSheet.Range("C1").Text = "Cost" + + dataSheet.Range("A2").Text = "Laptop" + dataSheet.Range("B2").Number = 5000 + dataSheet.Range("C2").Number = 3000 + + dataSheet.Range("A3").Text = "Tablet" + dataSheet.Range("B3").Number = 3000 + dataSheet.Range("C3").Number = 2000 + + dataSheet.Range("A4").Text = "Phone" + dataSheet.Range("B4").Number = 4000 + dataSheet.Range("C4").Number = 2500 + + 'CASE 1: Shared pivot cache - calculated field names must be unique + Dim sharedCache As IPivotCache = workbook.PivotCaches.Add(dataSheet.Range("A1:C4")) + + Dim pivot1 As IPivotTable = pivotSheet1.PivotTables.Add("Pivot1", pivotSheet1.Range("A1"), sharedCache) + pivot1.Fields(0).Axis = PivotAxisTypes.Row + pivot1.DataFields.Add(pivot1.Fields(1), "Total Sales", PivotSubtotalTypes.Sum) + pivot1.CalculatedFields.Add("Profit", "Sales - Cost") + + Dim pivot2 As IPivotTable = pivotSheet1.PivotTables.Add("Pivot2", pivotSheet1.Range("F1"), sharedCache) + pivot2.Fields(0).Axis = PivotAxisTypes.Row + pivot2.DataFields.Add(pivot2.Fields(2), "Total Cost", PivotSubtotalTypes.Sum) + pivot2.CalculatedFields.Add("Margin", "Sales - Cost") + + 'CASE 2: Separate pivot caches - identical or different calculated field names are allowed + Dim pivot3 As IPivotTable = pivotSheet2.PivotTables.Add("Pivot3", pivotSheet2.Range("A1"), + workbook.PivotCaches.Add(dataSheet.Range("A1:C4"))) + pivot3.Fields(0).Axis = PivotAxisTypes.Row + pivot3.DataFields.Add(pivot3.Fields(1), "Total Sales", PivotSubtotalTypes.Sum) + pivot3.CalculatedFields.Add("Profit", "Sales - Cost") + + Dim pivot4 As IPivotTable = pivotSheet2.PivotTables.Add("Pivot4", pivotSheet2.Range("F1"), + workbook.PivotCaches.Add(dataSheet.Range("A1:C4"))) + pivot4.Fields(0).Axis = PivotAxisTypes.Row + pivot4.DataFields.Add(pivot4.Fields(2), "Total Cost", PivotSubtotalTypes.Sum) + pivot4.CalculatedFields.Add("Profit", "Sales - Cost") + + 'Saving the workbook + workbook.SaveAs("Output.xlsx") +End Using +{% endhighlight %} +{% endtabs %} + +A complete working example to demonstrate calculated field naming across PivotTables using C# is present on this GitHub page. \ No newline at end of file From 9fb016cc1318f5b2cb5acbd0d06c8d62001d0c5b Mon Sep 17 00:00:00 2001 From: MOHAN CHANDRAN <93247949+Mohan2401@users.noreply.github.com> Date: Thu, 27 Nov 2025 18:25:14 +0530 Subject: [PATCH 3/3] Update can-pivot-tables-share-calculate-field-names.md --- .../NET/faqs/can-pivot-tables-share-calculate-field-names.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Document-Processing/Excel/Excel-Library/NET/faqs/can-pivot-tables-share-calculate-field-names.md b/Document-Processing/Excel/Excel-Library/NET/faqs/can-pivot-tables-share-calculate-field-names.md index d143417d7..7b98e3a68 100644 --- a/Document-Processing/Excel/Excel-Library/NET/faqs/can-pivot-tables-share-calculate-field-names.md +++ b/Document-Processing/Excel/Excel-Library/NET/faqs/can-pivot-tables-share-calculate-field-names.md @@ -6,7 +6,7 @@ control: XlsIO documentation: UG --- -# Can PivotTables share calculated field names? +# Can PivotTables have the same name for multiple calculated fields? In **Microsoft Excel**, calculated fields are scoped to the pivot cache, not to individual PivotTables. @@ -201,4 +201,4 @@ End Using {% endhighlight %} {% endtabs %} -A complete working example to demonstrate calculated field naming across PivotTables using C# is present on this GitHub page. \ No newline at end of file +A complete working example to demonstrate calculated field naming across PivotTables using C# is present on this GitHub page.