> For the complete documentation index, see [llms.txt](https://cortex-docs.paloaltonetworks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/lag.md).

# lag

Use the lag() function to access data from a preceding row in the same result set, enabling comparisons between the current row and a previous one.

## Syntax

lag(\<field>, \<offset>, \<default\_value>)

## Parameters

| Name           | Type    | Required | Description                                                                                                                                      |
| -------------- | ------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| field          | any     | Yes      | The field from which to extract the value.                                                                                                       |
| offset         | integer | No       | The number of rows back from the current row from which to obtain a value. The default is 1.                                                     |
| default\_value | any     | No       | The value to return if the offset points to a row that does not exist (for example, before the first row of the partition). The default is NULL. |

## Returns

The lag() function returns the value of the specified field from the row that is the specified number of rows before the current row. The data type matches the data type of the input field.

## Usage Notes

* The lag() function is an analytic function and must be used within the windowcomp stage.
* The chronological or logical order of the records is determined by the order by clause within the over() partition statement.
* This function is frequently used to calculate differences between consecutive events, such as the time elapsed between logins or the change in file size between versions.

## Examples

### Example 1: Identify consecutive processes

**Goal**: Check the previous process name that was executed.

**XQL Code**:

```sql
dataset \= xdr_data
| filter action_process_image_name != null
| windowcomp lag(action_process_image_name) as previous_process over(partition by agent_hostname order by _time asc)
| fields agent_hostname, action_process_image_name, previous_process, _time
| sort asc agent_hostname , _time
```

**Explanation**: The windowcomp partition operates within the parameters defined in the over() partition statement. The query organizes the data to evaluate by agent\_hostname according to the ascending chronological order of its creation time. The results returned in the new field previous\_process provide the process image name of the previous event for each agent hostname. Because this calculation utilizes the windowcomp stage, all lines of the dataset are returned along with this new field value.

**Output**:

| AGENT\_HOSTNAME | ACTION\_PROCESS\_IMAGE\_NAME | PREVIOUS\_PROCESS | \_TIME                 |
| --------------- | ---------------------------- | ----------------- | ---------------------- |
| mac-agent-1     | test-process-1               | null              | Mar 26th 2026 09:26:07 |
| mac-agent-1     | test-process-2               | test-process-1    | Mar 26th 2026 09:26:08 |
| mac-agent-1     | test-process-3               | test-process-2    | Mar 26th 2026 09:26:09 |

## Related Articles

* **Stages**: [windowcomp](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/windowcomp.md), [filter](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/filter.md), [fields](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/fields.md), [sort](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/sort.md)
* **Functions**: lead, [first\_value](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/first_value.md), [last\_value](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/last_value.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/lag.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
