Warp

How to build a pivot table (cross-tab report) in Filament

6 min read

Sooner or later someone asks for "sales by region, one column per month, with totals". Filament's tables are built for lists of records, and a cross-tab is a different shape: one row per group, columns that come from the data, and totals in both directions.

This post builds that report in plain Laravel and Filament first, with a query that works on every database Laravel supports. Then it covers the parts that get hard, and how Warp Pivot handles them, if your users need more than a fixed report.

The query: one row per region, one column per month

The usual trick is conditional aggregation: one GROUP BY, and one SUM(CASE …) per column.

Many examples group by MONTH(sold_at). That has two problems. MONTH() is MySQL only (SQLite has strftime, PostgreSQL has extract). And it groups by the month on the database's clock, which is usually UTC. A sale at 10:30 PM on 31 January in New York is stored as 1 February in UTC, and lands in the wrong column.

So compare against month boundaries worked out in PHP, in the business's time zone. It works on SQLite, MySQL, MariaDB and PostgreSQL alike, and it can use an index on sold_at:

use Carbon\CarbonImmutable;

$start = CarbonImmutable::create(2026, 1, 1, 0, 0, 0, 'America/New_York');

$query = Sale::query()
    ->join('stores', 'stores.id', '=', 'sales.store_id')
    ->select('stores.region')
    ->where('sales.sold_at', '>=', $start->utc())
    ->where('sales.sold_at', '<', $start->addYear()->utc())
    ->groupBy('stores.region')
    ->orderBy('stores.region');

foreach (range(0, 11) as $month) {
    $from = $start->addMonths($month);

    $query->selectRaw(
        "sum(case when sales.sold_at >= ? and sales.sold_at < ? then sales.amount else 0 end) as m{$month}",
        [$from->utc(), $from->addMonth()->utc()],
    );
}

$query->selectRaw('sum(sales.amount) as total, count(*) as orders');

$rows = $query->toBase()->get();

This assumes you store times in UTC (app.timezone set to UTC, the Laravel default). The boundaries are midnight in New York, converted to UTC, so each sale lands in the month it was made in, local time. toBase() returns plain rows instead of Sale models, which is what you want for totals.

Two details worth copying:

  • Missing months come back as 0, not as a missing key. That's because the columns come from the loop, not from the data. Grouping by month instead (GROUP BY month) leaves gaps that you then have to fill in PHP.
  • Ratios and averages are calculated from sums, never averaged. The average order value of the whole year is total / orders, not the average of the twelve monthly averages. The same goes for margins. Keep the sums and counts in the query and divide at the end, for each row and for the grand total.

Showing it in a Filament page

A custom page with a Livewire computed property and a Blade table is enough:

namespace App\Filament\Pages;

use Filament\Pages\Page;
use Illuminate\Support\Collection;
use Livewire\Attributes\Computed;

class SalesByMonth extends Page
{
    protected string $view = 'filament.pages.sales-by-month';

