Skip to content
Sheetwrite

Analysis

Data model

Relate workbook tables with one-to-many relationships that are saved in the workbook, undone and synchronized.

Edit on GitHubReport a problem

On this page

Every workbook table is a table of the data model. A relationship links two tables with one key column on each side:

A relationship and a pivot across itA relationship links two tables by one key column each. The from side, Orders[Customer], is the many side: many orders share a key. The to side, Customers[Customer], is the one side: each non-blank key is unique. A pivot over the model groups Orders by Customers › Region. A matching key reads that row's cell; no match or a blank key reads blank; a duplicate one-side key shows #VALUE! at the pivot's anchor.Many sideOrdersOrders[Customer]Keys repeatOne sideCustomersCustomers[Customer]Each key oncePivot over the modelOrders grouped by Customers › Regionmany → one
A relationship and a pivot across itA relationship links two tables by one key column each. The from side, Orders[Customer], is the many side: many orders share a key. The to side, Customers[Customer], is the one side: each non-blank key is unique. A pivot over the model groups Orders by Customers › Region. A matching key reads that row's cell; no match or a blank key reads blank; a duplicate one-side key shows #VALUE! at the pivot's anchor.Many sideOrdersOrders[Customer]Keys repeatOne sideCustomersCustomers[Customer]Each key oncePivot over the modelOrders grouped by Customers › Regionmany → one

A key with no match, or a blank key, reads blank. A duplicate key on the one side shows #VALUE! at the pivot.

  • from is the many side, the foreign key. Many from rows can have the same key.
  • to is the one side, the key. Each non-blank key is unique in its column.

The names follow the modelRelationship element that Excel writes in its XLSX files.

The Data Model panel beside an Orders table: it lists the Orders and Regions tables, the relationship from Orders Region to Regions Region with every key matching, and the form to edit it.The Data Model panel beside an Orders table: it lists the Orders and Regions tables, the relationship from Orders Region to Regions Region with every key matching, and the form to edit it.
Data › Data Tools › Relationships lists the tables and their relationships, with the status of each key.
Relate orders to customers
import { initSheetwrite, SheetwriteStore, type WorkbookSnapshot } from "@sheetwrite/core";
const snapshot: WorkbookSnapshot = {
workbook: { activeSheet: "data" },
sheets: [
{
id: "data",
name: "Data",
order: 0,
rowCount: 10,
columns: ["A", "B", "C", "D"].map((key) => ({
key,
header: key,
width: 100,
type: "text" as const,
})),
tables: [
{
id: "orders",
name: "Orders",
range: { sheet: "data", start: { row: 0, col: 0 }, end: { row: 4, col: 1 } },
{ id: "customer", name: "Customer" },
{ id: "amount", name: "Amount" },
],
headerRow: true,
totalsRow: false,
},
{
id: "customers",
name: "Customers",
range: { sheet: "data", start: { row: 0, col: 2 }, end: { row: 3, col: 3 } },
{ id: "id", name: "ID" },
{ id: "region", name: "Region" },
],
headerRow: true,
totalsRow: false,
},
],
cells: [],
},
],
};
id: "orders-customers",
from: { table: "orders", column: "customer" },
to: { table: "customers", column: "id" },
});
// The snapshot now holds `workbook.dataModel.relationships`.
if (saved.length !== 1) throw new Error("The relationship was not saved");

Data model panel

In the ribbon, Data › Data Tools › Manage Data Model (its button is labeled Data Model) and Relationships open the data model panel. Data Model appears when the workbook has a table, and Relationships when it has two. The panel loads on first use.

  • Tables lists each table with its column and data-row counts.
  • Relationships lists each relationship as Data[Key] → Lookup[Key] with its key status, and Edit and Delete buttons.
  • The form picks the data table and its key column (the many side), and the lookup table and its key column (the one side).

Each save and delete is one undo step. As Excel's Create Relationship dialog does, the panel does not keep a relationship whose lookup keys repeat or whose key types differ; it shows the reason instead. The API keeps such a relationship, because cells can change later.

Rules

