> AI agents: this is one page from Mammoth Analytics documentation. The index of all pages as Markdown is https://mammoth.io/docs/llms.txt. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get Markdown.

# Add a running total, rank or largest value per row

> Use Mammoth's Window function for running totals, ranks, Lead and Lag within groups, and Obtain Large or Small Values for the Nth largest of several columns.
>
> Applies to: Where: View › Describe your transformation › Date, Numeric & Text › Window function

**Window function** and **Obtain Large or Small Values** are **Numeric Tools** in **Describe your transformation** › **Date, Numeric & Text**. Both add a column and keep every row, and both re-run every time the pipeline refreshes.

## Add a running total, rank or moving comparison

**Window function** runs a calculation across rows for rankings, running totals and row-to-row comparisons. Group & Pivot gives one summary row per group; a window function adds its result as a new column beside the original rows. A number column's 3-dot menu has shortcuts: **Calculate running total** and **Ranking**.

![Window function panel with Rank selected, a Limit ranking result toggle, Group by and Add Sort controls, and a Place in new number column field](https://mammoth.io/docs/images/20261009_120100_WindowFunction-Rank.webp)

| Setting | What it does |
| --- | --- |
| **Select function** | Sum, Average, Min, Max, Standard Deviation, Variance, Count, Row number, Rank, Dense rank, Percent rank, Cumulative percent rank, N tile, First Value, Last value, Nth value, Lead, Lag. |
| **Select column** | The column to run the function on. Shown for Sum, Average, Min, Max, Standard Deviation, Variance, First Value, Last value, Nth value, Lead and Lag. Count, Row number, Rank, Dense rank, Percent rank, Cumulative percent rank and N tile need no source column. |
| **Value of N** | For N tile (a whole number of 2 or more) and Nth value (1 or more). |
| **Type** | For Sum, Average, Min, Max, Standard Deviation, Variance, Count, First Value, Last value, Nth value and N tile. **Accumulate** (default) builds the result row by row: a Sum of 5, 10 and 15 gives 5, 15 and 30. **Aggregate** works out one summary per group and stamps it on every row: if a group's total is 30, every row in it shows 30. |
| **Offset** | For Lead and Lag: a whole number of 1 or more. |
| **Limit ranking result** | For Row number, Rank and Dense rank. Turn it on to enter **Limit to following no. of rows** and keep only the top ranks. |
| **Group by** | Runs the calculation within each group, for example ranking within each region rather than across all data. |
| **Add Sort** | The order rows are processed in. Shown for Row number, Rank, Dense rank, Percent rank, Cumulative percent rank, Lead and Lag, and for the other functions when Type is **Accumulate**. Row order matters for a running total. |
| **Place in** | A name for the new column, or an existing column to overwrite. |

Mammoth warns when a sum, average or ranking runs over an ID column: "IDs are labels, not amounts, so a running total or ranking of them isn't meaningful." The warning doesn't block **Apply**.

### Rank customers by revenue within each region

1. Open **Window function** and select **Rank**.
2. Click **Group by** and select `region`.
3. Click **Add Sort**, choose `revenue` and sort descending.
4. Name the new column and click **Apply**.

Every customer row gets a rank within its region, not across the whole dataset.

## Find the largest or smallest of several columns in each row

**Obtain Large or Small Values** compares a set of number columns row by row and returns the Nth largest or smallest value.

![Obtain Large or Small Values panel with numeric columns selected, 1st smallest value, and a preview table](https://mammoth.io/docs/images/20260511_175724_obtain_small_large1.webp)

1. In **Select columns**, tick the number columns to compare (at least two). Use the search box to find them.
2. In **Select Nth**, set the rank to return: 1st, 2nd, 3rd and so on.
3. Choose **Largest value** or **Smallest value**.
4. Optional: in **Add a custom value to compare**, add a fixed number. It counts as an extra column and shows as its own column in the preview. It can be a Number parameter (click the **Val** chip).
5. Check the **Preview**, which updates as you go.
6. In **Place in**, name the output column or choose an existing column to overwrite. Click **Apply**.

For example, with three price columns, `supplier_a_price`, `supplier_b_price` and `supplier_c_price`, select all three, choose 1st and **Smallest value**. The result is the cheapest quote in each row, so you can see the best supplier per product.

## Fix a running total that looks out of order

| Symptom | Cause | Fix |
| --- | --- | --- |
| A running total looks out of order | The rows aren't sorted as you expect | Set **Add Sort** in Window function |

## Related

- [Group, pivot and unpivot](https://mammoth.io/docs/learn/clean-and-shape/group-and-pivot/): one summary row per group instead of a window.
- [Calculate with formulas](https://mammoth.io/docs/learn/clean-and-shape/formulas/): row-by-row Math Function calculations.
- [Work with text and JSON](https://mammoth.io/docs/learn/clean-and-shape/text-and-json/): the Text Tools in the same category.

---
Source: https://mammoth.io/docs/learn/clean-and-shape/running-totals-and-ranks.md · Updated: 2026-11-08