    #[Computed]
    public function rows(): Collection
    {
        // the query above
    }
}
<x-filament-panels::page>
    <table class="w-full text-sm">
        <thead>
            <tr>
                <th class="text-start">Region</th>
                @foreach (range(1, 12) as $month)
                    <th class="text-end">{{ \Carbon\Carbon::create(2026, $month)->format('M') }}</th>
                @endforeach
                <th class="text-end">Total</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($this->rows as $row)
                <tr>
                    <td>{{ $row->region }}</td>
                    @foreach (range(0, 11) as $month)
                        <td class="text-end tabular-nums">{{ \Illuminate\Support\Number::currency($row->{'m'.$month}) }}</td>
                    @endforeach
                    <td class="text-end font-semibold tabular-nums">{{ \Illuminate\Support\Number::currency($row->total) }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>
</x-filament-panels::page>

Add the bottom row of column totals with $this->rows->sum('m0') and so on, or with a second query without groupBy. Don't forget to protect the page: canAccess() decides who sees it, and the query should be scoped to the current tenant if your panel has tenancy. A custom query doesn't get Filament's tenant scoping for free.

For a fixed report that a few people look at, this is a fine place to stop.

Where it gets hard

The trouble starts when the report is useful, because then people want to change it:

  • "Can I see it by product instead?" Rows and columns stop being fixed. Every new combination is a new query, a new header, and new tests.
  • "Open West to see its states." Now you need levels (region → state → store), subtotals for each level, and only the open parts queried.
  • "Show me the top 10 products, and the rest as Other." A rank per group, plus a remainder.
  • "What are the sales behind this number?" A click on a cell that lists exactly the records it sums, with the same filters.
  • "Can I get it in Excel?" With merged headers and real numbers, not text.
  • A million rows. Pivot components that run in the browser load the records into JavaScript and add them up there. That works for small data, but the page gets heavier with every row. At some point the aggregation has to happen in the database, and only the totals should be sent to the browser.

Each of these is reasonable on its own. Together they are a product.

The same report with Warp Pivot

Warp Pivot is a pivot table for Filament that does the aggregation in the database with GROUP BY queries, only for the headers that are open, and draws the result on a canvas. The report above becomes:

use Qalainau\FilamentWarpPivot\Dimensions\Date;
use Qalainau\FilamentWarpPivot\Dimensions\Relationship;
use Qalainau\FilamentWarpPivot\Measures\Ratio;
use Qalainau\FilamentWarpPivot\Measures\Sum;
use Qalainau\FilamentWarpPivot\Pages\PivotPage;
use Qalainau\FilamentWarpPivot\Pivot;

class SalesPivot extends PivotPage
{
    public function pivot(Pivot $pivot): Pivot
    {
        return $pivot
            ->query(Sale::class)
            ->rows([
                Relationship::make('store.state.region')->label('Region'),
                Relationship::make('store.state')->label('State'),
            ])
            ->columns([Date::make('sold_at')->month()->timezone('America/New_York')])
            ->values([
                Sum::make('amount')->label('Sales')->money('USD'),
                Ratio::make('margin')->of(Sum::make('profit')->expression('amount - cost'), Sum::make('amount'))->percent(),
            ]);
    }
}

Regions open into states, and subtotals and the margin are calculated from the records (never averages of averages). Users drag fields between rows, columns and filters, save layouts, and double-click a cell to list, search and edit the sales behind it. Export to Excel, CSV and PDF is built in, and Filament's filters, policies and tenancy apply to every query.

On the demo, with 1,000,000 sales rows on MySQL, the first table shows in about 1.2 seconds, and opening every level at once takes about the same, because the source table is scanned once per query whatever is open. The live demo has 200,000 sales to drag around, with no sign-up.

If a fixed table is all you need, the query at the top of this post will serve you well. When users start asking for a different view every week, that's when a pivot component pays for itself.

More from the blog

See who is free next week and book them, in Filament

An agency manager needs 40 hours for a project that starts Monday. The planner shows only the people who have the time, the booking is a drag, and the overbooked project manager gets a fix in two clicks.

Laravel Rust vs Laravel Go vs Laravel PHP: The Complete Real-World Benchmark Guide

We benchmarked Laravel Rust, Laravel Go v0.1.0, and Laravel PHP (FPM, FrankenPHP, Swoole, Workerman) across 8 real workloads on the exact same machine — from 1,000-user spikes and heavy ORM to 100k-row deep pagination. Continuously updated as new tests land.

Work through support tickets without leaving the list, in Filament

A support agent opens a ticket, assigns it, sets it to pending and moves on to the next one with the arrow key. The list stays on the left; the View and Edit pages you already have open on the right.

Filament + AI coding tips, by email

How to fit Warp plugins into your own app and pair them with Claude Code or Cursor: prompts that work, mistakes AI makes, real examples. Once or twice a month. Unsubscribe anytime.

Ask us anything about Warp

No email address or sign-up needed.

We read every question and answer here on the site, usually within a day. Others see your question after we have checked it.

What's stopping you from using Warp's plugins?

One question. We read every answer, and it decides what we build next. It is not shown on the site.

What's stopping you from using Warp's plugins?