A relationship refers to tables and columns by their stable IDs, so renames, column moves and row or column edits keep it. A new relationship is refused when:

  • a table or a column does not exist,
  • both sides are the same table,
  • another relationship already joins the same two tables,
  • the relationships would form a cycle when direction is ignored. So two related tables always have exactly one path between them.

Removing a table or a column keeps the relationships that refer to it, and undo restores a working model.

A query refresh keeps the ID and column IDs of its output table, so relationships to that table stay. Pivots over related tables update in the same undo step as the refresh.

Undo and collaboration

setRelationship and removeRelationship are document operations. Each change is one undo step, and it is saved and synchronized like other workbook metadata. Two concurrent relationship changes conflict, because the rules apply to the whole model. A pending relationship also conflicts with a concurrent change to one of its tables, or with the removal of a sheet.

Store methodResult
getRelationships()Copies of the saved relationships
setRelationship(relationship)Inserts it, or replaces the one with its ID
removeRelationship(id)Removes it
getRelationshipStatus(id)Whether the keys resolve, checked from the current cells

Key checks

The full engine checks the key values each time getRelationshipStatus is called. Keys match as pivot group keys do: numbers by value, and text without case. A blank key matches nothing.

StatusMeaning
okEvery non-blank one-side key is unique. unmatched counts the non-blank many-side keys that no one-side row holds.
missingTable, missingColumnA table or a column was removed.
typeMismatchThe key at row on side from or to is a boolean, an error on the one side, or a number among text keys (or text among numbers).
duplicateKeyTwo one-side rows hold equal keys. rows are their data-row offsets.
unavailableThe default engine checks no keys, or no relationship has this ID.

A model source names a base table (the many side) and the fields to read. Each field is a table and column ID. A field's table must be the base table or a table the base reaches through many-to-one relationships. Field i in rows, columns, values and filters is model.fields[i].

Amount by customer region
id: "by-region",
name: "By region",
source: {
model: {
table: "orders",
fields: [
{ table: "customers", column: "region" },
{ table: "orders", column: "amount" },
],
},
},
anchor: { row: 0, col: 0 },
rows: [0],
columns: [],
values: [{ field: 1, aggregation: "SUM" }],
});

The result equals a pivot over the table you get by looking up each related column for every base row:

CaseRelated value
The key matches one rowThat row's cell
The key matches no row, or is blankBlank
The many-side key is an errorThat error

A duplicate one-side key or a key type mismatch shows #VALUE! at the anchor. A missing table, column or relationship shows #REF!. The default engine keeps and syncs model pivots but does not compute them. XLSX export keeps them in Sheetwrite metadata, because Excel needs its own data-model binary for a model pivot. It also writes their current output as cells, so Excel shows the values.

In the PivotTable Fields panel, a pivot over a table shows a Related section for each table that its table reaches through relationships. The actions menu of a related column adds it to an area. A table source then becomes a model source whose first fields are the table's columns, so the fields in use keep their indices. This is one undo step. The panel names a related field with its table, for example Customers › Region.

Measures

A measure is a named formula over model columns, saved in workbook.dataModel.measures. A pivot with a model source shows it as a value field, { measure: id }. As in Excel, each value cell, subtotal and grand total calculates the formula again over the base rows that its row items, column items and report filters select. So a total of =SUM(Orders[Amount])/COUNT(Orders[Amount]) is the mean of all its rows, not the sum of the means above it (Measures in Power Pivot, filter context).

