﻿# PivotDataGrid: Top N groups

Limit the number of row and column groups in the Blazor Pivot DataGrid with MaxGroups and combine the remaining items into a single Others group.

Keywords: pivot, crosstab, analysis, aggregation, top, top n, others, maxgroups, limit, datagrid, table

> API reference: [RadzenPivotDataGrid API](https://blazor.radzen.com/docs/api/Radzen.Blazor.RadzenPivotDataGrid-1.md)

## Examples

## Blazor PivotDataGrid Top N groups

Limit the number of row or column groups with `MaxGroups`. The most significant groups are kept and the remaining items are combined into a single group labeled via `OthersLabel`.
Set `MaxGroups` on `RadzenPivotRow` or `RadzenPivotColumn` to keep large datasets readable. Groups are ranked by the sorted aggregate or the first aggregate. Call `Reload()` after changing the value at runtime.

```razor
@inherits DbContextPage

<RadzenCard Variant="Variant.Outlined" class="rz-my-4">
    <RadzenStack Orientation="Orientation.Horizontal" Gap="2rem" AlignItems="AlignItems.Center" Style="margin:1rem" Wrap="FlexWrap.Wrap">
        <RadzenStack Orientation="Orientation.Horizontal" Gap="0.5rem" AlignItems="AlignItems.Center">
            <RadzenLabel Text="Top products per category" Component="RowMaxGroups" />
            <RadzenNumeric @bind-Value=@rowMaxGroups Min="1" Max="20" Name="RowMaxGroups" Style="width:5rem" Change="@(async (int? args) => await grid.Reload())" />
        </RadzenStack>
        <RadzenStack Orientation="Orientation.Horizontal" Gap="0.5rem" AlignItems="AlignItems.Center">
            <RadzenLabel Text="Top countries" Component="ColumnMaxGroups" />
            <RadzenNumeric @bind-Value=@columnMaxGroups Min="1" Max="20" Name="ColumnMaxGroups" Style="width:5rem" Change="@(async (int? args) => await grid.Reload())" />
        </RadzenStack>
    </RadzenStack>
</RadzenCard>

<RadzenPivotDataGrid @ref=@grid Data=@salesData TItem="SalesData" Style="height:700px"
        AllowPaging="true" PageSize="200" AllowFieldsPicking="false"
        ShowColumnsTotals="true" ShowRowsTotals="true" AllowDrillDown="true"
        GridLines="Radzen.DataGridGridLines.Default" AllowAlternatingRows="true">
    <Columns>
        <RadzenPivotColumn TItem="SalesData" Property="ShipCountry" Title="Ship Country" Width="150px"
                           MaxGroups="@columnMaxGroups" OthersLabel="Other countries" />
    </Columns>
    <Rows>
        <RadzenPivotRow TItem="SalesData" Property="CategoryName" Title="Product Category" />
        <RadzenPivotRow TItem="SalesData" Property="ProductName" Title="Product"
                        MaxGroups="@rowMaxGroups" OthersLabel="Other products" />
    </Rows>
    <Aggregates>
        <RadzenPivotAggregate TItem="SalesData" Property="TotalAmount" Title="Total Sales"
                            Aggregate="AggregateFunction.Sum" FormatString="{0:C}" />
    </Aggregates>
</RadzenPivotDataGrid>

@code {
    RadzenPivotDataGrid<SalesData> grid;
    int? rowMaxGroups = 3;
    int? columnMaxGroups = 5;

    private IEnumerable<SalesData> salesData;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        salesData = (from od in dbContext.OrderDetails
                     join o in dbContext.Orders on od.OrderID equals o.OrderID
                     join p in dbContext.Products on od.ProductID equals p.ProductID
                     join c in dbContext.Categories on p.CategoryID equals c.CategoryID
                     select new SalesData
                         {
                             CategoryName = c.CategoryName,
                             ProductName = p.ProductName,
                             ShipCountry = o.ShipCountry,
                             TotalAmount = (od.UnitPrice ?? 0) * (od.Quantity ?? 0) * (1 - (od.Discount ?? 0))
                         });
    }

    public class SalesData
    {
        public string CategoryName { get; set; }
        public string ProductName { get; set; }
        public string ShipCountry { get; set; }
        public double TotalAmount { get; set; }
    }
}
```
