Use cases
Drilldowns
Clicking a bar chart applies filters, groupings, and date bucketing.
The stack you choose picks the code on this page. Options marked coming soon can't be chosen yet.
Copies the Quill CLI's install command: curl -fsSL https://quill.co/install.sh | shView as MarkdownThe whole page, with the code for the chosen stack.
Drilldown with table
Filter and grouping UI gives you visibility into drilldown granularity. This example also allows you to toggle metrics and change the column grouping.
import { Chart, isReportFilterRule, useDashboardReports, useReport } from '@quillsql/react';
import SegmentedControl from '../components/segmented-control/segmented-control';
import { Select } from '../components/filter-builder/filter-builder';
import { MetricTable, type MetricRowsPage } from '../components/blocks/metric-explorer/metric-explorer';
import { MorphBlock, SkeletonMorph } from '../components/skeleton-morph/skeleton-morph';
import styles from './DrillDownBlock.module.css';
const T = 'kpi_transactions';
const MEASURES = [
{ value: `sum:${T}::amount`, label: 'Revenue', field: 'amount', format: 'dollar_amount' },
{ value: `count:${T}`, label: 'Transactions', field: 'count', format: 'whole_number' },
];
const SPLITS = ['plan', 'region', 'channel'].map((d) => ({ value: `${T}::${d}`, label: `By ${d}` }));
const BUCKETS = ['day', 'week', 'month'].map((b) => ({ value: b, label: `By ${b}` }));
const PRESETS = ['7', '30', '90'].map((n) => ({ value: n, label: `Last ${n} days` }));
const ROWS_PER_PAGE = 5;
const lastDays = (days: string) => ({ field: `${T}.day`, operator: 'inTheLast', value: [Number(days), 'day'] });
const measure = (value: string) => {
const { field, label, format } = MEASURES.find((m) => m.value === value)!;
return { aggregations: [value], yAxis: { fields: [{ field, label, format }] } };
};
/**
* The KPI drilldown with useReport alone: every control is a setReport or a setFilters on the KPI transactions report.
* The report's rows show under the chart, under the same filters, formatted and paged by Quill.
*/
export function DrillDownBlock() {
const { dashboardReports } = useDashboardReports('KPI breakdowns');
const report = useReport(Object.values(dashboardReports ?? {}).find((r) => r.name === 'KPI transactions')?.id,
{ initialPagination: { pageIndex: 0, pageSize: ROWS_PER_PAGE } });
const { chart, chartLoading, aggregations, groupColumnsBy, dateBucket, filters, filterOptions, setReport, setFilters, table, tableLoading } = report;
const rules = filters.rules.filter(isReportFilterRule);
const days = String(rules.find((r) => r.operator === 'inTheLast')?.value[0] ?? 30);
const category = rules.find((r) => r.operator === 'in')?.value[0];
const split = groupColumnsBy?.split('::')[1];
const categories = filterOptions.find((o) => o.field.endsWith(`.${split}`))?.options ?? [];
const filter = (nextDays: string, value?: string) =>
setFilters({ combinator: 'and', rules: [lastDays(nextDays), ...(value ? [{ field: `${T}.${split}`, operator: 'in', value: [value] }] : [])] });
// The headline is the latest bucket: its segments added up, under the bucket it covers.
const latest = chart?.pivotRows?.at(-1) as Record<string, unknown> | undefined;
const total = Object.entries(latest ?? {}).filter(([k]) => k !== 'day' && !k.startsWith('__')).reduce((sum, [, v]) => sum + Number(v), 0);
const revenue = aggregations[0] === MEASURES[0].value;
const page: MetricRowsPage = {
columns: table.columns,
rows: table.rows,
page: table.pagination.pageIndex + 1, pageCount: Math.max(0, table.getPageCount()), rowCount: table.getRowCount(),
loading: chartLoading || tableLoading, onPageChange: (next) => table.setPageIndex(next - 1),
};
return (
<div className={styles.root}>
<div className={styles.header}>
<SegmentedControl className={styles.measure} label="Measure" options={MEASURES} value={aggregations[0] ?? ''} onValueChange={(v) => setReport(measure(v))} />
<div className={styles.controls}>
<Select label="Break down by" hideLabel options={SPLITS} value={groupColumnsBy}
onChange={(v) => setReport({ groupColumnsBy: v, filters: { combinator: 'and', rules: rules.filter((r) => r.operator !== 'in') } })} />
<Select label="Date bucket" hideLabel options={BUCKETS} value={dateBucket}
onChange={(v) => setReport({ dateBucket: v as 'day' | 'week' | 'month', filters: { combinator: 'and', rules: rules.filter((r) => r.operator !== 'inBucket') } })} />
<Select label="Date range" hideLabel placeholder="Custom" options={PRESETS}
value={rules.some((r) => r.operator === 'inBucket') ? '' : days} onChange={(v) => filter(v, category)} />
<Select label="Category" hideLabel options={[{ value: '', label: `All ${split}s` }, ...categories]}
value={category} onChange={(v) => filter(days, v)} />
</div>
</div>
<div className={styles.body}>
<SkeletonMorph loading={!latest || chartLoading}>
<MorphBlock width={128} height={30}>
<div className={styles.value}>{new Intl.NumberFormat('en-US', revenue ? { style: 'currency', currency: 'USD', notation: 'compact' } : {}).format(total)}</div>
</MorphBlock>
<MorphBlock width={112} height={14} style={{ marginTop: 4 }}><div className={styles.period}>{String(latest?.day)}</div></MorphBlock>
</SkeletonMorph>
<Chart chart={chart} chartLoading={chartLoading} containerStyle={{ width: '100%', height: 260 }}
onClickChartElement={({ rowValue, columnValue }) => columnValue && setReport({
dateBucket: dateBucket === 'month' ? 'week' : 'day',
filters: { combinator: 'and', rules: [...rules,
{ field: `${T}.day`, operator: 'inBucket', value: { start: rowValue, bucket: dateBucket } },
{ field: `${T}.${split}`, operator: 'in', value: [columnValue] }] },
})} />
<MetricTable table={page} caption="KPI transactions" rowsPerPage={ROWS_PER_PAGE} />
</div>
</div>
);
}import { Quill } from '@quillsql/node';
const quill = new Quill({
privateKey: process.env.QUILL_PRIVATE_KEY,
databaseType: 'postgresql',
databaseConnectionString: process.env.DATABASE_URL,
});
// "authenticate" is your existing auth middleware.
app.post('/quill', authenticate, async (req, res) => {
const result = await quill.query({
tenants: [{ tenantField: 'organization_id', tenantIds: [req.user.organizationId] }],
metadata: req.body.metadata,
});
res.send(result);
});