
JavaScript pivot tables compared
Compare JavaScript pivot table components, pivot grids, reporting tools, and spreadsheet engines, including RevoGrid Pivot for embedded analytics.
The four orders in this example bring in 250 across 12 units. The average selling price per unit is about 20.83. To get that number at every level of a Pivot table, divide total revenue by total quantity after grouping the rows.
In RevoGrid Pivot, a formula measure divides two aggregate sums. The same report also shows each region’s share of revenue. A second view orders the quarters so you can calculate running revenue and compare each quarter with the previous one.
Pivot product · Calculated values and live example · Percentage examples · Runnable source
From examples/pivot-workflows in the documentation repository:
npm ci
npm run calculationsUse Node 24.13 or later. The script runs the report with @revolist/pivot/headless and prints the results. It stops with an error if a calculation differs from the expected value.
The example uses a V1 report. The app prepares two numeric fields, and Pivot uses them to calculate the weighted price.
| Region | Quarter | Unit price | Quantity | Revenue |
|---|---|---|---|---|
| North | 2026-Q1 | 10 | 1 | 10 |
| South | 2026-Q1 | 20 | 3 | 60 |
| South | 2026-Q1 | 20 | 6 | 120 |
| South | 2026-Q2 | 30 | 2 | 60 |
North contains one order; South contains three. The report's expected results are:
| Region | Revenue | Share of revenue | Eligible quantity | Weighted unit price |
|---|---|---|---|---|
| North | 10 | 4% | 1 | 10 |
| South | 240 | 96% | 11 | 21.8181818… |
| Grand total | 250 | 100% | 12 | 20.8333333… |
The grand-total weighted price is 250 / 12. Averaging the regional prices gives (10 + 240 / 11) / 2, or approximately 15.9091. Averaging the four order prices gives 20. Both answer different questions from “what was the average selling price per unit?”
For each fact, the application creates:
weightedAmount = price × quantity.eligibleQuantity = quantity, only when both inputs are present.function weightedInputs(row) {
const paired = row.price !== null && row.quantity !== null;
return {
...row,
weightedAmount: paired ? row.price * row.quantity : null,
eligibleQuantity: paired ? row.quantity : null,
};
}The complete prepare function first rejects nonfinite numbers and numeric strings, and rejects negative quantities for this business example. These are application input rules. A field label or a currency formatter does not perform that validation.
Pairing matters. If an order has a missing price and quantity 100, including those 100 units only in the denominator would depress the report's price. The executable example adds that record and verifies that the weighted grand total remains 250 / 12.
If returns use negative quantities in your application, define their financial meaning before changing the validation. A zero denominator has no weighted average; display the resulting missing value rather than inventing a price of zero.
Register the materialized columns as physical fields and give every measure a stable ID:
const measures = [
{
id: 'weighted-amount',
fieldId: 'weightedAmount',
aggregator: 'sum',
label: 'Weighted amount',
},
{
id: 'eligible-quantity',
fieldId: 'eligibleQuantity',
aggregator: 'sum',
label: 'Eligible quantity',
},
{
id: 'weighted-price',
fieldId: 'weightedAmount',
aggregator: 'sum',
label: 'Weighted price',
calculation: {
type: 'formula',
expression: {
type: 'binary',
operator: '/',
left: { type: 'measure', id: 'weighted-amount' },
right: { type: 'measure', id: 'eligible-quantity' },
},
},
},
];The formula references measure IDs, not source properties or generated column names. At North, it divides 10 by 1. At South, it divides 240 by 11. At the grand total, it divides 250 by 12. It never averages the already calculated regional prices.
The helper measures remain part of the analytical report. The example's custom printed table chooses which outputs to display; it does not delete formula dependencies from the report. When building a custom presentation, retain the supporting measures and choose the visible output deliberately.
Formula references read underlying aggregate values by default. If a formula should consume another measure's calculated percentage instead, use referenceValues: 'calculated' and test that interpretation. The analytical semantics guide explains these two stages and the treatment of missing comparisons.
Revenue share requires no application division:
{
id: 'share',
fieldId: 'revenue',
aggregator: 'sum',
label: 'Share',
calculation: { type: 'percentOfGrandTotal' },
}The numeric results are 0.04 and 0.96. A percentage formatter displays them as 4% and 96%; it does not change the stored numbers. Multiplying by 100 and then applying a percentage formatter would display the wrong values.
The denominator should follow the intended source scope. Filtering source facts changes that scope. Merely hiding a total row or showing a subset of the analytical result is not a reason to sum visible rows and invent a new denominator. Verify the percentage behavior that matches your report layout.
For the second report, put quarter on rows and keep the original revenue measure. Add two calculated measures:
{
id: 'running',
fieldId: 'revenue',
aggregator: 'sum',
label: 'Revenue to date',
calculation: {
type: 'runningTotal', axis: 'rows', fieldId: 'quarter',
},
},
{
id: 'growth',
fieldId: 'revenue',
aggregator: 'sum',
label: 'Change',
calculation: {
type: 'percentDifferenceFrom',
axis: 'rows',
fieldId: 'quarter',
base: 'previous',
},
}These are portable-report measures, so their calculation uses fieldId. A native PivotConfig uses field for the same axis field; keep the two configuration formats distinct.
| Quarter | Revenue | Running revenue | Change from previous quarter |
|---|---|---|---|
| 2026-Q1 | 190 | 190 | No previous quarter |
| 2026-Q2 | 60 | 250 | −68.4210526…% |
The second percentage is (60 - 190) / 190. Q1's comparison is null, not a decline of 100%. The example disables the grand-total row for this period-comparison view because a “change from previous grand total” does not answer the question shown by the table.
Sequence calculations require an intentional analytical order. The fixture supplies Q1 before Q2 with unambiguous period keys and asserts the resulting sequence. Localized display labels such as “January” and “February,” or a user's presentation sort, are not a substitute for defining the business calendar. Supply and verify the desired ordering when adapting the report to months, fiscal periods or multiple years.
The aggregation documentation also exposes createPivotWeightedAverage({ id, valueFieldId, weightFieldId }). It constructs paired derived fields and formula dependencies for a V2 local report. It is not a SQL expression generator, and a server adapter must implement its own equivalent calculation contract.
Use the V1 formula above if you are following this example with the public trial. The V2 helper failed in trial versions 4.5.1 and 4.5.2 with Cannot read properties of undefined (reading '2'). Test the V2 helper with the package you plan to deploy before choosing it.
Three checks to make when you adapt the report:
Use the SQL Pivot tutorial when the database should own aggregation. Use headless Pivot in Node.js when the application needs report files or a custom interface without a grid element.