Skip to contentSheetwrite

Analysis

One pivot across related tables

Pivot orders by a column of a related table, with a measure that recalculates in every cell. Then break a key and see what the relationship and the pivot report.

Orders
240
Tables
Orders, Products, Regions
Relationships
2
Try it
  1. Group the pivot by
    Revenue …
  2. Products: Checking…

How the model reads the tables

  • A pivot over the model equals a pivot over Orders with each related column looked up for every order. An order without a matching product shows a blank row label.
  • A repeated lookup key makes the lookup ambiguous, so the pivot shows #VALUE! until the key is unique again.
  • The measure =SUM(Orders[Amount])/COUNT(Orders[Amount]) is calculated again for each cell and total, so a total is the mean of all its orders.
  • Relationships, measures and model pivots are saved in the workbook, undone and synced. Excel opens an exported file without them.
The data model: three related tables and the pivot over themOrders has Order, Product, Region, Units and Amount. Many orders relate to one product through Orders[Product] and Products[Product ID], and to one region through Orders[Region] and Regions[Code]. The model pivot Revenue by group takes its rows from Category, Region or Manager, and its values from Orders: Revenue as SUM of Amount, Orders as COUNTA of Order, and Average order, a measure.OneProductsProduct ID,Product, CategoryManyOrdersOrder, Product,Region, Units, AmountOneRegionsCode,Region, ManagerModel pivotRevenue by groupBy Category, Region or Manager:Revenue, Orders, Average orderProductRegion → Code
The data model: three related tables and the pivot over themOrders has Order, Product, Region, Units and Amount. Many orders relate to one product through Orders[Product] and Products[Product ID], and to one region through Orders[Region] and Regions[Code]. The model pivot Revenue by group takes its rows from Category, Region or Manager, and its values from Orders: Revenue as SUM of Amount, Orders as COUNTA of Order, and Average order, a measure.ManyOrdersProduct,RegionOneProductsProduct IDOneRegionsCodeModel pivotRevenue by groupBy Category, Region or Manager:Revenue, Orders, Average order

Average order is a measure: =SUM(Orders[Amount])/COUNT(Orders[Amount]).

The code behind it

data-model.ts
import "@sheetwrite/core/styles.css";
import { createGrid, initSheetwrite, SheetwriteStore, type Workbook } from "@sheetwrite/core";
// An "orders" table (Product, Amount) and a "products" table (ID, Category), and a "report" sheet
declare const workbook: Workbook;
// Pivots over the data model need the full engine
await initSheetwrite({ engine: await import("@sheetwrite/formulas") });
// The grid fills its host, so the host needs a height
const host = document.querySelector<HTMLElement>("#grid");
if (host === null) throw new Error("Missing #grid host");
host.style.height = "480px";
const grid = createGrid(host, { workbook });
// Orders (many) → Products (one) by stable IDs, and a measure: one undo step
{
op: "setRelationship",
id: "orders-products",
from: { table: "orders", column: "product" },
to: { table: "products", column: "id" },
},
},
{
op: "setMeasure",
id: "average-order",
name: "Average order",
table: "orders",
formula: "=SUM(Orders[Amount])/COUNT(Orders[Amount])",
},
},
],
});
// A pivot over the model: rows from Products, values from Orders and the measure
{
id: "revenue-by-group",
name: "Revenue by group",
source: {
model: {
table: "orders",
fields: [
{ table: "products", column: "category" },
{ table: "orders", column: "amount" },
],
},
},
anchor: { row: 0, col: 0 },
rows: [0],
columns: [],
values: [{ field: 1, aggregation: "SUM", name: "Revenue" }, { measure: "average-order" }],
},
"report",
);
// Whether the keys resolve now: ok (with a count of unmatched keys), duplicateKey, …
if (grid.store instanceof SheetwriteStore) {
console.info(grid.store.getRelationshipStatus("orders-products"));
}
// When the page removes the host: grid.destroy();
Show all 65 linesShow the first lines