: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
import { useActions, useValues } from 'kea'
import { IconCalendar } from '@posthog/icons'
import { LemonSelect, LemonSwitch } from '@posthog/lemon-ui'
import { CompareFilter } from 'lib/components/CompareFilter/CompareFilter'
import { DateFilter } from 'lib/components/DateFilter/DateFilter'
import { getBIComparisonDisabledReason } from '../biComparison'
import { biEditorLogic } from '../biEditorLogic'
import { BI_DATE_OPTIONS } from '../biEditorOptions'
import { isDateTimeBIField } from '../biEditorTypes'
import { getBIDateField } from '../biQueryFilters'
export function BIDateControls(): JSX.Element {
const { config, dataPaneFields } = useValues(biEditorLogic)
const { setDateField, setDateRange, setCompareFilter } = useActions(biEditorLogic)
const dateField = getBIDateField(config)
const dateFields = dataPaneFields.dimensions.filter(isDateTimeBIField)
if (dateField && !dateFields.some((field) => field.expression === dateField.expression)) {
dateFields.unshift(dateField)
}
return (
<div className="flex flex-wrap items-center gap-1" data-attr="bi-editor-date-controls">
<LemonSelect
size="xsmall"
type="tertiary"
value={dateField?.expression ?? null}
options={[
{ value: null, label: 'No date column' },
...dateFields.map((field) => ({ value: field.expression, label: field.name })),
]}
onChange={(expression) =>
setDateField(dateFields.find((field) => field.expression === expression) ?? null)
}
renderButtonContent={(option) => `Date: ${option?.label}`}
tooltip="This column filters source rows by the selected date range. Set grouping granularity on a date pill. Dashboard date ranges override this range."
aria-label="Dashboard date column"
data-attr="bi-editor-date-field"
/>
<DateFilter
size="xsmall"
type="tertiary"
dateFrom={config.dateRange?.date_from ?? 'all'}
dateTo={config.dateRange?.date_to ?? null}
explicitDate={config.dateRange?.explicitDate ?? false}
onChange={(date_from, date_to, explicitDate) => setDateRange({ date_from, date_to, explicitDate })}
disabledReason={!dateField ? 'Select a date column first' : undefined}
dateOptions={BI_DATE_OPTIONS}
allowedRollingDateOptions={['hours', 'days', 'weeks', 'months', 'years']}
allowTimePrecision
allowFixedRangeWithTime
showExplicitDateToggle
makeLabel={(label) => (
<>
<IconCalendar />
<span>{label}</span>
</>
)}
/>
<LemonSwitch
label="Compare previous"
tooltip="Compare with the previous period"
checked={!!config.compareFilter?.compare && !config.compareFilter.compare_to}
onChange={(compare) => setCompareFilter({ compare })}
disabledReason={getBIComparisonDisabledReason(config)}
size="small"
/>
<CompareFilter
size="xsmall"
compareFilter={config.compareFilter}
updateCompareFilter={setCompareFilter}
disableReason={getBIComparisonDisabledReason(config)}
/>
</div>
)
}