# Drill-through & export

> Show the records behind any value, export them, and export the pivot itself to a styled Excel workbook, CSV or a plain 2-D array.

Two questions follow every pivot table: "which orders make up this number?" and "can I have this in Excel?". Drill-through answers the first, and the export functions the second.

## Drill-through

Double-click a value, or press <kbd>Enter</kbd> on it, to open a dialog with the records behind that cell. The dialog's title names the measure and the members, for example "Revenue · North › Leeds Trinity · 2026 › Q1", and the records are listed in a Birch UI Grid that users can sort, filter and scroll. The number of records is announced to screen readers.

The records respect every filter in the report, so they are exactly the rows that were aggregated into the cell. Cells on total rows and columns drill into all records of their group.

From the dialog, users can export the records:

| Button | File |
| --- | --- |
| Export CSV | `<fileName>-records.csv` |
| Export Excel | `<fileName>-records.xlsx`, with a header row, frozen headers, an autofilter and dates formatted as `yyyy-mm-dd` |

`fileName` defaults to `pivot-report`. The dialog's grid has floating filters on every column: the CSV export writes the records as the dialog shows them, with its filters and sort applied, while the Excel export always writes every record behind the cell. Both contain every visible source field; derived date parts and hidden fields are left out.

### Drill-through events and code

Every drill-through fires `drillThrough` with the records and the cell's position, so you can show the records your own way, open a detail page or log the request:

```ts
studio.on('drillThrough', ({ records, rowPath, colPath, measure }) => {
  analytics.track('pivot_drill', { rows: rowPath.join(' / '), columns: colPath.join(' / '), measure, count: records.length });
});
```

To get the records without the dialog, call `drillThrough(rowPath, colPath?)` with member keys:

```ts
const leedsQ1 = studio.drillThrough(['North', 'Leeds Trinity'], ['2026', '1']);
const allNorth = studio.drillThrough(['North']);
const everything = studio.drillThrough([]);
```

An empty path means "all members" on that axis, so `[]` returns every record that passes the filters.

Set `drillThrough: false` to turn the dialog off, for example when values are confidential at record level. The `drillThrough` event is not fired either, but `studio.drillThrough()` still works for your own code.

### Cell clicks

Every click on a cell fires `cellClick` with the row and column paths, the measure, the value and the kind of row and column (`'member'`, `'subtotal'` or `'grandTotal'`):

```ts
studio.on('cellClick', ({ rowPath, colPath, measure, value, rowKind }) => {
  if (rowKind === 'member' && value !== null) sidePanel.show({ store: rowPath.at(-1), period: colPath, measure, value });
});
```

## Excel export

The toolbar's **Export Excel** button, or `exportExcel()`, writes a styled `.xlsx` workbook of the pivot:

```ts
const bytes: Uint8Array = await studio.exportExcel({
  fileName: 'store-sales-q2-2026',
  sheetName: 'Q2 sales',
  title: 'Store sales, Q2 2026',
});
```

| Option | Default | Meaning |
| --- | --- | --- |
| `download` | `true` | Save the file; `false` only returns the bytes |
| `fileName` | the widget's `fileName`, or `pivot-report` | File name, `.xlsx` is added |
| `sheetName` | default sheet name | Name of the worksheet |
| `title` | the file name | Workbook title |

The promise resolves with the workbook's bytes, which you can upload or attach instead of downloading:

```ts
const bytes = await studio.exportExcel({ download: false });
await fetch('/api/reports/weekly-store-sales/attachments', {
  method: 'POST',
  headers: { 'content-type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' },
  body: bytes,
});
```

What the workbook contains:

- **Stacked column headers** as merged cells, styled as header rows.
- **Frozen panes** on the header rows and the row label columns, so they stay visible while scrolling.
- **Number formats** on every value column, generated from each measure's [number format](https://docs.birchui.com/pivot-studio/totals-and-formatting.md#number-formats) or taken from its `excel` code. Values stay numbers, so formulas work on them.
- **Outline levels** on rows, one per row level (up to 7, Excel's limit), so users can collapse and expand groups with Excel's outline buttons. Members that are collapsed in the widget are exported as collapsed groups, and the outline summary row is placed above or below each group to match `subtotalPosition`.
- **Bold subtotals with a fill**, and a filled **grand total row** with a top border.
- **Conditional formats**: color scale and threshold backgrounds, text colors and bold are applied as cell styles.

Every row member is included, also those collapsed on screen, so the file holds the full detail.

> **Note**
>
> Excel export uses the Birch UI Excel Engine. A Pivot Studio license covers it; you do not need a separate Excel Engine or Grid Pro license.

## CSV export

The toolbar's **Export CSV** button downloads the pivot as CSV. `exportCsv()` returns the text and downloads it only when you ask:

```ts
const csv = studio.exportCsv({ formatted: true, separator: ';' });
studio.exportCsv({ download: true, fileName: 'store-sales-week-27', headers: 'flat' });
```

| Option | Default | Meaning |
| --- | --- | --- |
| `formatted` | `false` | Write formatted values (`£12,400`) instead of numbers |
| `headers` | `'stacked'` | `'stacked'` writes one header row per column level; `'flat'` writes one row with combined headers |
| `separator` | `','` | Field separator |
| `download` | `false` | Also save a file |
| `fileName` | the widget's `fileName`, or `pivot-report` | File name, `.csv` is added |

The CSV has one column per row field followed by one column per value column, and contains the rows as they are shown, so collapsed members appear as their subtotal row only. Downloaded files start with a UTF-8 byte order mark so spreadsheet apps detect the encoding. Text that starts with `=`, `+`, `-` or `@` is prefixed with a quote, so a member name cannot run as a formula in a spreadsheet.

## A 2-D array for other tools

`getPivotMatrix(options?)` returns the same content as the CSV as an array of rows, for a chart library, a PDF generator or an e-mail template:

```ts
const matrix = studio.getPivotMatrix({ formatted: false, headers: 'flat' });
const [header, ...rows] = matrix;
```

Header rows come first, then one row per table row: the row labels, then the values (numbers, or `null` for empty cells, unless `formatted: true`).

For full control, `getView()` returns the computed `PivotView` with rows, columns, header spans and per-cell `value()`, `formatted()` and `cellStyle()` accessors, and the exported `pivotViewToMatrix()`, `buildPivotWorkbook()` and `buildRecordsWorkbook()` helpers build matrices and workbooks from it.

## Exporting without the UI

The headless engine produces the same matrix and workbook on a server, in a worker or in a scheduled job. See [Performance](https://docs.birchui.com/pivot-studio/performance.md#the-headless-engine).
