# Category grouping with Pattern Match

Pattern matching provides a powerful way to analyse sequences of transactions but, in some cases, you may consider it too granular to conduct analysis at the individual transaction level - for example, examining a person's twice-weekly supermarket shop, or monthly charitable donation. In such cases it is possible to combine the power of Category Grouping and Pattern Match aggregation techniques to:

- First, group transactions by an ordinal selector variable;
- Next, apply a numeric function to all the transactions in each group;
- Then, define and search for a pattern of interest.

> [!NOTE]
>
> You can carry out this analysis by creating an on the fly aggregation directly within the Expression editor. Alternatively - as described below - you can follow the steps of the Sequence Analysis wizard to generate the expression.

## Scenario

### Identify the longest sequence of years in which a person's total holiday spend strictly increases

To get started:

1. Open the **Sequence Analysis** wizard.
2. Select the **Bookings** table as the transactions to analyse and **People** as the grouping table.

   The grouping variable must be an ordinal selector type. In this example, the Booking Year virtual variable has been created from Booking Date.
3. Drag and drop **Booking Year** as the grouping variable.

   At the bottom of this step, you now have the option to work at the default individual transaction level, or to select a numeric variable and apply a numeric function to the transactions within each group.
4. Select **Apply function to group**.
5. Set **Sum** as the numeric function and drag and drop **Cost** as the function variable, then click **Next**.

   You move directly to the Value Definition step to select the required pattern definition type.
6. Select **Longest Strictly Increasing**.

   No include or exclude lists are necessary in this example, so you can proceed to define the return information.
7. Select to return the **Pattern Length**.
8. **Name** the expression.
9. Review the settings and **Finish**.

You can review and verify results using a data grid:

- **Person URN 422888** has seven holiday bookings in four separate years and the sum of the cost of the bookings in each has increased, returning a longest sequence of 4.
- **Person URN 425342** has six bookings in four separate years. Whilst the sum of the cost increased across three of those years, there was a drop-off in 2023. The longest sequence is 3.
- **Person URN 456030** has three bookings of a similar value in three separate years, but only the second and third demonstrate the required increase in value and, therefore, a pattern length of 2 is returned.

Related topics:

[Pattern match](<https://help.apteco.com/en/faststats/expressions/aggregations-on-the-fly/pattern-match/index.md>)
