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(insights): cap SQL series breakdown to top 50 values (#61859)

Georgis Andonisdc0ed04e469f
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/SeriesTab.tsx b/frontend/src/queries/nodes/DataVisualization/Components/SeriesTab.tsx
index 0e833b40395b..276d47bc9916 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/SeriesTab.tsx
+++ b/frontend/src/queries/nodes/DataVisualization/Components/SeriesTab.tsx
@@ -1,7 +1,7 @@
import { useActions, useValues } from 'kea'
import { Form } from 'kea-forms'
-import { IconGear, IconPlusSmall, IconTrash } from '@posthog/icons'
+import { IconGear, IconInfo, IconPlusSmall, IconTrash } from '@posthog/icons'
import {
LemonButton,
LemonColorButton,
@@ -15,6 +15,7 @@ import {
LemonTabs,
LemonTag,
Popover,
+ Tooltip,
} from '@posthog/lemon-ui'
import { DataColorToken, getSeriesColor, getSeriesColorPalette } from 'lib/colors'
@@ -27,7 +28,7 @@ import { ChartDisplayType } from '~/types'
import { AxisSeries, dataVisualizationLogic } from '../dataVisualizationLogic'
import { HeatmapSeriesTab } from './Heatmap/HeatmapSeriesTab'
-import { AxisBreakdownSeries, seriesBreakdownLogic } from './seriesBreakdownLogic'
+import { AxisBreakdownSeries, BREAKDOWN_LIMIT_LABEL, seriesBreakdownLogic } from './seriesBreakdownLogic'
import { getAvailableSeriesBreakdownColumns } from './seriesBreakdownUtils'
import { YSeriesLogicProps, YSeriesSettingsTab, ySeriesLogic } from './ySeriesLogic'
@@ -637,14 +638,18 @@ export const SeriesBreakdownSelector = (): JSX.Element => {
onClick={() => deleteSeriesBreakdown()}
/>
</div>
- <div className="ml-4 mt-2">
- {seriesBreakdownData.error ? (
- <div className="text-danger font-bold mt-1">{seriesBreakdownData.error}</div>
- ) : (
- seriesBreakdownData.seriesData.map((series, index) => (
- <BreakdownSeries series={series} index={index} key={`${series.name}-${index}`} />
- ))
- )}
+ <div className="ml-1 mt-2">
+ {seriesBreakdownData.warning ? (
+ <div className="flex items-center gap-1.5 text-warning bg-warning-highlight rounded px-2 py-1 mt-1 mb-2 text-xs font-medium">
+ <span>{BREAKDOWN_LIMIT_LABEL}</span>
+ <Tooltip title={seriesBreakdownData.warning}>
+ <IconInfo className="text-base shrink-0 ml-auto" />
+ </Tooltip>
+ </div>
+ ) : null}
+ {seriesBreakdownData.seriesData.map((series, index) => (
+ <BreakdownSeries series={series} index={index} key={`${series.name}-${index}`} />
+ ))}
</div>
</>
)
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.test.ts b/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.test.ts
index 310d8766fa67..187fbac1f692 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.test.ts
+++ b/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.test.ts
@@ -460,4 +460,53 @@ describe('seriesBreakdownLogic', () => {
},
})
})
+
+ it('caps the breakdown to the top values by total and warns', async () => {
+ logic = seriesBreakdownLogic({ key: testUniqueKey })
+ logic.mount()
+
+ const builtDataNodeLogic = dataNodeLogic({
+ key: testUniqueKey,
+ query: globalQuery.source,
+ })
+ builtDataNodeLogic.mount()
+
+ // 55 distinct breakdown values across 2 days; the higher the index the higher the total.
+ const results: [string, string, number][] = []
+ for (let i = 1; i <= 55; i++) {
+ results.push(['2026-06-04', `Category ${i}`, i])
+ results.push(['2026-06-05', `Category ${i}`, i])
+ }
+ builtDataNodeLogic.actions.setResponse({
+ results,
+ columns: ['event', 'browser', 'total_count'],
+ types: [
+ ['event', 'String'],
+ ['browser', 'Nullable(String)'],
+ ['total_count', 'UInt64'],
+ ],
+ })
+
+ builtDataVizLogic.actions.clearAxis()
+ builtDataVizLogic.actions.updateXSeries('event')
+ builtDataVizLogic.actions.addYSeries('total_count')
+
+ logic.actions.addSeriesBreakdown('browser')
+
+ await expectLogic(logic).toMatchValues({
+ breakdownColumnValues: expect.arrayContaining(['Category 1', 'Category 55']),
+ })
+
+ const { seriesBreakdownData } = logic.values
+ expect(seriesBreakdownData.seriesData).toHaveLength(50)
+ // Highest-total values are kept, lowest are dropped — including the exact
+ // cut-off (totals run 1..55, so the top 50 are Category 6..55).
+ const keptNames = seriesBreakdownData.seriesData.map((series) => series.name)
+ expect(keptNames).toContain('Category 55')
+ expect(keptNames).toContain('Category 6')
+ expect(keptNames).not.toContain('Category 5')
+ expect(keptNames).not.toContain('Category 1')
+ expect(seriesBreakdownData.warning).toContain('top 50')
+ expect(seriesBreakdownData.warning).toContain('55')
+ })
})
diff --git a/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.ts b/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.ts
index 9b5cb420d3ff..b1957da72900 100644
--- a/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.ts
+++ b/frontend/src/queries/nodes/DataVisualization/Components/seriesBreakdownLogic.ts
@@ -28,9 +28,19 @@ export interface BreakdownSeriesData<T> {
xData: AxisSeries<string>
seriesData: AxisBreakdownSeries<T>[]
isUnaggregated?: boolean
- error?: string
+ warning?: string
}
+/** Most series we'll render for a single breakdown before the chart becomes unreadable. */
+export const MAX_BREAKDOWN_SERIES = 50
+
+/** Short label shown when a breakdown is capped — pairs with the detailed warning below. */
+export const BREAKDOWN_LIMIT_LABEL = `Breakdowns are limited to ${MAX_BREAKDOWN_SERIES}`
+
+/** Detailed, count-aware copy shown in the cap's info tooltip. */
+export const getBreakdownLimitWarning = (totalValues: number): string =>
+ `Showing the top ${MAX_BREAKDOWN_SERIES} breakdown values by total out of ${totalValues}. Refine your query to narrow the breakdown.`
+
export const EmptyBreakdownSeries: BreakdownSeriesData<number | null> = {
xData: {
column: {
@@ -47,13 +57,6 @@ export const EmptyBreakdownSeries: BreakdownSeriesData<number | null> = {
seriesData: [],
}
-const createEmptyBreakdownSeriesWithError = (error: string): BreakdownSeriesData<number | null> => {
- return {
- ...EmptyBreakdownSeries,
- error,
- }
-}
-
const parseBreakdownSeriesValue = (value: unknown, selectedYAxis: SelectedYAxis): number | null => {
if (value === undefined || value === null || Number.isNaN(value)) {
return null
@@ -176,10 +179,6 @@ export const seriesBreakdownLogic = kea<seriesBreakdownLogicType>([
return EmptyBreakdownSeries
}
- if (breakdownColumnValues.length > 50) {
- return createEmptyBreakdownSeriesWithError('Too many breakdown values (max 50)')
- }
-
const data: any[] =
'results' in response && Array.isArray(response.results)
? response.results
@@ -187,6 +186,32 @@ export const seriesBreakdownLogic = kea<seriesBreakdownLogicType>([
? response.result
: []
+ const yColumns = yAxis
+ .map((selectedYAxis) => columns.find((n) => n.name === selectedYAxis.name))
+ .filter((column): column is NonNullable<typeof column> => Boolean(column))
+
+ // When there are more breakdown values than we can legibly chart, keep the
+ // most significant ones (highest total across the y-series) rather than an
+ // arbitrary first-N, and tell the user we capped it.
+ let visibleBreakdownValues = breakdownColumnValues
+ let warning: string | undefined
+ if (breakdownColumnValues.length > MAX_BREAKDOWN_SERIES) {
+ const totalByValue = new Map<unknown, number>()
+ for (const row of data) {
+ const rowTotal = yColumns.reduce((sum, yColumn) => {
+ const numeric = Number(row[yColumn.dataIndex])
+ return Number.isNaN(numeric) ? sum : sum + numeric
+ }, 0)
+ const breakdownValue = row[breakdownColumn.dataIndex]
+ totalByValue.set(breakdownValue, (totalByValue.get(breakdownValue) ?? 0) + rowTotal)
+ }
+
+ visibleBreakdownValues = [...breakdownColumnValues]
+ .sort((a, b) => (totalByValue.get(b) ?? 0) - (totalByValue.get(a) ?? 0))
+ .slice(0, MAX_BREAKDOWN_SERIES)
+ warning = getBreakdownLimitWarning(breakdownColumnValues.length)
+ }
+
// xData is unique x values
const xData = Array.from(new Set(data.map((n) => n[xColumn.dataIndex])))
@@ -203,7 +228,7 @@ export const seriesBreakdownLogic = kea<seriesBreakdownLogicType>([
return []
}
- return breakdownColumnValues.map<AxisBreakdownSeries<number | null>>((value) => {
+ return visibleBreakdownValues.map<AxisBreakdownSeries<number | null>>((value) => {
const seriesName = multipleYSeries
? `${selectedYAxis.name} - ${value || '[No value]'}`
: value || '[No value]'
@@ -276,6 +301,7 @@ export const seriesBreakdownLogic = kea<seriesBreakdownLogicType>([
},
seriesData,
isUnaggregated,
+ warning,
}
},
],