Mean amount by customer region
{
op: "setMeasure",
id: "mean-amount",
name: "Mean Amount",
table: "orders",
formula: "=SUM(Orders[Amount])/COUNT(Orders[Amount])",
},
},
],
});
id: "mean-by-region",
name: "Mean by region",
source: {
model: {
table: "orders",
fields: [{ table: "customers", column: "region" }],
},
},
anchor: { row: 0, col: 0 },
rows: [0],
columns: [],
values: [{ measure: "mean-amount" }],
});
  • Names. Every measure belongs to a home table. Measure names are unique ignoring case and differ from the column names of the home table, following the DAX naming requirements. The default header of a measure value is the measure name.
  • Formulas. A measure uses the formula language of cells. Its references are qualified data columns of any model table, such as Orders[Amount], and other measures by name, such as [Mean Amount] (column and measure references). Cell and range references, defined names and the #All, #Headers, #Totals and @ sections are refused, because a measure has no cell position.
  • Related tables. Columns of the base table see the rows of the cell. Another table sees its rows that match the cell's row items, column items and report filters on the fields it reaches through many-to-one relationships, including its own columns. Fields it cannot reach put no condition on it, so filters flow from the one side to the many side only, as with a single cross-filter direction. With orders related to customers, =SUM(Customers[Credit]) changes with a customer region on rows but shows the same value in every order month column.
  • CALCULATE. =CALCULATE(expression, filter, …) evaluates its expression in a changed filter context, following CALCULATE. A filter is either a comparison of one model column with a value, such as Customers[Region]="West" or Orders[Month]>=3 (with =, <>, <, <=, > or >=), which replaces the row items, column items and report filter on that column, or ALL(Table[Column]), which removes them, as ALL does. Filters on other columns stay, and a comparison filters a table only when the table reaches the compared column through many-to-one relationships. The value of a comparison is evaluated once and may not read columns. For example, =SUM(Orders[Amount])/CALCULATE(SUM(Orders[Amount]), ALL(Customers[Region])) is each region's share of its month. Other filters, such as ALL(Table), FILTER or a comparison of two columns, and a CALCULATE inside another show #VALUE!. Excel has no worksheet functions named CALCULATE or ALL, so a cell formula that uses them shows #NAME?.
  • Errors. A formula that does not parse shows #NAME? in its value cells, a missing table or column or an unknown measure name shows #REF!, and an unsupported reference or measures that refer to each other in a cycle show #VALUE!. A formula that returns more than one value also shows #VALUE!. A pivot whose measure was removed shows #REF! at its anchor until undo restores the measure. A cell with no rows of the base table stays blank when the measure reads base-table columns.
  • Renames. Renaming a table or one of its columns with updateTable rewrites the measure formulas that name it, as Excel updates formulas when a table is renamed. Renaming a measure with setMeasure rewrites the [Name] references to it. The rewrite is part of that change, so one undo step reverses both. A measure change made while another collaborator renames a table or column conflicts, and the client reloads the shared document, so both end with the new names.
  • GETPIVOTDATA reads a measure value by the measure name, like any value field.
  • Engines. The full engine keeps no measure results between recomputes.
Store methodResult
getMeasures()Copies of the saved measures
setMeasure(measure)Inserts it, or replaces the one with its ID
removeMeasure(id)Removes it
getMeasureStatus(id)missingTable when the home table is gone, syntax when the formula does not parse, otherwise ok; unavailable for an unknown ID. Checked on every engine; reference errors show in the pivot cells.

setMeasure and removeMeasure are document operations: each change is one undo step, and it is saved, synchronized and restored from version history. Two concurrent data model changes conflict. XLSX export keeps measures and the pivots that use them in Sheetwrite metadata, because Excel keeps measures in its data model part.

Data › Data Tools › Measures opens the Measures panel once the workbook has a table. It lists the measures under their home tables, with a note when a formula does not parse or its table is gone. The form below names a measure, picks its home table and takes its formula; Edit loads a measure into the form, and Remove deletes it. Each save and removal is one undo step. In the PivotTable field list, a pivot over a table or the data model lists the measures it does not show yet under Measures: Table, marked fx, with Add to Values, as Excel's Field List lists measures under their home table. Adding a measure to a pivot over one table makes its source a model source with the same fields.

XLSX files

Relationships and pivots over related tables are saved in Sheetwrite metadata, so a Sheetwrite file opens with them again. Import also reads the active relationships of an Excel data model between worksheet tables, by table and column name, and reports the relationships it cannot use. Sheetwrite does not write an Excel data model, so Excel opens the file without relationships. A model pivot, measures included, is written as the cells of its current output with its number formats, so Excel shows its values without a pivot; Sheetwrite reopens it as a live pivot. See Kept parts and round trips.