Pivot Table Data for .NET C# in-memory cube for fast data aggregation and pivot table reports generation

OLAP library NReco.PivotData.dll

  • pure C# library for data aggregation and grouping: calculates data for pivot table (pivot grid, pivot chart) from any enumerable data source or SQL database.
  • implements fast in-memory data cube (multidimensional dataset) and OLAP query operations (slice/dice/filter/roll-up/pivot).
  • aggregate (summary) functions: count, sum, average, min, max, count unique values, list unique values, list values, variance, standard deviation, custom formula. Several measures may be collected at once.
  • supports incremental data processing (real-time data aggregation), merging calculation results (parallel/distributed aggregation), lazy totals calculation.
  • fast data cube serialization (save/load in-memory cube state).
  • pivot table data model (2D or n-Dimensional): several dimensions per axis (label grouping), sort by labels, sort by values, totals, sub-totals.
  • simplest way to pivot a DataTable or POCO models list (with one line of C# code)!

download and pricing

PivotData library implements in-memory multidimensional dataset (data cube) for further analytical processing in .NET applications. Library performs fast real-time data grouping and aggregation, calculates data for pivot tables and charts without OLAP server.

BI toolkit NReco.PivotData.Extensions.dll + js front-end

  • pivot table visualization and export components:
    • HTML table renderer that supports labels grouping, multiple measures, totals and groups subtotals, custom values formatting and CSS styling
    • export pivot table to CSV, Excel, PDF, JSON or .NET DataTable
    • calculate pivot table percentage/difference values
    • javascript widget for interactive pivot table sorting (by labels/values/totals), fixed headers, server-side pagination for large tables
    • filter data cube or pivot table by simple keyword-based query
    • ASP.NET pivot table control for building pivot grid or pivot chart by end-user
  • data processing:
    • ETL components: parse numbers/dates, merge/match by regex, combine columns, calculate derived values
    • load tabular data from SQL database, CSV/TSV, JSON
    • populate cube with DB aggregation results (SELECT GROUP BY, MongoDb aggregate pipeline)
    • export cube to DataSet (star schema)
    • save/load cube data to file
    • memory-optimized read-only cube implementation (FixedPivotData)
  • read more details about PivotData Toolkit...

quick purchase process

  • 1 Choose a package
  • 2 Pay online 2Checkout.com is a worldwide leader in online payment services
  • 3 Download the package
PivotData Toolkit provides production-ready .NET components for creating pivot table reports and charts with several lines of C# code. It includes web pivot table builder that can be easily integrated into any ASP.NET application. You can evaluate Toolkit components free of charge: download PivotData examples.

how to use create pivot table example

  1. Install NReco.PivotData nuget package.
  2. Aggregate data using PivotData class with one line of C# code:
    var pivotData = new PivotData(
    	new string[] {"name","age"},
    	new AverageAggregatorFactory("salary"),
    	new DataTableReader(t) );
  3. Access data cube values:
    var grandTotal = pivotData[Key.Empty,Key.Empty].Value;
    var subTotalFor29 = pivotData[Key.Empty,29].Value;
    var allDimensionKeys = pivotData.GetDimensionKeys();
  4. Create pivot table model:
    var pivotTable = new PivotTable(
    	new []{"name"}, // row dimension(s)
    	new []{"age"}, // column dimension(s)
    	pivotData );
  5. Render pivot table to HTML table:
    For this code snippet you need to install NReco.PivotData.Extensions nuget package.
    var htmlResult = new StringWriter();
    var pvtHtmlWr = new PivotTableHtmlWriter(htmlResult);
  6. That's all! Next steps:
Have a question? Feel free to ask.

online c# pivot table demo

see also
ASP.NET Example: Pivot Table Builder Demo Advanced demos: interactive pivot table and pivot chart builder, customized percentage report
ASP.NET Example: Dashboard with charts How to use PivotData components for creating asp.net dashboard

frequently asked questions

PivotData OLAP library can do the following:
  • perform fast in-memory aggregation of tabular data and calculate one or several measures in one pass
  • execute OLAP queries from C# code in linq-style: slice/dice, drill up/drill down, roll-up/pivot (without OLAP server and MDX)
  • calculate summary data for pivot tables (pivot charts) in .NET applications
PivotData BI Toolkit usage scenarios:
  • reports automation: generate highly customizable pivot tables and pivot charts
  • render/export pivot tables to HTML/CSV/Excel/PDF/JSON formats on the server side
  • add web pivot table builder to ASP.NET application (MVC, MVC Core, WebForms) or any web applications (with PivotData .NET Core microservice on backend)
  • embedded business intelligence and analytics, scheduled reports generation, custom BI dashboards, real-time reporting functionality
PivotData can be used with large variety of data sources:
  • in-memory .NET collections (anything with IEnumerable interface)
  • any SQL database that has ADO.NET provider:
    • MS SQL Server, Azure SQL
    • MySQL, MariaDB, MemSql
    • PostgreSQL, Amazon RedShift
    • SQLite
    • Oracle DB
    • IBM DB2
  • MongoDB (aggregate pipeline can be used with Toolkit's GroupedSourceReader)
  • ElasticSearch
  • large CSV / TSV files (with help of CsvHelper for reading comma-delimeted/tab-delimited files)
  • JSON data (array of objects / array of values)
  • Azure Table Storage / Azure DocumentDB, Amazon SimpleDB / Google BigQuery
PivotData can process millions of data rows in seconds; it is suitable for processing large data files (>1Gb) like CSV or log files.
Aggregation of large datasets (>1 mln rows) should be performed on database/warehouse level (with GROUP BY query) and result can be loaded into PivotData cube for further processing.
PivotData Toolkit (NReco.PivotData.Extensions.dll) extends OLAP library with advanced components for typical analytics/reporting functionality:
PivotData Tool is a command line (console) utility that can be used for automating data aggregation and pivot table generation tasks (no programming needed):
  • aggregate data from csv file, MS SQL or any SQL database with ODBC driver
  • define derived values and parse rules for input data
  • save/load cube data from files
  • filter data cube (slice/dice)
  • export pivot table to HTML/CSV/Excel file
Binary cube files produced by PivotData Tool can be loaded from C# code with PivotData Toolkit or used as data source in PivotData microservice.
Typical PivotData Tool usage:
  • scheduled re-calculation of data cube files (for example, daily or hourly)
  • batch pivot table reports generation
PivotData microservice is a product built on the top of PivotData Toolkit:
  • provides web API for all pivot table HTML rendering and export functions
  • connects to various data sources with simple json config: MS SQL Server, MySQL, PostgreSQL, SQLite, MongoDb
  • uses internal data cache to provide better UX
  • can be scaled horizontally
  • includes pivot table builder front-end (HTML/JS) that may be easily embedded into any web application (PHP, Ruby, Python, Java etc)
  • .NET Core microservice: can be hosted both on Linux and Windows servers.
Depending on situation PivotData can (re)calculate all subtotal/grand total values either as part of data processing (PivotData.ProcessData) or when totals are accessed for the first time (LazyTotals=true). This difference is essential for data cubes with a lot of dimensions (>3) and/or significant amount of unique dimension keys (>100): non-lazy calculation of subtotals for all dimensions recombinations may take a lot of time and lead to OutOfMemory exception.

In general LazyTotals=false mode is recommended in the following cases:
  • you need extremely fast random access to sub-totals many times and sub-totals doesn't cause "out-of-memory" exception; for example, this may be concurrent requests to the same in-memory data cube.
  • data cube is used for aggregating real-time data stream (ProcessData is called many times for data chunks) and sub-totals should be kept up-to-date without re-calculating totals when next data chunk is arrived (for example in case of real-time monitoring/alerts).
  • all PivotData subtotals assumed to be used: good example is the pivot table report generation that renders subtotals for all row/column intersections. Since all subtotals should be calculated anyway, it is more effective to calculate them at once with LazyTotals=false.
    The typical scenario for generating pivot report from a big data cube (LazyTotals=true):
    • slicing by dimensions needed for pivot table with PivotData.Slice method (LazyTotals=false)
    • use sliced PivotData with PivotTable view to iterate through pivot table rows/columns
PivotData Toolkit can be used for creating OLAP local cubes (represented by .cub file) in the following way (lets assume that some dataset is already aggregated with PivotData library):
  1. export PivotData instance into relational representation (DataSet with star schema) using DataSetStarSchemaWriter component
  2. save DataSet into csv files with schema.ini for Text Driver (MS Jet Engine)
  3. use AMO (Analysis Services .NET API) to create new local cube (.cub file)
  4. configure dimensions, data source (JET OLE Provider to csv files) and setup cube according to PivotData instance metadata
  5. Process cube (this action will force OLAP to read all data from csv files and store them in the local cube)
This scenario is implemented in the CreateOfflineCube example (included into PivotData Toolkit package).
The following code snippet illustrates how to pivot a DataTable:
DataTable tbl;
var pvtData = new PivotData(new[] {"product", "year" }, new CountAggregatorFactory() );
pvtData.ProcessData(new DataTableReader(tbl));
var pvtTbl = new PivotTable(
    new [] {"product"}, //rows
	new [] {"year"}, //columns
PivotTable object can be easily exported to DataTable with PivotTableDataTableWriter (Toolkit component):
var dataTableWr = new PivotTableDataTableWriter("Test");
var tbl = dataTableWr.Write(pvtTbl);

See also: how to pivot DataTable to Excel PivotTable.

It is possible to render pivot table in WinForms/WPF/UWP apps in one of the following ways:
  • export PivotTable model to DataTable (with PivotTableDataTableWriter) and bind it to DataGrid control. This approach is useful if you don't need labels grouping and other advanced rendering options.
  • use WebBrowser (WinForms) or WebView (WPF/UWP) to show HTML provided by PivotTableHtmlWriter
Examples package includes "ToolkitPivotTableWinForms" example that illustrates these approaches.
PivotData library
NETStandard1.5 build (can be used by .NET Core 1.0 apps) is available starting from version 1.3.1.
Differences from full .NET 4.x build:
  • PivotData.ProcessData(IEnumerable<DataRow>) is not supported: DataRow class is not available in netstandard1.5
PivotData Toolkit
NETStandard1.5 build is available starting from version 1.3.0.
Differences from full .NET 4.x build:
  • DataSetStarSchemaWriter, PivotTableDataTableWriter are not available (there are no DataSet/DataTable in .NET Core).
PivotData library
PivotData library can be used for FREE in single-deployment projects (websites, intranet/extranet web applications) or applications for company's internal business purposes (redistributed only internally inside the company).
Applications for external redistribution (ISV) or SaaS deployments require commercial license (included into PivotData library enterprise package).
PivotData Toolkit
PivotData Toolkit (NReco.PivotData.Extensions.dll, js pivot table builder components, code from toolkit examples) usage/redistribution requires commercial license:
  • per-developer license (perpetual, royalty-free). All developers who are working on the project that uses PivotData Toolkit need to be licensed. Note: this license cannot be used for SaaS applications.
  • SaaS application license (perpetual, single-deployment). Anyone who works on the project may use PivotData Toolkit for the purpose of developing SaaS application. Only one production deployment is allowed (number of servers is not limited); if you offer self-hosted (on-premise) version of SaaS application, each installation requires its own SaaS application license.
If you’re not sure about the number of developer licenses you will need, please consult our support team.

All commercial packages include 1 year subscription of email support (assistance in components usage, troubleshooting, bugfixing) and free component updates. Expired subscription can be renewed with 50% discount to the full package price.

what's new NReco.PivotData.dll

2017 Feb 01 v.1.3.4 Changes:
  • added SliceQuery.Measure overload that gives keys context for measure value calculation. Useful in cases when formula result depends on the key (for example, calculate yearly growth).
  • added PivotData.ProcessData overload that can accept another IPivotData instance as input. Useful for multi-stage calculation when one pivot table is calculated by results of another one.
2016 Oct 18 v.1.3.3 Changes:
  • added FormulaAggregator (based on CompositeAggregator) for on-the-fly formula calculations
  • added SliceQuery.Measure overload for defining formula-based measure
  • minor fix in MaxAggregator/MinAggregator Merge method (extra check for null)
2016 Aug 14 v.1.3.2 Changes:
  • improved performance for SliceQuery.Where(string,object[])
  • now PivotDataState Serialize/Deserialize methods are available in netstandard1.5 build
  • ListAggregator/ListAggregatorFactory can accept field name to collect field values instead of source objects
  • now MinAggregator/MaxAggregator may be used with any IComparable values, not only with numeric types
2016 Jul 05 v.1.3.1 Changes:
  • Fixed issue with incorrect grand-total non-lazy calculation for PivotData cube with exactly one dimension
  • Improved internal performance for PivotData lazy totals calculation (1.5x)
  • Drastically increased performance for PivotTable row/column totals calculation (10x+)
  • Added build for .NET Standards 1.5 (now PivotData can be used from .NET Core 1.0 apps)
2016 May 10 v.1.3.0 Changes:
  • Bugfix: PivotTable.PreserveGroupOrder works incorrectly for the last column/row.
  • Added ListAggregatorFactory (ListAggregator) for accumulating all values from an input source (works like LINQ GroupBy).
  • Added VarianceAggregatorFactory (VarianceAggregator) for calculating variance, standard deviation (stdev), sample variance and sample standard deviation.
  • Added ObjectMember that simplifies processing of custom data models (typed collections).
  • Added PivotDataHelper that provides utility methods for any IPivotData object: GetDimensionKeys and GetDimensionType.
  • Improved ConvertHelper.ConvertToDecimal method (now it supports all primitive number types) and added ConvertHelper.ConvertToDouble method.
2016 Feb 04 v.1.2.6 Changes:
2016 Jan 13 v.1.2.5 Changes:
  • Fixed PivotTable issue when the same dimension is used for both rows and columns (should be diagonal matrix result).
  • New IAggregator.AsComposite() extension method avoids boilerplate code (cast to CompositeAggregator) for accessing PivotData or PivotTable with several measures.
  • Added QueryCube example that illustrates how to use SliceQuery to filter/slice/dice, define derived (calculated) dimensions and measures.
2015 Dec 11 v.1.2.4 Changes:
  • Added PivotTable.PreserveGroupOrder option: when enabled sort by value reorders rows/columns only inside groups.
  • now PivotData works in lazy totals mode by default (this is backward-compatible change)
  • added SortAsComparer for defining explicit order of pivot table rows or columns
  • fixed NaturalSortKeyComparer (exception when dimension contains mixed types). In this case it fallbacks to comparison of string equivalents.
  • added NaturalSortKeyComparer.ReverseInstance
2015 Sep 30 v.1.2.3 Changes:
  • moved SliceQuery from extensions (PivotData Toolkit) to NReco.PivotData assembly. SliceQuery can be used for querying data cube in linq-style.
  • PivotTable class: added PivotData property for accessing underlying data cube, added SortRowKeys/SortColumnKeys overloads for specifying measure index (for pivot tables with several measures)
  • Overloaded ToString in IAggregatorFactory implementations (used for getting human-readable measure names)
  • now NReco.PivotData.dll is a signed (strongly named) assembly
2015 July 27 v.1.2.2 Changes:
  • added PivotData.CopyTo for custom merge/slice cube operations
  • added PivotTableMD class for creating n-dimensional (n>2) pivot table views
  • added overloads for PivotData/PivotTable that accept custom comparers of dimension keys
  • changed LazyTotals mode behaviour: now accessing grand-total value doesn't force calculation of all sub-totals (reason out-of-memory exception for large cubes)
  • a lot of other minor changes/fixes
  • introduced IPivotData interface
  • improved PivotData performance and decreased memory consumption
Public API wasn't changed and 1.2.1 assumed to be backward compatible with 1.2.0.
Special thanks to Colin (XLcubed.com) for code changes used in this release.
2015 May 26 v.1.2.0 Changes:
  • fixed issue with PivotData.Merge behavior (totals)
  • fixed issue with PivotData.ProcessData in lazy totals mode
  • added fast Serialize/Deserialize methods for PivotDataState
  • added Slice method for PivotData (reduce dimensions / filter data)
  • significantly improved performance and decreased memory consumption (Key class is no longer used for wrapping key objects)
  • added CsvDemo example (aggregates data from csv file)
Note: PivotData API was changed (again!) and your code might need some (minor!) changes for migrating from 1.0.x/1.1.0.
2015 Apr 10 v.1.1.0 Changes:
  • dimension keys are sorted by default
  • added lazy totals calculation mode
  • added CompositeAggregator (enables ability to use several aggregators at once)
  • now PivotData dimensions are determined by single key
  • added PivotTable 2D view (slice) for PivotData. Supports: several dimensions for table rows/columns, sort row/columns by table values.
Note: PivotData API was changed and your code might need some minor changes for migrating from 1.0.x.
2015 Mar 09 v.1.0.1: Changes:
  • added serializable PivotDataState + save/load routines
  • added min/max aggregators
2015 Feb 11 v.1.0.0: Initial PivotData release. Implemented core features:
  • optimized totals calculation in batch processing model
  • merge operation for map/reduce usage scenario (parallel/distributed cube calculation)
  • basic set of aggregators: avg, count, count uniq / list uniq

toolkit changes NReco.PivotData.Extensions.dll

2017 Feb 01 v.1.3.4 changes:
2016 Dec 02 v.1.3.3 changes:
  • added JsonSource: use JSON data (file, REST API response) as input for PivotData.
  • PivotTableHtmlWriter RepeatKeysInGroups and RepeatDuplicateKeysAcrossDimensions options now can be enabled for rows, columns or both.
  • added RepeatKeysInGroups and RepeatDuplicateKeysAcrossDimensions properties to PivotTableExcelWriter (they have the same affect as in PivotTableHtmlWriter).
  • added PivotTableHtmlWriter.AllowHtml option (allows usage of HTML tags in table labels or cell values; false by default)
  • fixed PivotTableExcelWriter writer output when there are no dimensions for rows.
  • ToolkitPivotReportMvc example enhancements:
    • front-end code was reorganized into several reusable jQuery plugins to simplify integration
    • added ability to save/load report state
    • added SqlGroupByCube that illustrates how to calculate cube by SQL data source with GROUP BY query
2016 Oct 18 v.1.3.2 changes:
  • added PaginatePivotTable wrapper: used for efficient pivot table pagination on the server-side + related internal changed
  • added pagination support to front-end component (jquery.nrecopivottable.js) and ToolkitPivotReportMvc example
  • added new PivotTableHtmlWriter option: RepeatKeysInGroups (prevents labels grouping; false by default)
  • added new PivotTableHtmlWriter option: RepeatDuplicateKeysAcrossDimensions (prevents the same label merging across dimensions)
  • added more formatting options to PivotTableCsvWriter / PivotTableExcelWriter
  • GroupedSourceReader significant refactoring; some changes are NOT backward compatible.
  • fixed FixedPivotData issue (ArgumentOutOfRange exception when PivotDataState contains only several values)
  • fixed PivotTableDataTableWriter issue (appears when pivot table contains several measures)
2016 Sep 16 v.1.3.1 changes:
  • published NReco.PivotData.Extensions nuget package; it needs a license key to remove trial limitations
  • CubeKeywordFilter enhancements: starts-with / ends-with syntax (*keyword, keyword*), dimension hints syntax (year:2015)
  • PivotTableExcelWriter is now available in netstandard build (for .NET Core apps)
  • added more formatting options to ExcelWriter and CsvWriter (FormatKey/FormatValue)
  • fixed PivotTableJsonWriter JSON serialization issue (null and DBNull values)
  • fixed PivotTableDataTableWriter nullreference exception when MinAggregator or MaxAggregator is used
2016 Aug 14 v.1.3.0 changes:
  • added .NET Core-compatible NReco.PivotData.Extensions build (netstandard1.5)
  • added CubeKeywordFilter for simple keyword-based filtering
  • PivotReportMvc example: added keyword-based filter (CubeKeywordFilter + Awesomplete integration)
  • added PivotTableFactory.RegisterDimensionComparer (custom comparers for dimension keys)
  • added TopPivotTable.IncludeOtherGroups option
  • DifferencePivotTable wrapper for calculating difference between pivot table values
  • formatting options for PivotTableJsonWriter (FormatKey, FormatValue, FormatMeasureHeader, FormatDimensionLabel)
  • fixed PivotTableJsonWriter multiple aggregators bug
  • fixed CsvSource issue (CSV headers with whitespaces)
2016 Jun 03 v.1.2.0 changes:
  • added TopPivotTable wrapper (limits pivot table by top N rows and/or columns)
  • now PivotTableHtmlWriter can render dimension labels (for rows, columns, or both: controlled by RenderDimensionLabel property)
  • added PivotTableHtmlWriter option that controls THEAD/TBODY rendering ( RenderTheadTbody property )
  • added PercentagePivotTable.RoundDigits property
  • enhanced CsvSource to support CSV files without headers (Headers property)
  • MVC PivotReportMvc example enhancements:
    • added pivot charts (ChartistJS + PivotTableJsonWriter output)
    • pivot table js plugin: added fixed headers (freeze column/row headers) for large tables
    • added simple dashboard example
  • added new Toolkit package examples: ExcelPivotTable, DynamicListGrouping
  • fixed issue with grand total rendering without columns (PivotTableHtmlWriter)
  • fixed issue with rendering multiple measures in PivotTableDataTableWriter
2016 Feb 04 v.1.1.0 changes:
  • now all pivot table writers accept IPivotTable as input
  • added PercentagePivotTable for calculating pivot table percentage values (by grand total, row total or column total).
  • PivotTableHtmlWriter rendering logic was significantly refactored to support new sub-totals options: SubtotalColumns, SubtotalRows
  • PivotReportMvc example enhancements:
    • added option for calculating percentage values
    • added options for row and column sub-totals
    • added pivot chart vizualization: bar chart, stacked bar chart, horizontal bar chart, pie chart (Chartist JS library)
    • added customized summary report sample with export to Excel and PDF
2016 Jan 01 v.1.0.2 changes:
2015 Sep 30 v.1.0.1 changes:
  • added more options for PivotTable writers (HTML/CSV/Excel)
  • now all PivotTable writers support several measures (aggregators)
  • added simple WebForms example that renders pivot table
  • improved ToolkitPivotReportMvc example (data source selection, several measures support)
  • added DataSetStarSchemaWriter (exports PivotData to DataSet with star schema)
  • added ToolkitCreateOfflineCube (export PivotData to offline cube .cub file)
2015 Jul 28 v.1.0. First PivotData Toolkit release:
  • added FixedPivotData: read-only in-memory data cube implementation with optimized memory consumption.
  • added ToolkitSqlDbSource example (how to aggregate data from SQL database)
  • added ToolkitPivotBuilderMvc example (ASP.NET web pivot table builder)
  • a lot of bugfixes and performance improvements
2015 Jul 14 v.0.7 changes:
  • added PivotTableFactory (creates PivotTable by config)
  • added PivotTableCsvWriter (export PivotTable to CSV format)
  • added PivotTableExcelWriter (export PivotTable to Excel file)
  • added SliceQuery (simple way to slice and dice PivotData)
2015 Jun 25 v.0.6 changes:
  • added DbCommandSource (data source based on ADO.NET IDbCommand)
  • added GroupedSourceReader (loads pre-grouped dataset into PivotData)
2015 Jun 04 v.0.5 PivotData Toolkit alpha version. Basic features:
  • render PivotTable object to HTML table
  • PivotDataFactory (creates PivotData object by config)
  • parsers and derived value handlers for input data source
  • CSV data source

more components

  • PivotData Microservice

    .NET Core app that provides web API for PivotData Toolkit functionality: create pivot tables & charts by SQL/MongoDb databases, export to JSON/CSV/Excel/PDF. Can be used as backend for web pivot table builder.

  • PivotData Command Line Utility

    Pivotdata can aggregate data from CSV files and SQL databases and produce pretty pivot table reports (HTML/Excel/CSV/PDF). Doesn't need any programming and can be used by power users.