Perspective JavaScript: export a pivot without counting its total twice

Keep the grouping configuration and root-versus-child relationship with the exported reading.

The grouped report shows North, South, and a grand total. Adding every visible row would count the same units twice. Treat a pivot's total row as a summary of its children, and retain the grouping configuration with the export. A flat list of unlabeled numbers loses that relationship.

Use four fictional packing records: North has six and four units; South has five and three. The group totals should be ten and eight, with eighteen overall. These independently calculated values define the exercise; they are not observations from a business system.

Perspective: actual group totals and configured filter changes
Perspective: actual group totals and configured filter changes
Open the full example ↗

Open the complete example · Download the full HTML source

Configure the view that answers the question

Perspective separates a table from the views queried from it. Its view-querying documentation shows selecting columns, applying filters, grouping rows, and assigning aggregates through the view configuration.

const view = await table.view({
  columns: ['units'],
  group_by: ['region'],
  aggregates: {units: 'sum'}
});

This snippet starts with an existing Perspective table; initialize the matching engine and load the source records before creating the view. The browser worker guide describes a client backed by a dedicated worker and WebAssembly engine. Keep the loader, worker, and binary from a compatible release when building the page.

Specify sum explicitly for units so the intended measure remains visible in the configuration. A count of records and a sum of units answer different questions. For the fixture, both North and South contain two records, but their unit totals differ.

Keep the hierarchy when handing off rows

The grouping documentation explains grouped totals and row paths. Inspect the actual rows emitted by your selected release and preserve their hierarchy metadata. Render the root as “All regions” and its children as their region names rather than presenting three peer categories.

For the supplied fixture, the root's eighteen equals North's ten plus South's eight. Adding root and children would yield thirty-six, which is not a second valid overall total. Put that independent reconciliation beside the exported reading copy.

Export configuration as well as values when someone must reproduce the pivot. Record the selected columns, grouping order, filter conditions, and aggregate for every measure. A screenshot can show labels, but it cannot establish which source rows the configured view included.

The Excel PivotTable planning guide addresses a workbook task and its aggregation choices. This recipe concerns a Perspective view and its exported hierarchy; it does not create a native Excel PivotTable by placing the values in an HTML table.

Check a filter before freezing the reading copy

Apply a North-only filter in a separate configured view and compare the result with the source records. Its included unit total should be ten. Restore the unfiltered configuration and require eighteen again. Label the filter on the reading copy so a reviewer can explain why two exports differ.

Do not claim that an exported HTML summary will keep querying a running worker after that engine has been removed. Deliver a live page with its required assets or a clearly labeled static reading copy. Finish by reconciling the root against the leaf totals and storing the accepted configuration beside the source extract.

The complete example pins Perspective 2.10.1 and includes its loader, worker code, and WebAssembly bytes. It renders the actual to_json() row paths. To keep the engine usable inside a constrained page preview, this delivery selects the package's in-process fallback and embeds its worker code and WebAssembly bytes. It does not promise a dedicated worker. North, South, and Restore all regions each rebuild a configured view.

Keep reading