: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(bi): copy shelf fields with modifier drag (#115539)
Marius Andra769a2b3d84e9
diff --git a/docs/published/handbook/engineering/data-warehouse.md b/docs/published/handbook/engineering/data-warehouse.mdindex 0a355492b7ec..8f6ef6eef435 100644--- a/docs/published/handbook/engineering/data-warehouse.md+++ b/docs/published/handbook/engineering/data-warehouse.md@@ -88,7 +88,7 @@ Calculated fields appear in Dimensions or Measures with a ƒ indicator. Drag orThe result footer reports distinct dimension groups and non-empty measure values (marks), rather than source-row counts. It excludes total rows and marks truncated results as limited. The grouping labels describe the executed query while edits await Run.-Date and timestamp drops start with daily buckets. A date pill offers **Timeline (keeps the year)** buckets and **Date part (combines years)** choices: month of year, quarter of year, and day of week. **Duplicate** creates an independently configurable shelf field, so the same date can appear at two granularities.+Date and timestamp drops start with daily buckets. A date pill offers **Timeline (keeps the year)** buckets and **Date part (combines years)** choices: month of year, quarter of year, and day of week. **Duplicate** creates an independently configurable shelf field, so the same date can appear at two granularities. Hold **Alt** (**Option** on Mac) while dragging a dimension or measure to copy it to another shelf or position. Copies retain calculations and settings; dropping with the modifier outside a shelf leaves the original in place.**Marks → Color** separates series coloring from shelf grouping. **Single color** preserves every group and series while using one color; switching back restores automatic or customized colors. With two categorical dimensions, select either field to define the colored series and use the other on the axis. Date dimensions remain on the timeline. Automatic uses lines for dates, grouped bars for one or two categorical dimensions, and a number for a single total. Explicit chart choices remain selected. **New worksheet** is available directly in the worksheet header.diff --git a/products/business_intelligence/frontend/BIEditor.tsx b/products/business_intelligence/frontend/BIEditor.tsxindex be1c1716e8a7..3b6bce8fe924 100644--- a/products/business_intelligence/frontend/BIEditor.tsx+++ b/products/business_intelligence/frontend/BIEditor.tsx@@ -64,7 +64,7 @@ export function BIEditor({ tabId, children }: { tabId: string; children: ReactNo}}onDrop={(event) => {const pill = parseBIShelfPillDragData(event.dataTransfer.getData(BI_SHELF_PILL_DRAG_MIME_TYPE))- if (pill?.dragSessionId === dragSessionId) {+ if (pill?.dragSessionId === dragSessionId && !event.altKey) {event.preventDefault()removeFieldFromShelf(pill.shelf, pill.index)}diff --git a/products/business_intelligence/frontend/biEditorLogic.test.ts b/products/business_intelligence/frontend/biEditorLogic.test.tsindex 54355623e545..6de361fcaa20 100644--- a/products/business_intelligence/frontend/biEditorLogic.test.ts+++ b/products/business_intelligence/frontend/biEditorLogic.test.ts@@ -137,7 +137,7 @@ describe('biEditorLogic', () => {logic.unmount()})- it('duplicates a timeline field for date-part grouping and keeps both definitions across reload', () => {+ it.each(['rows', 'columns'] as const)('copies a timeline field into %s without changing the original', (target) => {const logic = biEditorLogic({ tabId: TAB_ID })logic.mount()const date = {@@ -152,20 +152,50 @@ describe('biEditorLogic', () => {editorView: BIEditorView.BI,config: { ...config, rows: [date], columns: [], values: [] },})- logic.actions.duplicateField('rows', 0)- logic.actions.setFieldDatePart('rows', 1, 'month')- expect(logic.values.config.rows[0]).toEqual(date)- expect(logic.values.config.rows[1].id).not.toBe(date.id)+ logic.actions.duplicateField('rows', 0, target, 0)+ logic.actions.setFieldDatePart(target, 0, 'month')+ expect(logic.values.config.rows[target === 'rows' ? 1 : 0]).toEqual(date)+ expect(logic.values.config[target][0].id).not.toBe(date.id)expect(logic.values.generatedQuery?.query).toContain('toStartOfMonth(timestamp)')expect(logic.values.generatedQuery?.query).toContain('toMonth(timestamp)')const restored = parseBIEditorState(BIEditorView.BI, JSON.stringify(logic.values.config))!- expect(restored.config.rows[1].datePart).toBe('month')- logic.actions.setFieldDateBucket('rows', 1, 'year')- expect(logic.values.config.rows[1].datePart).toBeUndefined()+ expect(restored.config[target][0].datePart).toBe('month')+ logic.actions.setFieldDateBucket(target, 0, 'year')+ expect(logic.values.config[target][0].datePart).toBeUndefined()expect(logic.values.generatedQuery?.query).toContain('toStartOfYear(timestamp)')logic.unmount()})+ it('copies a calculated measure at the drop position and keeps filters attached to the original', () => {+ const logic = biEditorLogic({ tabId: TAB_ID })+ logic.mount()+ logic.actions.setAutoUpdate(false)+ const measure = {+ field: { ...eventField, type: 'float' as const },+ aggregation: 'custom' as const,+ customExpression: 'sum(revenue) / nullIf(count(), 0)',+ label: 'Revenue per event',+ shelf: 'columns' as const,+ tableCalculation: { type: 'running_total' as const },+ }+ logic.actions.restoreState({+ editorView: BIEditorView.BI,+ config: {+ ...config,+ values: [measure],+ resultFilters: [{ id: 'original', measureIndex: 0, operator: 'greater_than', value: '5' }],+ topN: { fieldId: eventField.id, count: 5, measureIndex: 0, includeOther: false },+ },+ })+ logic.actions.duplicateField('values', 0, 'values', 0)+ expect(logic.values.config.values).toEqual([measure, measure])+ expect(logic.values.config.resultFilters?.[0].measureIndex).toBe(1)+ expect(logic.values.config.topN?.measureIndex).toBe(1)+ logic.actions.duplicateField('values', 0, 'rows')+ expect(logic.values.config.values).toEqual([measure, measure, { ...measure, shelf: 'rows' }])+ logic.unmount()+ })+it.each(['field', 'blank', 'calculation', 'move'] as const)('preserves a Count result filter when adding the first measure via %s',(method) => {diff --git a/products/business_intelligence/frontend/biEditorLogic.ts b/products/business_intelligence/frontend/biEditorLogic.tsindex 62462f1c9176..cb177ad04e3d 100644--- a/products/business_intelligence/frontend/biEditorLogic.ts+++ b/products/business_intelligence/frontend/biEditorLogic.ts@@ -428,11 +428,15 @@ export interface biEditorLogicActions {}duplicateField: (shelf: Exclude<BIShelf, 'filters'>,- index: number+ index: number,+ toShelf?: BIShelf,+ toIndex?: number) => {fieldId: stringindex: numbershelf: 'columns' | 'rows' | 'values'+ toIndex: number | undefined+ toShelf: BIShelf}editCalculatedField: (field?: BIField) => {field: BIField | undefined@@ -760,7 +764,12 @@ export const biEditorLogic = kea<biEditorLogicType>([setColorBy: (colorBy: string | null | undefined) => ({ colorBy }),editCalculatedField: (field?: BIField) => ({ field }),removeLocalField: (fieldId: string) => ({ fieldId }),- duplicateField: (shelf: Exclude<BIShelf, 'filters'>, index: number) => ({ shelf, index, fieldId: uuid() }),+ duplicateField: (+ shelf: Exclude<BIShelf, 'filters'>,+ index: number,+ toShelf: BIShelf = shelf,+ toIndex?: number+ ) => ({ shelf, index, toShelf, toIndex, fieldId: uuid() }),setFieldDatePart: (shelf: BIShelf, index: number, datePart: BIField['datePart']) => ({shelf,index,@@ -1118,27 +1127,44 @@ export const biEditorLogic = kea<biEditorLogicType>([setFieldDateBucketInConfig(config, shelf, index, dateBucket),setFieldDatePart: (config, { shelf, index, datePart }) =>setFieldDateBucketInConfig(config, shelf, index, null, datePart),- duplicateField: (config, { shelf, index, fieldId }) => {+ duplicateField: (config, { shelf, index, fieldId, toShelf, toIndex }) => {+ let duplicated: BIConfigif (shelf === 'values') {const value = config.values[index]- return value ? { ...config, values: [...config.values, { ...value }] } : config+ if (!value || (toShelf === 'filters' && value.label)) {+ return config+ }+ duplicated = {+ ...config,+ values: [+ ...config.values,+ {+ ...value,+ shelf:+ toShelf === 'values' && toIndex !== undefined+ ? (config.values[toIndex]?.shelf ?? value.shelf)+ : value.shelf,+ },+ ],+ }+ } else {+ const field = config[shelf][index]+ if (!field) {+ return config+ }+ duplicated = {+ ...config,+ [shelf]: [+ ...config[shelf],+ {+ ...field,+ id: fieldId,+ localFieldId: field.localDefinition ? (field.localFieldId ?? field.id) : undefined,+ },+ ],+ }}- const field = config[shelf][index]- return field- ? {- ...config,- [shelf]: [- ...config[shelf],- {- ...field,- id: fieldId,- localFieldId: field.localDefinition- ? (field.localFieldId ?? field.id)- : undefined,- },- ],- }- : config+ return moveFieldInConfig(duplicated, shelf, config[shelf].length, toShelf, toIndex)},setValueCustomExpression: (config, { index, customExpression }) => ({...config,diff --git a/products/business_intelligence/frontend/components/BIPill.tsx b/products/business_intelligence/frontend/components/BIPill.tsxindex 9edeb0c804c2..25fbb0bd1e60 100644--- a/products/business_intelligence/frontend/components/BIPill.tsx+++ b/products/business_intelligence/frontend/components/BIPill.tsx@@ -36,7 +36,7 @@ export const BIPill = forwardRef<HTMLButtonElement, BIPillProps>(function BIPilldraggableonDragStart={(event) => {const dragData: BIShelfPillDragData = { shelf, index, dragSessionId }- event.dataTransfer.effectAllowed = 'move'+ event.dataTransfer.effectAllowed = shelf === 'filters' ? 'move' : 'copyMove'event.dataTransfer.setData(BI_SHELF_PILL_DRAG_MIME_TYPE, JSON.stringify(dragData))}}className={cn(diff --git a/products/business_intelligence/frontend/components/BIShelfDropTarget.tsx b/products/business_intelligence/frontend/components/BIShelfDropTarget.tsxindex 85db71d0f35c..c80624290ffc 100644--- a/products/business_intelligence/frontend/components/BIShelfDropTarget.tsx+++ b/products/business_intelligence/frontend/components/BIShelfDropTarget.tsx@@ -36,8 +36,14 @@ export function BIShelfDropTarget({}): JSX.Element {const { activeDropShelf, dragSessionId } = useValues(biEditorLogic)const { config } = useValues(biEditorLogic)- const { addFieldToShelf, clearActiveDropShelf, moveFieldToShelf, setActiveDropShelf, setMeasureShelf } =- useActions(biEditorLogic)+ const {+ addFieldToShelf,+ clearActiveDropShelf,+ moveFieldToShelf,+ duplicateField,+ setActiveDropShelf,+ setMeasureShelf,+ } = useActions(biEditorLogic)return (<div@@ -56,7 +62,7 @@ export function BIShelfDropTarget({const dragKind = carriesBIDrag(event)if (dragKind) {event.preventDefault()- event.dataTransfer.dropEffect = dragKind === 'pill' ? 'move' : 'copy'+ event.dataTransfer.dropEffect = dragKind === 'pill' && !event.altKey ? 'move' : 'copy'}}}onDragLeave={(event) => {@@ -74,6 +80,10 @@ export function BIShelfDropTarget({if (pill.dragSessionId !== dragSessionId) {return}+ if (event.altKey && pill.shelf !== 'filters') {+ duplicateField(pill.shelf, pill.index, shelf, index)+ return+ }if (pill.shelf === 'values' && shelf === 'values' && index !== undefined) {setMeasureShelf(pill.index, config.values[index]?.shelf ?? 'rows')}