Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(bi): pivot tables according to row and column shelves (#122777)

Marius Andrab56add8ba946
diff --git a/docs/published/handbook/engineering/data-warehouse.md b/docs/published/handbook/engineering/data-warehouse.md
index 9c70ec1e24a8..7f91cc7b3f15 100644
--- a/docs/published/handbook/engineering/data-warehouse.md
+++ b/docs/published/handbook/engineering/data-warehouse.md
@@ -123,7 +123,7 @@ The **Marks** card offers table calculations per measure: percent of total, runn
**Analysis → Missing dates** defaults to **Observed points only**. **Show gaps** creates empty buckets that occupy a point in the window but do not contribute to an average. **Fill with zero** makes those buckets contribute zero. Filling runs before calculations, separately for each series and comparison period, across the selected date range. It requires one bucketed date dimension matching the worksheet date column and a bounded range. It is available for project and warehouse tables in tables and time-series charts; direct connections do not support it. Saved worksheets and dashboard date overrides retain these semantics.
-Pivot tables display every measure beneath nested column headers. Expand or collapse a row or column header to move between hierarchy levels. Collapsed values are reaggregated from source rows, including averages and distinct counts. **Subtotals** also shows the intermediate aggregates while expanded. Row and column grand totals retain their separate controls, and total cells keep drill-down scoped to the visible dimensions. Expansion is local to the view; the worksheet's shelves and settings remain unchanged. Tracking records hierarchy expansion with axis and depth, and adds missing-date and full-window settings to the existing worksheet events.
+**Table** automatically uses a pivot layout when both **Rows** and **Columns** contain dimensions. Row members run down the table; column members run across it, with every measure beneath each column header. Move all dimensions onto one shelf to return to a flat table. Period comparisons keep their comparison table layout. Direct database connections keep the flat table layout. Large pivots virtualize rows and columns while retaining grouped headers and the sticky row labels. Pivots support up to 10,000 measure columns; wider results ask you to reduce column dimensions or use Top N. Text-valued custom aggregates retain their values. The explicit **Pivot table** chart uses the same pivot layout. Expand or collapse a row or column header to move between hierarchy levels. Collapsed values are reaggregated from source rows, including averages and distinct counts. **Subtotals** also shows the intermediate aggregates while expanded. Row and column grand totals retain their separate controls, and total cells keep drill-down scoped to the visible dimensions. Expansion is local to the view; the worksheet's shelves and settings remain unchanged. Tracking records hierarchy expansion with axis and depth, and adds missing-date and full-window settings to the existing worksheet events.
**Hierarchies** in the data pane names an ordered set of dimensions, such as Country → State → City. Create a hierarchy with 2–10 levels, then start it on rows or columns. The level list marks the current field and shows the next levels. Selecting a result offers **Drill to** the next level in the current editor worksheet, retaining parent filters, date range, measures, variables, and formatting. The path above the result offers **Drill up**, removing only filters introduced by that drill. Navigation participates in undo/redo and respects Auto update. Changed or removed hierarchy levels disable an incompatible return path. Outside the editor, the action explicitly opens a new worksheet. Leaf values and levels already on a shelf do not offer another drill action; nested pivot expansion remains available for those shelves. Definitions stay with the saved worksheet and survive **Clear sheet**. **Import from worksheet** copies a hierarchy from an accessible saved worksheet using the same table and connection. The copy is independent: edits do not change the source worksheet or create global fields. Deleting a local calculated field removes its uses from hierarchy levels, dropping definitions with fewer than two remaining levels.
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index b89fd286ebba..95ec5a1770fc 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -6857,9 +6857,9 @@ snapshots:
products-business-intelligence-comparison-summary--table--light:
hash: v1.k794b7964.2476db083ec4c26dd53dab30351f38104c63d9578cd30eae620b60e8cc22a438.plfjAxTsZmL_nRDwhmqufs8yWeTX0wXrr0u5Is8_RUw
products-business-intelligence-pivot-table--multiple-measures-and-hierarchies--dark:
- hash: v1.k794b7964.b92a13e09ed110143b68de5e967f20864fae45eabdcf41f3e77284a412ecf2fb.9kqmJUDi_54LfCfI0Hqyjz1r16tSdhFCKz1jtY_9sr8
+ hash: v1.k794b7964.26ebc58171eb340426f9fe220739132bca02f59b8489cb5a0b8a6411f99bb34d.oNv5X9sNKiPmhC_sE607b5P0yHAJju-hcC7higWxX9g
products-business-intelligence-pivot-table--multiple-measures-and-hierarchies--light:
- hash: v1.k794b7964.f3a5839e83ddc9921b021426487c0f0db66fe969bc1369dd799c98bd83c91c19.e4TqncN8iN7TmyLw5M8lgUEjfSjD2LgrVykUPu1Ws48
+ hash: v1.k794b7964.a536391f02b293d2bdef0f6bb873b8daf6848e3c6bd2fd1b12f248f5847f2a4e.L_mE9SUEZfVSdz4aGS_wbCmwOg9CYDJr9fC5-sEH0ZU
products-business-intelligence-scatter--detail-and-color--dark:
hash: v1.k794b7964.10050e03ee37342a8bf6e58db6149ad8a57aa66d32e5353dd9400dee8d055b30.XTp9ixf5AOcS_sZxzXYYoHvn4OT2xNm0UCHS8cQN1dQ
products-business-intelligence-scatter--detail-and-color--light:
@@ -10597,7 +10597,7 @@ snapshots:
scenes-app-data-warehouse-sql-editor--bi-data-source-picker--light:
hash: v1.k794b7964.57c4bdfb1483395ad0024727c66d9a841e3f43613be6bdf02edb23451b1c46d9.FQWCKqDSa6Qxcl_rzfu6jknxmBe9gqJYzJbu2c_D2Bw
scenes-app-data-warehouse-sql-editor--bi-drilldown--dark:
- hash: v1.k794b7964.02ad86b7e4a224ac85cd705e46af5d9f76df87567f8a63c68091d093e624ee8c.3Nc46ISOYpuGCix5aaMoZd0j-U7jzZJPyKT0kGhKnZ0
+ hash: v1.k794b7964.ca510931f3cd674c4f1fbc2a708e97cae10aacd646d7074b5219a2e18f8229bd.F39VENBr9AOYc_8ITg5S_AjAoXYGxsrvSzpdztyPabg
scenes-app-data-warehouse-sql-editor--bi-drilldown--light:
hash: v1.k794b7964.ec73ea266a65215aeb6d010f6431e0c35c3c78f4aee5e78e1f536407cc1d0f1b.SiiJU4ZIC4jZdCp3pvVs1t3UtqVrsq8Bmz5pdpWxhtY
scenes-app-data-warehouse-sql-editor--bi-edit-saved-insight--dark:
@@ -10629,9 +10629,9 @@ snapshots:
scenes-app-data-warehouse-sql-editor--bi-one-click-comparison--light:
hash: v1.k794b7964.0f90c14cd12a3f4a166687b69dde3516674f4152d70b7db4c7ee5e9ec81269f1.KVtNP2Z7u-Zo5FXH_WFGbtwKRetmCGCKZ7fTBGqg7G0
scenes-app-data-warehouse-sql-editor--bi-pivot-totals--dark:
- hash: v1.k794b7964.3fead53beba7744ef51f75934fb366df87f4865202300763dfdc29c01e286259.tcPVPFcYD_61Sb7xFXZ53_cF3fR9mVbkht05a65YCjc
+ hash: v1.k794b7964.a7f2fae9f6e58cc514dc6b4ea7070928c8045be0f863eb86496ec99975f1795a.G3UiEpCP_ElGHUVHgL2bR-eweievpW4WF2HBSyVqF_c
scenes-app-data-warehouse-sql-editor--bi-pivot-totals--light:
- hash: v1.k794b7964.b20188de297f5e04222c25e6b0fee8755ac9229fb9e3a3718453c5bc6e024297.fr8s7bWmCM9K6hJDSWcoMrozT8cgKos2qOJOl5ys-lc
+ hash: v1.k794b7964.d2c7e0bf74821578bb45b1f42ce92d12096b32b1320bb84dd991f9cd6a5b6851.tz00BuUNFzAY78a1Dj2q22bk0NkjHiIg26aZg2qKSlE
scenes-app-data-warehouse-sql-editor--bi-quick-filters--dark:
hash: v1.k794b7964.be74d6e8accc2887e029a462983d1aa4895a64488e5e77558643988d1c5383d0.XDdSVpGEvgCkmdqGEpRkzla9SsQkQSw8jV0Du7HxtJY
scenes-app-data-warehouse-sql-editor--bi-quick-filters--light:
@@ -10649,9 +10649,9 @@ snapshots:
scenes-app-data-warehouse-sql-editor--bi-source-preview-large-sample--light:
hash: v1.k794b7964.ddb42f5556e7e09b1219e54d68f83213e8737ebbde55266a4dd1427d9cab5326.S77Umy6YW9HB314IPWIm5yyYg_G0KpQKpBoG99A-iBE
scenes-app-data-warehouse-sql-editor--bi-table-analysis--dark:
- hash: v1.k794b7964.13e469538d6b0f6d5fb6f5a1d3b0f7c93a08282d843f28e1b54670cc234f1ef3.0LLI_p2z10NDsCrEq3dnR0bM-utFqudI3A5jwdkcARI
+ hash: v1.k794b7964.8e5e14fc64a4db188055d8c1f86db24f7141b3e9bd25bba821070231ea99f53d.nYQuEegEy1vfbcvOZBhML27nhwtWKB6YNpLr5T5ulMg
scenes-app-data-warehouse-sql-editor--bi-table-analysis--light:
- hash: v1.k794b7964.44d69a1bbaccaf0b36c54de60fe8da960486b037d9c463ebd792babe5c06e5e9.fxjYqqNq6B7MDjD9WUXXn2D6PCVSTqWdPMbdlAo44NA
+ hash: v1.k794b7964.222855798a3123f816a945a51c85bc3ba4654e7495bf839b40642279abfcf188.ee_DmapFr84nuFWe5fEF24YDFD2qSppU-8jwlYEc4Xk
scenes-app-data-warehouse-sql-editor--bi-wide-transposed-results--dark:
hash: v1.k794b7964.6a420f7474558367a8c3e4ed5d3fa96f7d51edb3b4f7a6cac4e9af1b5cc0e531.Fcz42DazeV4XO1xHNi5h6puOzCUrftQY5jVIeXZut8k
scenes-app-data-warehouse-sql-editor--bi-wide-transposed-results--light:
diff --git a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.test.tsx b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.test.tsx
index 1ed470c77986..05ea43ee6c0b 100644
--- a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.test.tsx
+++ b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.test.tsx
@@ -37,52 +37,97 @@ describe('LemonTable', () => {
const renderedOrder = (): string[] => screen.getAllByTestId('cell-name').map((el) => el.textContent ?? '')
- it.each([false, true])('windows wide tables horizontally with row virtualization %s', (virtualized) => {
- const height = jest.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockReturnValue(180)
- const width = jest.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockReturnValue(800)
- const columns = Array.from({ length: 1801 }, (_, index) => ({
- title: `Column ${index}`,
- key: `column-${index}`,
- render: (_: unknown, record: { id: number }): string => `${record.id}:${index}`,
- }))
- const data = [{ id: 1 }, { id: 2 }]
- try {
- const { container, rerender } = render(
- <LemonTable
- columns={columns}
- dataSource={data}
- virtualizedColumnWidth={192}
- allowContentScroll
- virtualized={virtualized}
- />
- )
- const table = container.querySelector('table')!
- const scroll = container.querySelector('.ScrollableShadows__inner')!
- expect(table).toHaveAttribute('aria-colcount', '1801')
- expect(screen.getByText('1:0')).toBeVisible()
- expect(container.querySelectorAll('tbody td:not([aria-hidden])').length).toBeLessThan(30)
-
- fireEvent.scroll(scroll, { target: { scrollLeft: 900 * 192 } })
- expect(screen.queryByText('1:0')).not.toBeInTheDocument()
- expect(screen.getByText('2:900').closest('td')).toHaveAttribute('aria-colindex', '901')
- expect(screen.getByText('Column 900').closest('th')).toHaveAttribute('aria-colindex', '901')
- expect(container.querySelectorAll('tbody td:not([aria-hidden])').length).toBeLessThan(30)
-
- fireEvent.scroll(scroll, { target: { scrollLeft: 1801 * 192 - 800 } })
- expect(screen.getByText('1:1800')).toBeVisible()
- expect(screen.getByText('2:1800')).toBeVisible()
-
- rerender(<LemonTable columns={columns.slice(0, 2)} dataSource={data} />)
- expect(table).not.toHaveAttribute('aria-colcount')
- expect(screen.getByText('1:0')).toBeVisible()
- expect(screen.getByText('2:1')).toBeVisible()
- expect(container.querySelectorAll('tbody td')).toHaveLength(4)
- } finally {
- cleanup()
- height.mockRestore()
- width.mockRestore()
+ it.each([false, true].flatMap((virtualized) => [false, true].map((pivot) => ({ virtualized, pivot }))))(
+ 'windows wide tables with $virtualized row virtualization and $pivot pivot headers',
+ ({ virtualized, pivot }) => {
+ const height = jest.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockReturnValue(180)
+ const width = jest.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockReturnValue(800)
+ const columns = Array.from({ length: 1801 }, (_, index) => ({
+ title: `Column ${index}`,
+ key: `column-${index}`,
+ width: index === 0 ? 280 : index % 2 ? 240 : undefined,
+ render: (_: unknown, record: { id: number }): string => `${record.id}:${index}`,
+ }))
+ const totalWidth = columns.reduce((sum, column) => sum + (column.width ?? 192), 0)
+ const data = [{ id: 1 }, { id: 2 }]
+ try {
+ const { container, rerender } = render(
+ <LemonTable
+ columns={columns}
+ dataSource={data}
+ virtualizedColumnWidth={192}
+ firstColumnSticky={pivot}
+ headerRows={
+ pivot
+ ? [
+ [
+ { title: 'Dimensions', colSpan: 1 },
+ ...Array.from({ length: 900 }, (_, index) => ({
+ title: `Group ${index}`,
+ colSpan: 2,
+ })),
+ ],
+ [
+ { title: 'Detail', colSpan: 1 },
+ ...Array.from({ length: 1800 }, (_, index) => ({
+ title: `Member ${index}`,
+ colSpan: 1,
+ })),
+ ],
+ ]
+ : undefined
+ }
+ allowContentScroll
+ virtualized={virtualized}
+ />
+ )
+ const table = container.querySelector('table')!
+ const scroll = container.querySelector('.ScrollableShadows__inner')!
+ expect(table).toHaveAttribute('aria-colcount', '1801')
+ expect(screen.getByText('1:0')).toBeVisible()
+ expect(container.querySelectorAll('tbody td:not([aria-hidden])').length).toBeLessThan(30)
+ expect(table.querySelector('col')).toHaveStyle({ width: '280px' })
+
+ fireEvent.scroll(scroll, {
+ target: {
+ scrollLeft: columns.slice(0, 900).reduce((sum, column) => sum + (column.width ?? 192), 0),
+ },
+ })
+ if (pivot) {
+ expect(screen.getByText('1:0').closest('td')).toHaveClass('LemonTable__cell--sticky')
+ expect(screen.getByText('Group 450')).toHaveAttribute('colspan', '2')
+ const headerRows = table.querySelectorAll('thead tr:not(.LemonTable__loader-row)')
+ const spanCounts = Array.from(headerRows, (row) =>
+ Array.from(row.querySelectorAll('th')).reduce((sum, cell) => sum + cell.colSpan, 0)
+ )
+ expect(new Set(spanCounts).size).toBe(1)
+ expect(headerRows[0].querySelectorAll('th').length).toBeLessThan(15)
+ } else {
+ expect(screen.queryByText('1:0')).not.toBeInTheDocument()
+ }
+ expect(screen.getByText('2:900').closest('td')).toHaveAttribute('aria-colindex', '901')
+ expect(screen.getByText('Column 900').closest('th')).toHaveAttribute('aria-colindex', '901')
+ expect(container.querySelectorAll('tbody td:not([aria-hidden])').length).toBeLessThan(30)
+ expect(
+ Array.from(table.querySelectorAll('col')).reduce((sum, col) => sum + parseFloat(col.style.width), 0)
+ ).toBe(totalWidth)
+
+ fireEvent.scroll(scroll, { target: { scrollLeft: totalWidth - 800 } })
+ expect(screen.getByText('1:1800')).toBeVisible()
+ expect(screen.getByText('2:1800')).toBeVisible()
+
+ rerender(<LemonTable columns={columns.slice(0, 2)} dataSource={data} />)
+ expect(table).not.toHaveAttribute('aria-colcount')
+ expect(screen.getByText('1:0')).toBeVisible()
+ expect(screen.getByText('2:1')).toBeVisible()
+ expect(container.querySelectorAll('tbody td')).toHaveLength(4)
+ } finally {
+ cleanup()
+ height.mockRestore()
+ width.mockRestore()
+ }
}
- })
+ )
it('does not rerender retained virtualized cells when scrolling', () => {
const height = jest
diff --git a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.tsx b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.tsx
index 847e90e22717..1cd3e2d7ba4a 100644
--- a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.tsx
+++ b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.tsx
@@ -20,7 +20,13 @@ import { useColumnWidths } from '../../hooks/useColumnWidths'
import { PaginationAuto, PaginationControl, PaginationManual, usePagination } from '../PaginationControl'
import { Tooltip } from '../Tooltip'
import { BulkSelectionBar } from './BulkSelectionBar'
-import { determineColumnKey, getColumnWidthCap, getStickyColumnInfo } from './columnLayoutUtils'
+import {
+ determineColumnKey,
+ getColumnWidthCap,
+ getStickyColumnInfo,
+ virtualColumnIndex,
+ windowHeaderRow,
+} from './columnLayoutUtils'
import { LemonTableLoader } from './LemonTableLoader'
import { Sorting, SortingIndicator, getNextSorting } from './sorting'
import { TableColumnResizeHandle } from './TableColumnResizeHandle'
@@ -132,9 +138,9 @@ export interface LemonTableProps<T extends Record<string, any>, K extends BulkSe
allowContentScroll?: boolean
/** Requires a bounded scroll container. Expandable rows use the normal rendering path. */
virtualized?: boolean
- /** Default column width in pixels for horizontal virtualization of flat, non-spanning tables.
+ /** Default column width in pixels for horizontal virtualization. Supports headerRows and a sticky first column.
* Columns with a numeric `width` keep that width instead.
- * Grouped, expandable, pinned, selectable, and resizable tables use the normal rendering path. */
+ * Column groups, expandable rows, pinned columns, selection, and resizing use the normal rendering path. */
virtualizedColumnWidth?: number
/**
* Whether the header row stays visible while the page scrolls. The header pins to the scene's scroll
@@ -439,9 +445,7 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio
virtualizedColumnWidth > 0 &&
columnGroups.length === 1 &&
!columnGroups[0].title &&
- !headerRows?.length &&
!expandable &&
- !firstColumnSticky &&
!pinnedColumns?.length &&
!bulkSelection &&
!rowActions &&
@@ -466,28 +470,47 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio
}
}, [virtualizedColumnWidthsKey, columnVirtualizer])
const virtualColumns = columnVirtualizer.getVirtualItems()
- const firstColumnIndex = virtualColumns[0]?.index ?? 0
+ const firstColumnIndex = Math.max(Number(!!firstColumnSticky), virtualColumns[0]?.index ?? 0)
const lastColumnIndex = virtualColumns.at(-1)?.index ?? -1
const columnsStart = virtualColumns[0]?.start ?? 0
const columnsEnd = virtualColumns.at(-1)?.end ?? 0
const columnsTotalSize = columnVirtualizer.getTotalSize()
+ const stickyColumnWidth = firstColumnSticky ? getVirtualizedColumnWidth(visibleColumns[0]) : 0
const columnWindow = useMemo(
() =>
virtualizeColumns
? {
startIndex: firstColumnIndex,
- paddingLeft: columnsStart,
+ endIndex: lastColumnIndex + 1,
+ stickyFirstColumn: !!firstColumnSticky,
+ paddingLeft: Math.max(0, columnsStart - stickyColumnWidth),
paddingRight: Math.max(0, columnsTotalSize - columnsEnd),
}
: undefined,
- [virtualizeColumns, firstColumnIndex, columnsStart, columnsEnd, columnsTotalSize]
+ [
+ virtualizeColumns,
+ firstColumnIndex,
+ lastColumnIndex,
+ firstColumnSticky,
+ columnsStart,
+ columnsEnd,
+ columnsTotalSize,
+ stickyColumnWidth,
+ ]
)
const renderedColumnGroups = useMemo(
() =>
virtualizeColumns
- ? [{ children: visibleColumns.slice(firstColumnIndex, lastColumnIndex + 1) }]
+ ? [
+ {
+ children: [
+ ...(firstColumnSticky ? visibleColumns.slice(0, 1) : []),
+ ...visibleColumns.slice(firstColumnIndex, lastColumnIndex + 1),
+ ],
+ },
+ ]
: columnGroups,
- [virtualizeColumns, visibleColumns, firstColumnIndex, lastColumnIndex, columnGroups]
+ [virtualizeColumns, visibleColumns, firstColumnIndex, lastColumnIndex, columnGroups, firstColumnSticky]
)
const renderedColumns = useMemo(
() => renderedColumnGroups.flatMap((group) => group.children),
@@ -607,23 +630,29 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio
<col style={{ width: tableLayout === 'fixed' ? '3rem' : '1%' }} />
) /* Expand/collapse column */
}
- {!!columnWindow?.paddingLeft && <col style={{ width: columnWindow.paddingLeft }} />}
+ {!!columnWindow?.paddingLeft && !firstColumnSticky && (
+ <col style={{ width: columnWindow.paddingLeft }} />
+ )}
{renderedColumns
.filter((column) => !column.isHidden)
.map((column, index) => (
// eslint-disable-next-line react/forbid-dom-props
- <col
- key={`LemonTable-col-${index}`}
- // width:0 has no effect in auto-layout tables (ignored by Safari).
- // width:1% is a standard workaround to shrink a column to its content.
- style={{
- width: virtualizeColumns
- ? getVirtualizedColumnWidth(column)
- : column.width === 0
- ? '1%'
- : column.width,
- }}
- />
+ <React.Fragment key={`LemonTable-col-${index}`}>
+ <col
+ // width:0 has no effect in auto-layout tables (ignored by Safari).
+ // width:1% is a standard workaround to shrink a column to its content.
+ style={{
+ width: virtualizeColumns
+ ? getVirtualizedColumnWidth(column)
+ : column.width === 0
+ ? '1%'
+ : column.width,
+ }}
+ />
+ {index === 0 && firstColumnSticky && !!columnWindow?.paddingLeft && (
+ <col style={{ width: columnWindow.paddingLeft }} />
+ )}
+ </React.Fragment>
))}
{!!columnWindow?.paddingRight && <col style={{ width: columnWindow.paddingRight }} />}
</colgroup>
@@ -632,16 +661,34 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio
{headerRows?.map((row, rowIndex) => (
<tr key={rowIndex} className="LemonTable__row--grouping">
{isRowExpansionToggleShown && <th className="LemonTable__toggle" />}
- {row.map((cell, cellIndex) => (
- <th
- key={cellIndex}
- colSpan={cell.colSpan}
- scope="colgroup"
- className="LemonTable__boundary"
- >
- {cell.title}
- </th>
+ {!!columnWindow?.paddingLeft && !firstColumnSticky && (
+ <th aria-hidden style={{ padding: 0, border: 0 }} />
+ )}
+ {windowHeaderRow(row, columnWindow).map((cell, cellIndex) => (
+ <React.Fragment key={cell.columnIndex}>
+ <th
+ aria-colindex={cell.columnIndex + 1}
+ colSpan={cell.colSpan}
+ scope="colgroup"
+ className={clsx(
+ 'LemonTable__boundary',
+ firstColumnSticky &&
+ cellIndex === 0 &&
+ 'LemonTable__header--sticky'
+ )}
+ >
+ {cell.title}
+ </th>
+ {cellIndex === 0 &&
+ firstColumnSticky &&
+ !!columnWindow?.paddingLeft && (
+ <th aria-hidden style={{ padding: 0, border: 0 }} />
+ )}
+ </React.Fragment>
))}
+ {!!columnWindow?.paddingRight && (
+ <th aria-hidden style={{ padding: 0, border: 0 }} />
+ )}
</tr>
))}
{columnGroups.some((group) => group.title) && (
@@ -686,7 +733,7 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio
<th className="LemonTable__toggle" />
) /* Expand/collapse */
}
- {!!columnWindow?.paddingLeft && (
+ {!!columnWindow?.paddingLeft && !firstColumnSticky && (
<th aria-hidden style={{ padding: 0, border: 0 }} />
)}
{renderedColumnGroups.flatMap((columnGroup, columnGroupIndex) =>
@@ -708,12 +755,12 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio
const widthCap = getColumnWidthCap(column)
const clipTitle = truncateHeader || !!widthCap
- return (
+ return [
<th
key={`LemonTable-th-${columnGroupIndex}-${columnKey}`}
aria-colindex={
columnWindow
- ? columnWindow.startIndex + columnIndex + 1
+ ? virtualColumnIndex(columnIndex, columnWindow) + 1
: undefined
}
className={clsx(
@@ -901,8 +948,17 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio
onResizeEnd={column.onResizeEnd}
/>
) : null}
- </th>
- )
+ </th>,
+ columnIndex === 0 &&
+ firstColumnSticky &&
+ !!columnWindow?.paddingLeft ? (
+ <th
+ key="virtual-padding-left"
+ aria-hidden
+ style={{ padding: 0, border: 0 }}
+ />
+ ) : null,
+ ]
})
)}
{!!columnWindow?.paddingRight && (
@@ -973,24 +1029,32 @@ export function LemonTable<T extends Record<string, any>, K extends BulkSelectio
.fill(null)
.map((_, rowIndex) => (
<tr key={`LemonTable-tr-${rowIndex} ph-no-capture`}>
- {!!columnWindow?.paddingLeft && (
+ {!!columnWindow?.paddingLeft && !firstColumnSticky && (
<td aria-hidden style={{ padding: 0, border: 0 }} />
)}
{renderedColumnGroups.flatMap((columnGroup, columnGroupIndex) =>
columnGroup.children.map((column, columnIndex) => (
- <td
+ <React.Fragment
key={`LemonTable-td-${columnGroupIndex}-${columnIndex}`}
- className={clsx(
- columnIndex === columnGroup.children.length - 1 &&
- 'LemonTable__boundary',
- firstColumnSticky &&
- columnIndex === 0 &&
- 'LemonTable__cell--sticky',
- column.className
- )}
>
- <LemonSkeleton />
- </td>
+ <td
+ className={clsx(
+ columnIndex === columnGroup.children.length - 1 &&
+ 'LemonTable__boundary',
+ firstColumnSticky &&
+ columnIndex === 0 &&
+ 'LemonTable__cell--sticky',
+ column.className
+ )}
+ >
+ <LemonSkeleton />
+ </td>
+ {columnIndex === 0 &&
+ firstColumnSticky &&
+ !!columnWindow?.paddingLeft && (
+ <td aria-hidden style={{ padding: 0, border: 0 }} />
+ )}
+ </React.Fragment>
))
)}
{!!columnWindow?.paddingRight && (
diff --git a/frontend/src/lib/lemon-ui/LemonTable/TableRow.tsx b/frontend/src/lib/lemon-ui/LemonTable/TableRow.tsx
index 302f2eeaf22a..1342667ed357 100644
--- a/frontend/src/lib/lemon-ui/LemonTable/TableRow.tsx
+++ b/frontend/src/lib/lemon-ui/LemonTable/TableRow.tsx
@@ -6,7 +6,7 @@ import { IconCollapse, IconExpand } from '@posthog/icons'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { More } from 'lib/lemon-ui/LemonButton/More'
-import { getColumnWidthCap, getStickyColumnInfo } from './columnLayoutUtils'
+import { getColumnWidthCap, getStickyColumnInfo, virtualColumnIndex, VirtualColumnWindow } from './columnLayoutUtils'
import { ExpandableConfig, LemonTableColumn, LemonTableColumnGroup, TableCellRepresentation } from './types'
export interface TableRowProps<T extends Record<string, any>> {
@@ -17,7 +17,7 @@ export interface TableRowProps<T extends Record<string, any>> {
rowRibbonColorDetermined: string | null | undefined
rowStatusDetermined: 'highlighted' | 'highlight-new' | null | undefined
columnGroups: LemonTableColumnGroup<T>[]
- columnWindow?: { startIndex: number; paddingLeft: number; paddingRight: number }
+ columnWindow?: VirtualColumnWindow
onRow: ((record: T, index: number) => Omit<HTMLProps<HTMLTableRowElement>, 'key'>) | undefined
expandable: ExpandableConfig<T> | undefined
firstColumnSticky: boolean | undefined
@@ -111,7 +111,9 @@ function TableRowRaw<T extends Record<string, any>>({
)}
</td>
)}
- {!!columnWindow?.paddingLeft && <td aria-hidden style={{ padding: 0, border: 0 }} />}
+ {!!columnWindow?.paddingLeft && !firstColumnSticky && (
+ <td aria-hidden style={{ padding: 0, border: 0 }} />
+ )}
{columnGroups.flatMap((columnGroup, columnGroupIndex) =>
columnGroup.children
.filter((column) => !column.isHidden)
@@ -166,34 +168,40 @@ function TableRowRaw<T extends Record<string, any>>({
const spansColumns = extraCellProps.colSpan !== undefined && extraCellProps.colSpan !== 1
const widthCap = spansColumns ? undefined : getColumnWidthCap(column)
return (
- <td
- key={`col-${columnGroupIndex}-${columnKeyOrIndex}`}
- aria-colindex={columnWindow ? columnWindow.startIndex + columnIndex + 1 : undefined}
- className={clsx(
- columnIndex === 0 && 'LemonTable__boundary',
- // Hold the value on one line, because a capped cell that wraps grows the row
- // taller instead of cropping
- widthCap && 'whitespace-nowrap',
- isSticky && 'LemonTable__cell--sticky',
- isColumnSticky && 'LemonTable__cell--pinned',
- column.align && `text-${column.align}`,
- typeof column.className === 'function'
- ? column.className(value as T[keyof T], record, recordIndex)
- : column.className
+ <React.Fragment key={`col-${columnGroupIndex}-${columnKeyOrIndex}`}>
+ <td
+ aria-colindex={
+ columnWindow ? virtualColumnIndex(columnIndex, columnWindow) + 1 : undefined
+ }
+ className={clsx(
+ columnIndex === 0 && 'LemonTable__boundary',
+ // Hold the value on one line, because a capped cell that wraps grows the row
+ // taller instead of cropping
+ widthCap && 'whitespace-nowrap',
+ isSticky && 'LemonTable__cell--sticky',
+ isColumnSticky && 'LemonTable__cell--pinned',
+ column.align && `text-${column.align}`,
+ typeof column.className === 'function'
+ ? column.className(value as T[keyof T], record, recordIndex)
+ : column.className
+ )}
+ // eslint-disable-next-line react/forbid-dom-props
+ style={{
+ ...(typeof column.style === 'function'
+ ? column.style(value as T[keyof T], record, recordIndex)
+ : column.style),
+ ...(widthCap ? { maxWidth: widthCap } : {}),
+ ...(isColumnSticky ? { left: `${leftPosition}px` } : {}),
+ }}
+ onContextMenu={onCellContextMenuChained}
+ {...restCellProps}
+ >
+ {isTableCellRepresentation(contents) ? contents.children : contents}
+ </td>
+ {columnIndex === 0 && firstColumnSticky && !!columnWindow?.paddingLeft && (
+ <td aria-hidden style={{ padding: 0, border: 0 }} />
)}
- // eslint-disable-next-line react/forbid-dom-props
- style={{
- ...(typeof column.style === 'function'
- ? column.style(value as T[keyof T], record, recordIndex)
- : column.style),
- ...(widthCap ? { maxWidth: widthCap } : {}),
- ...(isColumnSticky ? { left: `${leftPosition}px` } : {}),
- }}
- onContextMenu={onCellContextMenuChained}
- {...restCellProps}
- >
- {isTableCellRepresentation(contents) ? contents.children : contents}
- </td>
+ </React.Fragment>
)
})
)}
diff --git a/frontend/src/lib/lemon-ui/LemonTable/columnLayoutUtils.ts b/frontend/src/lib/lemon-ui/LemonTable/columnLayoutUtils.ts
index 0509af92e5c9..3c1a75e87927 100644
--- a/frontend/src/lib/lemon-ui/LemonTable/columnLayoutUtils.ts
+++ b/frontend/src/lib/lemon-ui/LemonTable/columnLayoutUtils.ts
@@ -1,3 +1,5 @@
+import type { ReactNode } from 'react'
+
// Pure column-layout helpers used by LemonTable internals. Kept separate from
// columnUtils.tsx, whose column factories render TZLabel/ProfilePicture and would
// otherwise drag that graph (including eventUsageLogic and the taxonomy JSON) into
@@ -99,3 +101,37 @@ export function determineColumnKey(column: LemonTableColumn<any, any>, obligatio
}
return columnKey
}
+
+export interface VirtualColumnWindow {
+ startIndex: number
+ endIndex: number
+ paddingLeft: number
+ paddingRight: number
+ stickyFirstColumn: boolean
+}
+
+export function virtualColumnIndex(index: number, window: VirtualColumnWindow): number {
+ return window.stickyFirstColumn && index === 0 ? 0 : window.startIndex + index - Number(window.stickyFirstColumn)
+}
+
+export function windowHeaderRow(
+ row: { title: ReactNode; colSpan: number }[],
+ window?: VirtualColumnWindow
+): { title: ReactNode; colSpan: number; columnIndex: number }[] {
+ let columnIndex = 0
+ return row.flatMap((cell) => {
+ const start = columnIndex
+ columnIndex += cell.colSpan
+ if (!window) {
+ return [{ ...cell, columnIndex: start }]
+ }
+ const visibleStart = Math.max(start, window.startIndex)
+ const visibleEnd = Math.min(columnIndex, window.endIndex)
+ return [
+ ...(window.stickyFirstColumn && start === 0 ? [{ ...cell, colSpan: 1, columnIndex: 0 }] : []),
+ ...(visibleEnd > visibleStart
+ ? [{ ...cell, colSpan: visibleEnd - visibleStart, columnIndex: visibleStart }]
+ : []),
+ ]
+ })
+}
diff --git a/frontend/src/queries/nodes/DataVisualization/DataVisualization.tsx b/frontend/src/queries/nodes/DataVisualization/DataVisualization.tsx
index b569068f9485..4c514768f505 100644
--- a/frontend/src/queries/nodes/DataVisualization/DataVisualization.tsx
+++ b/frontend/src/queries/nodes/DataVisualization/DataVisualization.tsx
@@ -45,6 +45,7 @@ import { BIPaneCharts } from 'products/business_intelligence/frontend/BIPaneChar
import { BIPivotTable } from 'products/business_intelligence/frontend/BIPivotTable'
import { getBIVisualizationSource } from 'products/business_intelligence/frontend/biQueryResults'
import { BIScatterGraph } from 'products/business_intelligence/frontend/BIScatterGraph'
+import { isBIPivotTable } from 'products/business_intelligence/frontend/biTableLayout'
import { HogQLBoldNumber } from 'products/product_analytics/frontend/insights/shared/BoldNumber/BoldNumber'
import { DataNodeLogicProps, dataNodeLogic } from '../DataNode/dataNodeLogic'
@@ -433,7 +434,7 @@ function InternalDataTableVisualization(props: DataTableVisualizationProps): JSX
)
} else if (
query.kind === NodeKind.BIVisualizationNode &&
- (query.config.chartType === ChartDisplayType.TwoDimensionalHeatmap ||
+ (isBIPivotTable(query.config) ||
(query.config.compareFilter?.compare &&
[ChartDisplayType.ActionsTable, ChartDisplayType.BoldNumber, ChartDisplayType.Metric].includes(
effectiveVisualizationType
@@ -446,15 +447,11 @@ function InternalDataTableVisualization(props: DataTableVisualizationProps): JSX
chartSettings,
onInspect: canDrill ? inspect : undefined,
}
- component =
- query.config.chartType === ChartDisplayType.TwoDimensionalHeatmap ? (
- <BIPivotTable {...resultProps} />
- ) : (
- <BIComparisonSummary
- {...resultProps}
- card={effectiveVisualizationType !== ChartDisplayType.ActionsTable}
- />
- )
+ component = isBIPivotTable(query.config) ? (
+ <BIPivotTable {...resultProps} embedded={props.embedded} />
+ ) : (
+ <BIComparisonSummary {...resultProps} card={effectiveVisualizationType !== ChartDisplayType.ActionsTable} />
+ )
} else if (
query.kind === NodeKind.BIVisualizationNode &&
query.config.panes?.fields.length &&
@@ -692,7 +689,8 @@ function InternalDataTableVisualization(props: DataTableVisualizationProps): JSX
/>
{query.kind === NodeKind.BIVisualizationNode &&
- effectiveVisualizationType === ChartDisplayType.ActionsTable && (
+ effectiveVisualizationType === ChartDisplayType.ActionsTable &&
+ !isBIPivotTable(query.config) && (
<LemonButton
size={controlSize}
type="secondary"
diff --git a/products/business_intelligence/frontend/BIAnalysisControls.tsx b/products/business_intelligence/frontend/BIAnalysisControls.tsx
index 415e867c4e46..ec36152313f7 100644
--- a/products/business_intelligence/frontend/BIAnalysisControls.tsx
+++ b/products/business_intelligence/frontend/BIAnalysisControls.tsx
@@ -6,6 +6,7 @@ import { ChartDisplayType } from '~/types'
import { biEditorLogic } from './biEditorLogic'
import { getBIValuePillLabel, isDateTimeBIField } from './biEditorTypes'
+import { isBIPivotTable } from './biTableLayout'
import { getBIMissingDatesDisabledReason } from './biTimeSeries'
import { BIShelfCard } from './components/BIShelfCard'
@@ -109,13 +110,11 @@ export function BIAnalysisControls(): JSX.Element {
<LemonLabel>Totals</LemonLabel>
<LemonCheckbox
labelClassName="text-xs"
- label={
- config.chartType === ChartDisplayType.ActionsTable ? 'Grand total' : 'Row grand totals'
- }
+ label={isBIPivotTable(config) ? 'Row grand totals' : 'Grand total'}
checked={!!config.totals?.rows}
onChange={(rows) => setTotals({ ...config.totals, rows })}
/>
- {config.chartType === ChartDisplayType.TwoDimensionalHeatmap ? (
+ {isBIPivotTable(config) ? (
<LemonCheckbox
labelClassName="text-xs"
label="Column grand totals"
diff --git a/products/business_intelligence/frontend/BIPivotTable.test.tsx b/products/business_intelligence/frontend/BIPivotTable.test.tsx
new file mode 100644
index 000000000000..6c8d9eb18d11
--- /dev/null
+++ b/products/business_intelligence/frontend/BIPivotTable.test.tsx
@@ -0,0 +1,110 @@
+import '@testing-library/jest-dom'
+
+import { cleanup, fireEvent, render, screen } from '@testing-library/react'
+
+import { BIConfig, BIField } from '~/queries/schema/schema-business-intelligence'
+import { initKeaTests } from '~/test/init'
+import { ChartDisplayType } from '~/types'
+
+import { DEFAULT_BI_CONFIG } from './biEditorTypes'
+import { BIPivotTable } from './BIPivotTable'
+
+const field = (name: string): BIField => ({
+ id: name,
+ name,
+ expression: name,
+ type: 'string',
+ source: { table: 'events' },
+})
+
+describe('BIPivotTable', () => {
+ beforeEach(initKeaTests)
+ afterEach(() => {
+ cleanup()
+ jest.restoreAllMocks()
+ })
+
+ it('windows a large sparse pivot on both axes and inspects the last cell', () => {
+ jest.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockImplementation(
+ function (this: HTMLElement): number {
+ return this.tagName === 'TR' ? 36 : 180
+ }
+ )
+ jest.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockReturnValue(800)
+ const config = {
+ ...DEFAULT_BI_CONFIG,
+ chartType: ChartDisplayType.ActionsTable,
+ rows: [field('region')],
+ columns: [field('channel')],
+ values: [
+ { field: field('amount'), aggregation: 'sum' as const },
+ { field: field('event'), aggregation: 'custom' as const, customExpression: 'argMax(event, timestamp)' },
+ ],
+ }
+ const results = [
+ ...Array.from({ length: 600 }, (_, index) => [`Region ${index}`, 'Channel 0', index, 'view']),
+ ...Array.from({ length: 89 }, (_, index) => ['Region 0', `Channel ${index + 1}`, index + 1, 'view']),
+ ['Region 599', 'Channel 89', 59989, 'purchase'],
+ ]
+ const onInspect = jest.fn()
+ const { container } = render(
+ <div style={{ height: 180 }}>
+ <BIPivotTable
+ config={config}
+ columns={['bi_row_region', 'bi_column_channel', 'sum_amount', 'custom_event_2']}
+ results={results}
+ onInspect={onInspect}
+ embedded
+ />
+ </div>
+ )
+ const scroll = container.querySelector('.ScrollableShadows__inner')!
+ expect(screen.getByText('Region 0')).toBeVisible()
+ expect(screen.queryByText('Region 599')).not.toBeInTheDocument()
+ expect(container.querySelectorAll('tbody td').length).toBeLessThan(250)
+
+ fireEvent.scroll(scroll, { target: { scrollLeft: 181 * 192 - 800, scrollTop: 600 * 36 } })
+
+ expect(screen.getByText('Region 599')).toBeVisible()
+ expect(screen.queryByText('Region 0')).not.toBeInTheDocument()
+ expect(screen.getByText('59989')).toBeVisible()
+ const lastCell = screen.getByText('purchase')
+ fireEvent.click(lastCell)
+ expect(onInspect).toHaveBeenCalledWith({
+ bi_row_region: 'Region 599',
+ bi_column_channel: 'Channel 89',
+ sum_amount: 59989,
+ custom_event_2: 'purchase',
+ })
+ expect(container.querySelectorAll('tbody td').length).toBeLessThan(250)
+ })
+ it('keeps the last column reachable at the width limit and rejects wider pivots', () => {
+ jest.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockReturnValue(180)
+ jest.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockReturnValue(800)
+ const config: BIConfig = {
+ ...DEFAULT_BI_CONFIG,
+ chartType: ChartDisplayType.ActionsTable,
+ rows: [field('region')],
+ columns: [field('channel')],
+ values: [
+ { field: field('amount'), aggregation: 'sum' },
+ { field: field('cost'), aggregation: 'sum' },
+ ],
+ }
+ const results = Array.from({ length: 5001 }, (_, index) => ['East', `Channel ${index}`, index, index + 10000])
+ const props = { config, columns: ['bi_row_region', 'bi_column_channel', 'sum_amount', 'sum_cost_2'] }
+ const { container, rerender } = render(<BIPivotTable {...props} results={results.slice(0, 5000)} />)
+ const scroll = container.querySelector('.ScrollableShadows__inner')!
+ fireEvent.scroll(scroll, { target: { scrollLeft: 10001 * 192 - 800 } })
+ expect(screen.getByText('14999')).toBeVisible()
+ expect(container.querySelectorAll('tbody td').length).toBeLessThan(30)
+
+ rerender(<BIPivotTable {...props} results={results} />)
+ expect(
+ screen.getByText(
+ 'This pivot has more than 10,000 measure columns. Reduce the column dimensions or use Top N.'
+ )
+ ).toBeVisible()
+ expect(container.querySelector('table')).not.toBeInTheDocument()
+ })
+})
diff --git a/products/business_intelligence/frontend/BIPivotTable.tsx b/products/business_intelligence/frontend/BIPivotTable.tsx
index 09c8def140f6..2bde00bf4a33 100644
--- a/products/business_intelligence/frontend/BIPivotTable.tsx
+++ b/products/business_intelligence/frontend/BIPivotTable.tsx
@@ -11,13 +11,18 @@ import { getBIFieldPillLabel } from './biEditorTypes'
import { biPivotCellKey, biPivotKey, BIPivotMember, buildBIPivotModel, visibleBIPivotMembers } from './biPivot'
import { formatBIMeasure, getBIResultMeasures } from './biResultPresentation'
+// Virtualization still needs a full-width scroll area, which browsers cap even with only a few cells mounted.
+const MAX_PIVOT_MEASURE_COLUMNS = 10000
+
export function BIPivotTable({
config,
columns,
results,
chartSettings,
onInspect,
+ embedded,
}: {
+ embedded?: boolean
config: BIConfig
columns: string[]
results: unknown[][]
@@ -27,15 +32,18 @@ export function BIPivotTable({
const [collapsedRows, setCollapsedRows] = useState<Set<string>>(new Set())
const [collapsedColumns, setCollapsedColumns] = useState<Set<string>>(new Set())
const model = useMemo(() => buildBIPivotModel(config, columns, results), [config, columns, results])
+ const columnMembers = useMemo(
+ () => new Map(visibleBIPivotMembers(model.columns, new Set(), true).map((member) => [member.key, member])),
+ [model.columns]
+ )
const measures = getBIResultMeasures(config, chartSettings)
- const rows = visibleBIPivotMembers(model.rows, collapsedRows, true)
+ const rows = useMemo(() => visibleBIPivotMembers(model.rows, collapsedRows, true), [model.rows, collapsedRows])
const visibleColumns = visibleBIPivotMembers(model.columns, collapsedColumns, !!config.totals?.subtotals)
- if (
- rows.length * visibleColumns.length * measures.length > 50000 ||
- visibleColumns.length * measures.length > 500
- ) {
+ if (visibleColumns.length * measures.length > MAX_PIVOT_MEASURE_COLUMNS) {
return (
- <LemonBanner type="info">Too many pivot cells to display. Reduce the dimensions or use Top N.</LemonBanner>
+ <LemonBanner type="info">
+ This pivot has more than 10,000 measure columns. Reduce the column dimensions or use Top N.
+ </LemonBanner>
)
}
const toggle = (member: BIPivotMember, side: 'rows' | 'columns'): void => {
@@ -69,15 +77,6 @@ export function BIPivotTable({
<span>{member.label}</span>
)
}
- const findMember = (path: unknown[]): BIPivotMember | undefined => {
- let siblings = model.columns
- let member: BIPivotMember | undefined
- for (let depth = 1; depth <= path.length; depth++) {
- member = siblings.find((item) => item.key === biPivotKey(path.slice(0, depth)))
- siblings = member?.children ?? []
- }
- return member
- }
const headerRows = Array.from({ length: config.columns.length }, (_, depth) => {
const cells: { title: React.ReactNode; colSpan: number; key: string }[] = [
{
@@ -93,7 +92,7 @@ export function BIPivotTable({
if (previous.key === key) {
previous.colSpan += measures.length
} else {
- const member = findMember(path)
+ const member = columnMembers.get(key)
cells.push({
key,
colSpan: measures.length,
@@ -111,9 +110,13 @@ export function BIPivotTable({
return cells
})
return (
- <div className="p-2 w-full min-w-0" data-attr="bi-pivot-table">
+ <div className="h-full w-full min-h-0 min-w-0 flex" data-attr="bi-pivot-table">
<LemonTable<BIPivotMember>
- size="small"
+ className="DataVisualizationTable"
+ embedded={embedded}
+ allowContentScroll
+ virtualized
+ virtualizedColumnWidth={visibleColumns.length * measures.length > 50 ? 192 : undefined}
rowKey="key"
dataSource={rows}
useURLForSorting={false}
@@ -152,12 +155,12 @@ export function BIPivotTable({
size="xsmall"
type="tertiary"
fullWidth
- className="justify-end"
+ noPadding
data-attr="bi-pivot-inspect"
onClick={() => onInspect(record)}
tooltip="Explore this result"
>
- {label}
+ <span className="w-full text-right">{label}</span>
</LemonButton>
) : (
label
diff --git a/products/business_intelligence/frontend/biAnalysis.test.ts b/products/business_intelligence/frontend/biAnalysis.test.ts
index 59531f0a241c..026d40824252 100644
--- a/products/business_intelligence/frontend/biAnalysis.test.ts
+++ b/products/business_intelligence/frontend/biAnalysis.test.ts
@@ -193,10 +193,14 @@ describe('BI analysis queries', () => {
expect(across.query).not.toContain('PARTITION BY bi_column_event')
})
- it.each(['average', 'count_distinct'] as const)('reaggregates %s for Other and totals', (aggregation) => {
+ it.each(
+ [ChartDisplayType.ActionsTable, ChartDisplayType.TwoDimensionalHeatmap].flatMap((chartType) =>
+ (['average', 'count_distinct'] as const).map((aggregation) => ({ chartType, aggregation }))
+ )
+ )('reaggregates $aggregation for Other and totals in $chartType', ({ chartType, aggregation }) => {
const worksheet: BIConfig = {
...config,
- chartType: ChartDisplayType.TwoDimensionalHeatmap,
+ chartType,
values: [{ field: field('properties.amount', 'float'), aggregation }],
topN: { fieldId: 'event', count: 5, measureIndex: 0, includeOther: true },
totals: { rows: true, columns: true, subtotals: true },
@@ -227,7 +231,9 @@ describe('BI analysis queries', () => {
sort: { key: 'rows:number', direction: 'asc' },
})!
expect(result.query).toContain('PARTITION BY bi_grouping = 0')
- expect(result.query).toContain('GROUPING SETS ((bi_row_number, bi_column_event), ())')
+ expect(result.query).toContain(
+ 'GROUPING SETS ((bi_row_number, bi_column_event), (bi_row_number), (bi_column_event), ())'
+ )
expect(result.query).toContain('WHERE bi_grouping = 0 OR bi_rank <= 50')
expect(result.query).toContain('ORDER BY bi_grouping DESC, bi_result.bi_row_number ASC LIMIT 100')
const probe = buildBIQuery(
diff --git a/products/business_intelligence/frontend/biAnalysis.ts b/products/business_intelligence/frontend/biAnalysis.ts
index 95d388434ca1..e94744772a2c 100644
--- a/products/business_intelligence/frontend/biAnalysis.ts
+++ b/products/business_intelligence/frontend/biAnalysis.ts
@@ -4,6 +4,7 @@ import { ChartDisplayType } from '~/types'
import { biComparisonSeries } from './biComparison'
import { limitBIComparisonQuery } from './biComparisonLimit'
+import { isBIPivotTable } from './biTableLayout'
import { buildBIFilledPeriod, getBIMissingDatesDisabledReason } from './biTimeSeries'
export const BI_TABLE_CALCULATIONS: { value: BITableCalculation['type']; label: string }[] = [
@@ -80,7 +81,7 @@ export interface BIAnalysisInput {
export function hasBIAnalysis(config: BIConfig): boolean {
return (
- config.chartType === ChartDisplayType.TwoDimensionalHeatmap ||
+ isBIPivotTable(config) ||
(!!config.missingDates && !getBIMissingDatesDisabledReason(config)) ||
!!config.topN ||
!!config.resultFilters?.length ||
@@ -139,7 +140,7 @@ function groupingSets(config: BIConfig, input: BIAnalysisInput): string[][] {
if (![ChartDisplayType.ActionsTable, ChartDisplayType.TwoDimensionalHeatmap].includes(config.chartType)) {
return sets
}
- const pivot = config.chartType === ChartDisplayType.TwoDimensionalHeatmap
+ const pivot = isBIPivotTable(config)
if (pivot && config.totals?.rows) {
sets.push(rows)
}
@@ -178,7 +179,7 @@ function groupingSets(config: BIConfig, input: BIAnalysisInput): string[][] {
export function buildBIAnalysisQuery(config: BIConfig, input: BIAnalysisInput): string {
const dimensions = [...input.rows, ...input.columns]
- const pivot = config.chartType === ChartDisplayType.TwoDimensionalHeatmap
+ const pivot = isBIPivotTable(config)
const ctes: string[] = []
const topMeasure = config.values.length
? input.measures.find((measure) => measure.value === config.values[config.topN?.measureIndex ?? 0])
diff --git a/products/business_intelligence/frontend/biDrilldown.ts b/products/business_intelligence/frontend/biDrilldown.ts
index 9e50667ee4a1..ebc690b8f628 100644
--- a/products/business_intelligence/frontend/biDrilldown.ts
+++ b/products/business_intelligence/frontend/biDrilldown.ts
@@ -22,6 +22,7 @@ import {
} from './biEditorTypes'
import { isBIConditionGroup, normalizeBIConditionGroup } from './biFilterGroups'
import { getBIMemberLabel } from './biMemberAliases'
+import { isBIPivotTable } from './biTableLayout'
export interface BIDrillSelection {
filters: BIFilter[]
@@ -89,12 +90,7 @@ export function getBIDrillSelection(config: BIConfig, record: Record<string, unk
continue
}
const displayValue = value
- if (
- config.chartType === ChartDisplayType.TwoDimensionalHeatmap ||
- config.totals?.rows ||
- config.totals?.columns ||
- config.totals?.subtotals
- ) {
+ if (isBIPivotTable(config) || config.totals?.rows || config.totals?.columns || config.totals?.subtotals) {
if (value === 'Total') {
continue
}
diff --git a/products/business_intelligence/frontend/biEditorTypes.test.ts b/products/business_intelligence/frontend/biEditorTypes.test.ts
index 474e82ef0ad2..637b60276fff 100644
--- a/products/business_intelligence/frontend/biEditorTypes.test.ts
+++ b/products/business_intelligence/frontend/biEditorTypes.test.ts
@@ -252,16 +252,23 @@ describe('BI editor query generation', () => {
expect(ambiguous.query).not.toContain("AS 'plan.tier'")
})
- it('maps warehouse dates and dashboard properties to unbucketed worksheet fields', () => {
+ it('keeps direct connection tables flat and maps dashboard filters to unbucketed worksheet fields', () => {
const source = { table: 'orders', connectionId: 'example-connection' }
const result = buildBIQuery({
...DEFAULT_BI_CONFIG,
source,
dateField: { ...timestampField, source, expression: 'created_at', dateBucket: 'month' },
dateRange: { date_from: '-30d' },
- rows: [{ ...eventField, source, name: 'plan', expression: 'subscription_plan' }],
+ chartType: ChartDisplayType.ActionsTable,
+ rows: [
+ { ...eventField, source, name: 'plan', expression: 'subscription_plan' },
+ { ...eventField, source, name: 'region', expression: 'region' },
+ ],
+ columns: [{ ...eventField, source, name: 'channel', expression: 'channel' }],
})!
- expect(result.query).toContain("{filters((created_at) AS 'timestamp', (subscription_plan) AS 'plan')}")
+ expect(result.query).toContain("(created_at) AS 'timestamp', (subscription_plan) AS 'plan'")
+ expect(result.query).not.toContain('toJSONString')
+ expect(result.query).not.toContain('GROUPING SETS')
expect(result.node.source.connectionId).toBe(source.connectionId)
expect(result.node.source.filters?.dateRange?.date_from).toBe('-30d')
})
@@ -544,16 +551,21 @@ describe('BI editor query generation', () => {
})
it.each([
- ...([100, 1000, 10000, 50000] as const).map((limit) => ({ limit, repeated: false })),
- { limit: 100 as const, repeated: true },
+ ...([100, 1000, 10000, 50000] as const).map((limit) => ({
+ limit,
+ repeated: false,
+ chartType: ChartDisplayType.TwoDimensionalHeatmap,
+ })),
+ { limit: 100 as const, repeated: true, chartType: ChartDisplayType.TwoDimensionalHeatmap },
+ { limit: 100 as const, repeated: false, chartType: ChartDisplayType.ActionsTable },
])(
- 'maps every BI row and column dimension to pivot table axes with limit $limit (repeated: $repeated)',
- ({ limit, repeated }) => {
+ 'maps every BI row and column dimension to pivot table axes in $chartType with limit $limit (repeated: $repeated)',
+ ({ limit, repeated, chartType }) => {
const secondRow = repeated ? eventField : timestampField
const secondColumn = repeated ? browserField : countryField
const result = buildBIQuery({
source: { table: 'events' },
- chartType: ChartDisplayType.TwoDimensionalHeatmap,
+ chartType,
rows: [eventField, secondRow],
columns: [browserField, secondColumn],
values: [{ field: revenueField, aggregation: 'sum' }],
diff --git a/products/business_intelligence/frontend/biEditorTypes.ts b/products/business_intelligence/frontend/biEditorTypes.ts
index 6bffe356c87f..f95ae8ef891a 100644
--- a/products/business_intelligence/frontend/biEditorTypes.ts
+++ b/products/business_intelligence/frontend/biEditorTypes.ts
@@ -49,6 +49,7 @@ import { localFieldError } from './biLocalFields'
import { getBIMeasureSettings, isBIMeasureSettings, mergeBIMeasureSettings } from './biMeasureSettings'
import { getBIChartFields, getBIPaneFields, isBIPanes } from './biPaneConfig'
import { getBIFiltersPlaceholder, getBIQueryFilters, normalizeBIDates } from './biQueryFilters'
+import { isBIPivotTable } from './biTableLayout'
export enum BIEditorView {
SQL = 'sql',
@@ -1296,7 +1297,7 @@ function needsGeneratedDimensionAliases(dimensions: BIDimension[], hasMeasures:
export function getBIResultDimensions(config: BIConfig): { field: BIField; column: string; position?: number }[] {
const { rowDimensions, columnDimensions } = computeBIQueryParts(config)
const dimensions = [...rowDimensions, ...columnDimensions]
- if (config.chartType === ChartDisplayType.TwoDimensionalHeatmap) {
+ if (isBIPivotTable(config)) {
return [rowDimensions, columnDimensions].flatMap((side, index) =>
side.map(({ field, alias }, position) => ({
field,
@@ -1524,7 +1525,7 @@ export function buildBIQuery(config: BIConfig, probeForMoreRows = false): BIQuer
const generatedDimensionAliases = needsGeneratedDimensionAliases(dimensions, configuredValues.length > 0)
const comparing = !!config.compareFilter?.compare
const dimensionExpressions = dimensions.map(({ field }) => fieldExpression(field))
- const isPivotTable = config.chartType === ChartDisplayType.TwoDimensionalHeatmap
+ const isPivotTable = isBIPivotTable(config)
const hasSeriesBreakdown =
dimensions.length === 2 &&
[
diff --git a/products/business_intelligence/frontend/biPivot.test.ts b/products/business_intelligence/frontend/biPivot.test.ts
index ff24223b6555..c1e94fd0d9ab 100644
--- a/products/business_intelligence/frontend/biPivot.test.ts
+++ b/products/business_intelligence/frontend/biPivot.test.ts
@@ -1,9 +1,11 @@
import { BIConfig, BIField } from '~/queries/schema/schema-business-intelligence'
+import { NodeKind } from '~/queries/schema/schema-general'
import { ChartDisplayType } from '~/types'
import { getBIDrillSelection } from './biDrilldown'
-import { DEFAULT_BI_CONFIG } from './biEditorTypes'
+import { buildBIQuery, getBIResultDimensions, DEFAULT_BI_CONFIG } from './biEditorTypes'
import { biPivotCellKey, buildBIPivotModel, visibleBIPivotMembers } from './biPivot'
+import { isBIPivotTable } from './biTableLayout'
const field = (name: string): BIField => ({
id: name,
@@ -13,48 +15,115 @@ const field = (name: string): BIField => ({
source: { table: 'events' },
})
-it('keeps reaggregated multi-measure hierarchy cells and their drill-down dimensions when collapsed', () => {
+it.each([ChartDisplayType.TwoDimensionalHeatmap, ChartDisplayType.ActionsTable])(
+ 'keeps reaggregated multi-measure hierarchy cells and their drill-down dimensions in %s when collapsed',
+ (chartType) => {
+ const config: BIConfig = {
+ ...DEFAULT_BI_CONFIG,
+ source: { table: 'events' },
+ chartType,
+ rows: [field('region'), field('city')],
+ columns: [field('year'), field('quarter')],
+ totals: { rows: true, columns: true },
+ values: [
+ { field: field('amount'), aggregation: 'average' },
+ { field: field('user'), aggregation: 'count_distinct' },
+ ],
+ }
+ const model = buildBIPivotModel(
+ config,
+ ['bi_rows', 'bi_columns', 'average_amount', 'count_distinct_user_2'],
+ [
+ ['["Europe","Total"]', '["2026","Total"]', 17.5, 3],
+ ['["Europe","Paris"]', '["2026","Q1"]', 10, 2],
+ ['["Europe","Rome"]', '["2026","Q1"]', 20, 2],
+ ['["Total","Total"]', '["Total","Total"]', 17.5, 3],
+ ['["Total (category)",null]', '["2026","Q1"]', 8, 1],
+ ]
+ )
+ const collapsed = visibleBIPivotMembers(model.rows, new Set(['["Europe"]']), true)
+ expect(collapsed.map((row) => row.label)).toEqual(['Europe', 'Total (category)', '(no value)', 'Total'])
+ expect(visibleBIPivotMembers(model.columns, new Set(['["2026"]']), false).map((row) => row.label)).toEqual([
+ '2026',
+ 'Total',
+ ])
+ const record = model.cells.get(biPivotCellKey(['Europe'], ['2026']))!
+ expect(record.average_amount).toBe(17.5)
+ expect(record.count_distinct_user_2).toBe(3)
+ expect(getBIDrillSelection(config, record).filters.map(({ field, value }) => [field.name, value])).toEqual([
+ ['region', 'Europe'],
+ ['year', '2026'],
+ ])
+ const empty = model.cells.get(biPivotCellKey(['Total (category)', null], ['2026', 'Q1']))!
+ expect(getBIDrillSelection(config, empty).filters.map(({ operator, value }) => [operator, value])).toEqual([
+ ['equals', 'Total'],
+ ['is_not_set', ''],
+ ['equals', '2026'],
+ ['equals', 'Q1'],
+ ])
+ }
+)
+
+it('pivots table members by shelf and restores flat results when dimensions return to rows', () => {
+ const region = field('region')
+ const channel = field('channel')
const config: BIConfig = {
...DEFAULT_BI_CONFIG,
- source: { table: 'events' },
- chartType: ChartDisplayType.TwoDimensionalHeatmap,
- rows: [field('region'), field('city')],
- columns: [field('year'), field('quarter')],
- totals: { rows: true, columns: true },
+ source: region.source,
+ chartType: ChartDisplayType.ActionsTable,
+ rows: [region],
+ columns: [channel],
values: [
- { field: field('amount'), aggregation: 'average' },
- { field: field('user'), aggregation: 'count_distinct' },
+ { field: field('amount'), aggregation: 'sum' },
+ { field: field('orders'), aggregation: 'count' },
],
}
- const model = buildBIPivotModel(
- config,
- ['bi_rows', 'bi_columns', 'average_amount', 'count_distinct_user_2'],
- [
- ['["Europe","Total"]', '["2026","Total"]', 17.5, 3],
- ['["Europe","Paris"]', '["2026","Q1"]', 10, 2],
- ['["Europe","Rome"]', '["2026","Q1"]', 20, 2],
- ['["Total","Total"]', '["Total","Total"]', 17.5, 3],
- ['["Total (category)",null]', '["2026","Q1"]', 8, 1],
- ]
+ const results = [
+ ['north', 'direct', 12, 3],
+ ['north', 'partner', 8, 2],
+ ['south', 'direct', 9, 1],
+ ]
+ const columns = [...getBIResultDimensions(config).map(({ column }) => column), 'sum_amount', 'count_orders_2']
+ const model = buildBIPivotModel(config, columns, results)
+ expect(isBIPivotTable(config)).toBe(true)
+ expect(model.rows.map(({ label }) => label)).toEqual(['north', 'south'])
+ expect(model.columns.map(({ label }) => label)).toEqual(['direct', 'partner'])
+ expect(model.cells.get(biPivotCellKey(['north'], ['partner']))).toMatchObject({ sum_amount: 8, count_orders_2: 2 })
+ expect(model.cells.has(biPivotCellKey(['south'], ['partner']))).toBe(false)
+ const swapped = { ...config, rows: [channel], columns: [region] }
+ const transposed = buildBIPivotModel(
+ swapped,
+ [...getBIResultDimensions(swapped).map(({ column }) => column), 'sum_amount', 'count_orders_2'],
+ results.map(([region, channel, ...values]) => [channel, region, ...values])
)
- const collapsed = visibleBIPivotMembers(model.rows, new Set(['["Europe"]']), true)
- expect(collapsed.map((row) => row.label)).toEqual(['Europe', 'Total (category)', '(no value)', 'Total'])
- expect(visibleBIPivotMembers(model.columns, new Set(['["2026"]']), false).map((row) => row.label)).toEqual([
- '2026',
- 'Total',
- ])
- const record = model.cells.get(biPivotCellKey(['Europe'], ['2026']))!
- expect(record.average_amount).toBe(17.5)
- expect(record.count_distinct_user_2).toBe(3)
- expect(getBIDrillSelection(config, record).filters.map(({ field, value }) => [field.name, value])).toEqual([
- ['region', 'Europe'],
- ['year', '2026'],
- ])
- const empty = model.cells.get(biPivotCellKey(['Total (category)', null], ['2026', 'Q1']))!
- expect(getBIDrillSelection(config, empty).filters.map(({ operator, value }) => [operator, value])).toEqual([
- ['equals', 'Total'],
- ['is_not_set', ''],
- ['equals', '2026'],
- ['equals', 'Q1'],
- ])
+ expect(transposed.rows.map(({ label }) => label)).toEqual(['direct', 'partner'])
+ expect(transposed.columns.map(({ label }) => label)).toEqual(['north', 'south'])
+ expect(transposed.cells.get(biPivotCellKey(['partner'], ['north']))).toMatchObject({
+ sum_amount: 8,
+ count_orders_2: 2,
+ })
+ const flat = { ...config, rows: [region, channel], columns: [] }
+ expect(isBIPivotTable(flat)).toBe(false)
+ expect(getBIResultDimensions(flat).map(({ column }) => column)).toEqual(['region', 'channel'])
+ expect(buildBIQuery(flat)?.query).not.toContain('bi_current')
+})
+
+it.each([
+ { chartType: ChartDisplayType.ActionsLineGraph },
+ { columns: [] },
+ { rows: [] },
+ { columns: [field(' ')] },
+ { compareFilter: { compare: true } },
+ { source: { table: 'orders', connectionId: 'example-connection' } },
+ { querySnapshot: { kind: NodeKind.HogQLQuery, query: 'SELECT 1' } },
+] satisfies Partial<BIConfig>[])('keeps non-pivot presentations for %j', (overrides) => {
+ expect(
+ isBIPivotTable({
+ ...DEFAULT_BI_CONFIG,
+ chartType: ChartDisplayType.ActionsTable,
+ rows: [field('region')],
+ columns: [field('channel')],
+ ...overrides,
+ })
+ ).toBe(false)
})
diff --git a/products/business_intelligence/frontend/biQueryResults.test.ts b/products/business_intelligence/frontend/biQueryResults.test.ts
index c40a768f8e1a..4afd9af9d712 100644
--- a/products/business_intelligence/frontend/biQueryResults.test.ts
+++ b/products/business_intelligence/frontend/biQueryResults.test.ts
@@ -28,7 +28,6 @@ const node = (config: BIConfig): BIVisualizationNode => ({
describe('BI visualization data reuse', () => {
test.each([
- ChartDisplayType.ActionsTable,
ChartDisplayType.ScatterPlot,
ChartDisplayType.ActionsLineGraph,
ChartDisplayType.ActionsBarValue,
@@ -68,23 +67,24 @@ describe('BI visualization data reuse', () => {
expect((getBIVisualizationResponse(query, cached) as HogQLQueryResponse).columns).toEqual(expected)
})
- it('keeps data changes and pivot queries distinct, and preserves source execution options', () => {
- const query = node(config)
- const source = getBIVisualizationSource(query)
- expect(
- getBIVisualizationSource(node({ ...config, chartType: ChartDisplayType.TwoDimensionalHeatmap }))
- ).not.toEqual(source)
- expect(
- getBIVisualizationSource(
- node({ ...config, filters: [{ field: config.rows[0], operator: 'equals', value: 'east' }] })
- )
- ).not.toEqual(source)
- expect(getBIVisualizationSource(node({ ...config, limit: 10000 }))).not.toEqual(source)
- query.source.connectionId = 'example-connection'
- query.source.variables = { example: { variableId: 'example', code_name: 'threshold', value: 3 } }
- expect(getBIVisualizationSource(query)).toMatchObject({
- connectionId: 'example-connection',
- variables: query.source.variables,
- })
- })
+ it.each([ChartDisplayType.ActionsTable, ChartDisplayType.TwoDimensionalHeatmap])(
+ 'keeps data changes and %s pivot queries distinct, and preserves source execution options',
+ (chartType) => {
+ const query = node(config)
+ const source = getBIVisualizationSource(query)
+ expect(getBIVisualizationSource(node({ ...config, chartType }))).not.toEqual(source)
+ expect(
+ getBIVisualizationSource(
+ node({ ...config, filters: [{ field: config.rows[0], operator: 'equals', value: 'east' }] })
+ )
+ ).not.toEqual(source)
+ expect(getBIVisualizationSource(node({ ...config, limit: 10000 }))).not.toEqual(source)
+ query.source.connectionId = 'example-connection'
+ query.source.variables = { example: { variableId: 'example', code_name: 'threshold', value: 3 } }
+ expect(getBIVisualizationSource(query)).toMatchObject({
+ connectionId: 'example-connection',
+ variables: query.source.variables,
+ })
+ }
+ )
})
diff --git a/products/business_intelligence/frontend/biResultPresentation.test.ts b/products/business_intelligence/frontend/biResultPresentation.test.ts
new file mode 100644
index 000000000000..349870921f9d
--- /dev/null
+++ b/products/business_intelligence/frontend/biResultPresentation.test.ts
@@ -0,0 +1,20 @@
+import { formatBIMeasure } from './biResultPresentation'
+
+describe('BI measure formatting', () => {
+ it.each([
+ ['purchase', 'purchase'],
+ ['', ''],
+ [true, 'true'],
+ [['view', 'purchase'], '["view","purchase"]'],
+ [{ status: 'ready' }, '{"status":"ready"}'],
+ [null, '—'],
+ [undefined, '—'],
+ [NaN, '—'],
+ ])('preserves aggregate results: %j', (value, expected) => {
+ expect(formatBIMeasure(value)).toBe(expected)
+ })
+
+ it.each([0.125, '0.125'])('retains numeric formatting for %j', (value) => {
+ expect(formatBIMeasure(value, { formatting: { style: 'percent', decimalPlaces: 1 } })).toBe('12.5%')
+ })
+})
diff --git a/products/business_intelligence/frontend/biResultPresentation.ts b/products/business_intelligence/frontend/biResultPresentation.ts
index 9569db90e7ec..046981a199ed 100644
--- a/products/business_intelligence/frontend/biResultPresentation.ts
+++ b/products/business_intelligence/frontend/biResultPresentation.ts
@@ -28,7 +28,11 @@ export function biNumericValue(value: unknown): number | null {
export function formatBIMeasure(value: unknown, settings?: ChartAxis['settings']): string {
const number = biNumericValue(value)
- return number === null
- ? '—'
- : String(formatDataWithSettings(number * (settings?.formatting?.style === 'percent' ? 100 : 1), settings))
+ if (number === null) {
+ if (value === null || value === undefined || typeof value === 'number') {
+ return '—'
+ }
+ return typeof value === 'object' ? JSON.stringify(value) : String(value)
+ }
+ return String(formatDataWithSettings(number * (settings?.formatting?.style === 'percent' ? 100 : 1), settings))
}
diff --git a/products/business_intelligence/frontend/biSceneLogic.test.ts b/products/business_intelligence/frontend/biSceneLogic.test.ts
index 7bae8e9c6c1e..3d9ebf56545f 100644
--- a/products/business_intelligence/frontend/biSceneLogic.test.ts
+++ b/products/business_intelligence/frontend/biSceneLogic.test.ts
@@ -1160,7 +1160,7 @@ describe('biSceneLogic', () => {
})
test.each(['ready', 'failed', 'cancelled', 'missing'] as const)(
- 'handles chart-only edits with %s results',
+ 'reuses chart-only edits and reruns pivot edits with %s results',
async (state) => {
logic.actions.restoreWorksheet(worksheet())
const data = dataNodeLogic({
@@ -1185,9 +1185,12 @@ describe('biSceneLogic', () => {
editor.actions.setChartType(ChartDisplayType.ActionsStackedBar)
await jest.advanceTimersByTimeAsync(500)
expect(run).toHaveBeenCalledTimes(state === 'ready' ? 0 : 1)
- editor.actions.setChartType(ChartDisplayType.ActionsTable)
+ editor.actions.setChartType(ChartDisplayType.ActionsAreaGraph)
await jest.advanceTimersByTimeAsync(500)
expect(run).toHaveBeenCalledTimes(state === 'ready' ? 0 : 1)
+ editor.actions.setChartType(ChartDisplayType.ActionsTable)
+ await jest.advanceTimersByTimeAsync(500)
+ expect(run).toHaveBeenCalledTimes(state === 'ready' ? 1 : 2)
} finally {
jest.useRealTimers()
data.unmount()
@@ -1201,11 +1204,13 @@ describe('biSceneLogic', () => {
editor.actions.setAutoUpdate(false)
const run = jest.spyOn(logic.actions, 'runQuery')
try {
+ editor.actions.setChartType(ChartDisplayType.ActionsAreaGraph)
+ expect(logic.values.lastRunQuery?.display).toBe(ChartDisplayType.ActionsAreaGraph)
editor.actions.setChartType(ChartDisplayType.ActionsTable)
- expect(logic.values.lastRunQuery?.display).toBe(ChartDisplayType.ActionsTable)
+ expect(logic.values.lastRunQuery?.display).toBe(ChartDisplayType.ActionsAreaGraph)
editor.actions.setLimit(10000)
editor.actions.setChartType(ChartDisplayType.ActionsLineGraph)
- expect(logic.values.lastRunQuery?.display).toBe(ChartDisplayType.ActionsTable)
+ expect(logic.values.lastRunQuery?.display).toBe(ChartDisplayType.ActionsAreaGraph)
expect(logic.values.lastRunQuery?.config.limit).toBe(1000)
expect(run).not.toHaveBeenCalled()
} finally {
diff --git a/products/business_intelligence/frontend/biTableLayout.ts b/products/business_intelligence/frontend/biTableLayout.ts
new file mode 100644
index 000000000000..c6566ddd63a6
--- /dev/null
+++ b/products/business_intelligence/frontend/biTableLayout.ts
@@ -0,0 +1,14 @@
+import { BIConfig } from '~/queries/schema/schema-business-intelligence'
+import { ChartDisplayType } from '~/types'
+
+export function isBIPivotTable(config: BIConfig): boolean {
+ return (
+ !config.querySnapshot &&
+ (config.chartType === ChartDisplayType.TwoDimensionalHeatmap ||
+ (config.chartType === ChartDisplayType.ActionsTable &&
+ !config.compareFilter?.compare &&
+ !config.source?.connectionId &&
+ config.rows.some((field) => field.expression.trim() || field.name.trim()) &&
+ config.columns.some((field) => field.expression.trim() || field.name.trim())))
+ )
+}