2016-10-26 1 views
0

Je reçois (gratis) une colonne « Total » dans mon tableau croisé dynamique:Comment puis-je obtenir des vals "Grand Total" à la moyenne plutôt qu'à la somme?

enter image description here

Pour « qte_totale » et « TotalPrice » ce qui est excellent, mais pour « AvgPrice » et « PrcntgOfTotal » pas beaucoup.

Comment puis-je obtenir ces vals pour montrer la moyenne dans le premier cas plutôt que d'une somme?

les pourcentages mensuels sont le% du prix total pour la somme de ce mois (et non la somme globale de tous les mois); donc GrandTotal.PrcntgOfTotal ne devrait pas être une moyenne des valeurs mensuelles non plus.

Voici le code que j'ai qui génère ce tableau croisé dynamique:

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); 
} 

Répondre

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

Notez que le NUM est ZONE DE DONNÉES l'index dans la collection de champs de données. Défini en:

OfficeOpenXml.Table.PivotTable.ExcelPivotTableDataFieldCollection