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

# Creating window measures

> Apply a SQL window function to a measure, such as a running total, rank, or moving average.

A window measure applies a SQL window function to another measure, such as a running total, a rank within a group, a moving average, or the value from a previous row. You can build a window measure in a workbook, or in the model IDE using the [`window` parameter](https://docs.omni.co/modeling/measures/parameters/window).

## Requirements

To create or edit a window measure in a workbook, you need **Restricted Querier**, **Querier**, **Modeler**, or **Connection Admin** permissions.

## Creating a window measure

Window functions apply to measures only.

> [accordion] Create a new window measure
>
> 1. In the field browser of a query tab, hover over a measure and click the  icon that appears.
> 2. Select **Modeling > New window function**.

> [accordion] Edit or duplicate an existing window measure
>
> Hover over an existing window measure and click the  icon, then select **Edit** or **Duplicate**. Duplicating appends "Copy" to the field's label and `_copy` to its field name.

Both options open the window measure drawer.

### Choosing a function

By default, the drawer's **Function** section is in **Pick** mode. Use the dropdown to choose a function.

The descriptions use two terms:

* A partition is a group of rows that the function treats as a separate set. The function starts over in each partition. See [Partition by and order by](#partition-by-and-order-by).
* A frame is the set of rows around the current row that the function calculates over. See [Setting the frame](#setting-the-frame).

| Function | Description |
| - | - |
| Running total | Sum of the rows in the frame |
| Moving average | Average of the rows in the frame |
| Running min | Lowest value in the frame |
| Running max | Highest value in the frame |
| Running count | Count of the rows in the frame |
| Rank | Rank within the partition, with gaps after ties |
| Dense rank | Rank within the partition, without gaps after ties |
| Row number | Row position within the partition |
| Previous value | The value from an earlier row (SQL `LAG`) |
| Next value | The value from a later row (SQL `LEAD`) |
| First value | The first value in the frame |
| Last value | The last value in the frame |

Most functions apply to another measure — use the **Measure** field to choose it. **Previous value** and **Next value** also take a **Rows back or forward** count, for how many rows to look away from the current one.

### Writing SQL directly

If the function you need isn't available in **Pick** mode — for example, an expression over more than one measure — switch the toggle to **Write SQL**. This opens a SQL editor for the expression, without its `OVER` clause; partition, order, and frame stay configured separately, as described below.

Switching back to **Pick** keeps your hand-written SQL as written. The SQL only changes if you then make a change in **Pick** mode, which rewrites it to match.

### Partition by and order by

**Partition by** and **Order by** are shared by both **Pick** and **Write SQL** modes:

* **Partition by**: the dimensions that split rows into separate groups, called partitions. The window function restarts in each partition.
* **Order by**: the dimensions or measures that sort rows within each partition. Add multiple sort fields, and toggle each one between **Asc** and **Desc**.

### Setting the frame

The frame is the set of rows the function uses to calculate the value for each row. It moves with the current row and stays inside the current partition.

**Frame start** and **Frame end** set where the frame begins and ends, relative to the current row: **First row**, **Rows preceding**, **Current row**, **Rows following**, or **Last row**. For example, with a sum ordered by month:

| Frame start | Frame end | Result |
| - | - | - |
| First row | Current row | Running total |
| 2 rows preceding | Current row | Rolling 3-month sum |
| First row | Last row | Partition total on every row |

After you set **Frame start** or **Frame end**, a **Rows**/**Range** toggle appears. **Rows** counts rows. **Range** counts values of the single **Order by** field, so rows that share a value share a frame.

The frame doesn't apply to **Rank**, **Dense rank**, **Row number**, **Previous value**, or **Next value**, so the drawer hides it for those functions.

## Next steps

* [`window` parameter reference](https://docs.omni.co/modeling/measures/parameters/window)
