> ## Documentation Index
> Fetch the complete documentation index at: https://endclose.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Rollup rules

> Match several records on one side to one record on the other, such as charges to a payout or split wires to an invoice, with many-to-one rules.

A rollup rule sums several records on one side of a reconciliation and matches the total to a single record on the other side. Use one when a processor settles many charges as one bank payout, or when a customer pays one invoice in several wires.

In the API a rollup rule is a reconciliation rule with `cardinality: "many_to_one"` and a `rollup` object. For rules that pair one record with one record, see [Matching rules](https://endclose.com/docs/guides/matching-rules.md).

## Prerequisites

* A reconciliation with data streams on Side A and Side B. See [Reconciliations](https://endclose.com/docs/concepts/reconciliations.md).
* A field the grouped records share, such as a payout id or batch id in metadata, or a date field if you group by day.
* For the API, an [API key](https://endclose.com/docs/api-reference/authentication.md) and the reconciliation's id.

## How rollup rules group records

A rollup rule has three parts.

1. **The grouped side.** `rollup.side` picks which side is summed. With `a`, the default, several Side A records match one Side B record. With `b`, several Side B records match one Side A record.
2. **The groups.** `group_by` names the field that forms the groups, and `rollup.group_by_kind` says how to read it:
   * `identifier` puts records with the same value, such as the same `metadata.payout_id`, in one group and compares the group's total amount to the other side.
   * `date` (the default) buckets records by day and walks back from the single record's date, keeping a running total, to find the days that add up to it. The first `group_by` key must be a date field.
3. **The match.** A group matches when its total equals the other record's amount, or comes within `rollup.tolerance` of it.

End Close only makes a rollup match when it's unambiguous: the single record has exactly one candidate group, and that group has exactly one candidate record. Anything else stays unmatched for review.

### Conditions on a rollup rule

`filters` on a rollup rule works differently from a one-to-one rule:

* `amount eq amount` is satisfied by the rollup itself.
* Every other condition must be an `eq` without a tolerance. It keeps a group with a single record that carries the same value, for example `metadata.invoice_number eq metadata.invoice_ref`.
* OR groups, other operators and condition tolerances are refused. To allow a gap between the total and the amount, set `rollup.tolerance`.

In a condition, `key` is always the Side A field and `value` the Side B field, whichever side the rule groups.

### Matching within a variance

`rollup.tolerance` is how far a group total can be from the single record's amount and still match, in cents. When the amounts differ, End Close books the difference as an adjustment record, described "Rule variance", so the match nets to zero. The adjustment lives on the reconciliation's own adjustments stream, not on one of your data streams. Variances apply only when the group and the record are in the same currency.

### Netting debits against credits

By default a rollup adds each record's `amount` without its `direction`. A group of three $100 credits and one $20 debit totals $320, so it never matches a $280 payout.

Set `rollup.net` to `true` when the single record carries the net of the group, as a payment facilitator's payout does: a day of sales (credit) less refunds and chargebacks (debit). The group total is then credits minus debits, and End Close compares it to the single record's signed amount:

* A group that nets to a credit matches a credit record of that amount.
* A group that nets to a debit matches a debit record of that amount, such as a clawback, and never a credit.

Because direction now counts, both sides must use the same convention. If one side records a payout as a debit and the other records the sales as credits, a net rule does not match them.

`rollup.net` works with both `group_by_kind` values, both `side` values and `rollup.tolerance`. A variance on a net rule is booked as a credit adjustment on the side whose signed total is lower.

## Create a rollup rule in the app

**1. Open the reconciliation's rules**

Go to **Reconciliations**, open the reconciliation and choose the **Rules** tab, then **New rule**.

**2. Choose many-to-one**

Pick **Many-to-one**.

**3. Filter (optional)**

Under **1. Filter**, add conditions the single record must share with the group, such as operator equals operator.

**4. Group**

Under **2. Group**, set **Side to group** to **Side A** or **Side B**. Choose **By identifier** to group by an exact value such as a batch id, or **By date** to sum per day. Pick the field in **Group by**.

**5. Net debits against credits (optional)**

Under **3. Match**, turn on **Net debits against credits** when the single record is the net of the group, such as a payout for sales less refunds.

**6. Add a variance or lookback window (optional)**

Choose **+ Allow variance** and enter an amount in dollars to match totals that are slightly off. Choose **+ Add lookback window** to limit how many days back End Close looks for a group.

**7. Save**

Add a description and save the rule.

## Create a rollup rule with the API

### Charges to a payout (group Side A)

Card charges are on Side A, each carrying the payout it settled in as `metadata.payout_id`. Bank deposits are on Side B. This rule sums the charges in each payout and matches the total to one deposit that arrives within five days:

```bash cURL theme={"dark"}
curl -X POST https://api.endclose.com/v1/reconciliation_rules \
  -H "X-API-Key: $ENDCLOSE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "reconciliation_id": "'"$RECONCILIATION_ID"'",
    "description": "Charges in a payout to one bank deposit",
    "cardinality": "many_to_one",
    "group_by": ["metadata.payout_id"],
    "filters": {
      "operand": "and",
      "rules": [{ "key": "amount", "operand": "eq", "value": "amount" }]
    },
    "rollup": {
      "side": "a",
      "group_by_kind": "identifier",
      "date_field": "date",
      "lookback_days": 5
    }
  }'
```

### One payout to its payments (group Side B)

Here the payout is on Side A, for example a ledger entry for each processor payout, and the individual payments are on Side B, each tagged with `metadata.payout_id`. Setting `rollup.side` to `b` sums the payments and matches the total to the payout. The second condition ties each group to the payout whose `external_id` it names, and `tolerance` accepts up to \$1.00 of difference:

```bash cURL theme={"dark"}
curl -X POST https://api.endclose.com/v1/reconciliation_rules \
  -H "X-API-Key: $ENDCLOSE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "reconciliation_id": "'"$RECONCILIATION_ID"'",
    "description": "Payments to the payout they belong to",
    "cardinality": "many_to_one",
    "group_by": ["metadata.payout_id"],
    "filters": {
      "operand": "and",
      "rules": [
        { "key": "amount", "operand": "eq", "value": "amount" },
        { "key": "external_id", "operand": "eq", "value": "metadata.payout_id" }
      ]
    },
    "rollup": {
      "side": "b",
      "group_by_kind": "identifier",
      "tolerance": 100
    }
  }'
```

With `side: "b"`, `group_by` names Side B fields and `rollup.date_field` names a Side A field.

### Sales and refunds to a net payout

Settled transactions are on Side A: sales as credits, refunds and chargebacks as debits, each carrying `metadata.payout_ref`. Payouts are on Side B with the same reference. With `net` set to `true`, the rule matches each payout to its transactions when the payout equals sales minus refunds:

```bash cURL theme={"dark"}
curl -X POST https://api.endclose.com/v1/reconciliation_rules \
  -H "X-API-Key: $ENDCLOSE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "reconciliation_id": "'"$RECONCILIATION_ID"'",
    "description": "Sales less refunds to the net payout",
    "cardinality": "many_to_one",
    "group_by": ["metadata.payout_ref"],
    "filters": {
      "operand": "and",
      "rules": [
        { "key": "metadata.payout_ref", "operand": "eq", "value": "metadata.payout_ref" },
        { "key": "amount", "operand": "eq", "value": "amount" }
      ]
    },
    "rollup": {
      "side": "a",
      "group_by_kind": "identifier",
      "net": true
    }
  }'
```

### Rollup settings

| Field | Required | Description |
| - | - | - |
| `cardinality` | Yes | `many_to_one` |
| `group_by` | Yes | Fields on the grouped side to group by. The first is the primary key; further keys split groups further |
| `rollup.side` | No | `a` (default) or `b`: the side that is summed |
| `rollup.group_by_kind` | No | `date` (default) or `identifier` |
| `rollup.tolerance` | No | Allowed difference between the group total and the amount, in cents |
| `rollup.net` | No | `false` (default) adds amounts without their direction. `true` sums credits minus debits and compares the signed total to the single record's signed amount |
| `rollup.lookback_days` | With `date_field` | How many days of history a group can span |
| `rollup.date_field` | With `lookback_days` | The date field that anchors the lookback window |

Leave out `lookback_days` and `date_field` to match without a time limit. Two rollup rules with the same conditions are not duplicates if they group different sides, group by different fields, or differ in `net`.

## When rollup rules run

Rollup rules run in batch runs. On a continuous reconciliation, End Close starts one a few minutes after an import's records arrive, if the reconciliation has a rollup rule, and also when you save a rule. On a batch reconciliation, choose **Run** in the app or call `POST /reconciliations/{id}/run`. Rules run in priority order with your one-to-one rules, and a record matched by an earlier rule can't join a later group.

## FAQ

> [accordion] Why didn't a payout match even though the charges add up?
>
> Rollup matches must be unambiguous. If two groups add up to the same deposit, or two deposits fit the same group, End Close leaves them unmatched. Add a condition that ties the group to one record, such as a payout id, or narrow the lookback window.

> [accordion] Why didn't a payout match a group that includes refunds?
>
> Without `rollup.net`, the rollup adds refunds to the total instead of subtracting them. Set `rollup.net` to `true`, and check that the payout has the same direction as the group's net.

> [accordion] Can a rollup group records in different currencies?
>
> Yes, if the totals match exactly after conversion to USD. `rollup.tolerance` only applies when the group and the single record are in the same currency.

> [accordion] Where does the variance go?
>
> Into an adjustment record on the match, described "Rule variance" and dated on the single record. It sits on the reconciliation's adjustments stream, so your source data streams are unchanged.

> [accordion] Can I match many records to many records?
>
> Not with a rule. Many-to-many rules aren't available yet. Select the records in the reconciliation and choose **Reconcile** to match them by hand.

## Related

> [card] [Matching rules](https://endclose.com/docs/guides/matching-rules.md)
>
> One-to-one rules, operators, tolerances and transforms.

> [card] [Rules and matches](https://endclose.com/docs/concepts/rules-and-matches.md)
>
> How rules, matches and adjustments fit together.

> [card] [Stripe](https://endclose.com/docs/integrations/stripe.md)
>
> Bring in charges and payouts to reconcile against your bank.

> [card] [Review exceptions](https://endclose.com/docs/guides/review-exceptions.md)
>
> Work through what your rules leave unmatched.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.