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
- Group the pivot by
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.
Average order is a measure: =SUM(Orders[Amount])/COUNT(Orders[Amount]).
The code behind it
import "@sheetwrite/core/styles.css";
// An "orders" table (Product, Amount) and a "products" table (ID, Category), and a "report" sheet
// Pivots over the data model need the full engine// The grid fills its host, so the host needs a heightconst host = document.querySelector<HTMLElement>("#grid");if (host === null) throw new Error("Missing #grid host");host.style.height = "480px";
// Orders (many) → Products (one) by stable IDs, and a measure: one undo step { }, }, { }, }, ],});
// A pivot over the model: rows from Products, values from Orders and the measure { model: { ], }, }, }, "report",);
// Whether the keys resolve now: ok (with a count of unmatched keys), duplicateKey, …}
// When the page removes the host: grid.destroy();