2016-10-26 1 views
0

Ich erhalte (gratis) eine "Gesamtsumme" Spalte in meiner Pivot-Tabelle:Wie kann ich "Grand Total" -Werte als Mittelwert und nicht als Summe erhalten?

enter image description here

Für "TotalQty" und "Totalprice" Das ist toll, aber für "avgprice" und "PrcntgOfTotal" nicht so viel.

Wie kann ich diese Werte erhalten, um den Durchschnitt im ersten Fall statt einer Summe anzuzeigen?

Die Monatsprozentsätze sind die% des Gesamtpreises für die Summe dieses Monats (nicht die Gesamtsumme aller Monate); Daher sollte GrandTotal.PrcntgOfTotal auch kein Durchschnitt der Monatswerte sein. Hier

ist der Code, den ich habe, dass generiert diese Pivot-Tabelle:

private void PopulatePivotTableSheet() 
{ 
    string NORTHWEST_CORNER_OF_PIVOT_TABLE = "A6"; 
    AddPrePivotTableDataToPivotTableSheet(); 
    var dataRange = rawDataWorksheet.Cells[rawDataWorksheet.Dimension.Address]; 
    dataRange.AutoFitColumns(); 
    var pivotTable = pivotTableWorksheet.PivotTables.Add(
         pivotTableWorksheet.Cells[NORTHWEST_CORNER_OF_PIVOT_TABLE], 
         dataRange, 
         "PivotTable"); 
    pivotTable.MultipleFieldFilters = true; 
    //pivotTable.RowGrandTotals = true; <= default setting 
    //pivotTable.ColumGrandTotals = true; <= default setting 
    //pivotTable.RowGrandTotals = false; // this prevents the "Grand Total" column 
    //pivotTable.ColumGrandTotals = false; // this prevents the totals rows at the bottom 
    //pivotTable.Compact = true; 
    //pivotTable.CompactData = true; 
    pivotTable.GridDropZones = false; 
    pivotTable.Outline = false; 
    pivotTable.OutlineData = false; 
    pivotTable.ShowError = true; 
    pivotTable.ErrorCaption = "[error]"; 
    pivotTable.ShowHeaders = true; 
    pivotTable.UseAutoFormatting = true; 
    pivotTable.ApplyWidthHeightFormats = true; 
    pivotTable.ShowDrill = true; 

    // Row field[s] 
    var descRowField = pivotTable.Fields["Description"]; 
    pivotTable.RowFields.Add(descRowField); 

    // Column field[s] 
    var monthYrColField = pivotTable.Fields["MonthYr"]; 
    pivotTable.ColumnFields.Add(monthYrColField); 

    // Data field[s] 
    var totQtyField = pivotTable.Fields["TotalQty"]; 
    pivotTable.DataFields.Add(totQtyField); 

    var totPriceField = pivotTable.Fields["TotalPrice"]; 
    pivotTable.DataFields.Add(totPriceField); 

    // Don't know how to calc these vals here, so had to put them on the data sheet 
    var avgPriceField = pivotTable.Fields["AvgPrice"]; 
    pivotTable.DataFields.Add(avgPriceField); 

    var prcntgOfTotalField = pivotTable.Fields["PrcntgOfTotal"]; 
    pivotTable.DataFields.Add(prcntgOfTotalField); 
} 

Antwort

1
pivotTable.DataFields[DATAFIELD NUM].Function = 
OfficeOpenXml.Table.PivotTable.DataFieldFunctions.Average 

Beachten Sie, dass die DATENFELD NUM ist der Index im Datenfeld Sammlung. Definiert in:

OfficeOpenXml.Table.PivotTable.ExcelPivotTableDataFieldCollection 
Verwandte Themen