> 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/stages/switch.md).

# switch

{% hint style="info" %}

#### Licenses

Requires any XSIAM license with the Data Pipeline add-on.
{% endhint %}

{% hint style="warning" %}

#### Important

The `switch` stage is supported only in Data Pipeline filters. It is not supported in XQL queries, Parsing Rules, Data Model Rules, or Correlation Rules. For more information, see [Configure Data Pipelines](https://cortex-docs.paloaltonetworks.com/cortex-xsiam/configure-cortex-xsiam/data-management/configure-data-pipelines).
{% endhint %}

Use the `switch` stage in a Data Pipeline filter to apply different `fields` stages to events based on conditions. Each event is evaluated against the `case` conditions in order, and the `fields` stages of the matching case determine which fields are kept or removed for that event.

## Syntax

```
switch
    case <condition1>: (
        fields [-] <field1>, <field2>, ...
        [| fields [-] <field3>, ...]
    )
    [case <condition2>: (
        fields [-] <field1>, <field2>, ...
    ) ...]
    [default: (
        fields [-] <field1>, <field2>, ...
    )]
```

## Parameters

| Name          | Type    | Required | Description                                                                                                                                  |
| ------------- | ------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| `case`        | clause  | Yes      | Defines a condition and the `fields` stages to apply to events that match it. You can define multiple case clauses.                          |
| `<condition>` | boolean | Yes      | A logical expression, using the same syntax as the filter stage. Supports `and`, `or`, `not`, comparison operators, and supported functions. |
| `default`     | clause  | No       | Defines the `fields` stages to apply to events that do not match any `case` condition. The `default` clause does not accept a condition.     |
| `fields`      | stage   | Yes      | One or more fields stages, separated by pipes (`\|`) and enclosed in parentheses. Use `-` to exclude fields.                                 |

## Returns

The `switch` stage returns each event with the fields selected or excluded by the `fields` stages of the matching `case`. Events that match no `case` are processed by the `default` clause. If there is no `default` clause, those events are returned unchanged.

## Usage notes

* The `switch` stage is available only in Data Pipeline filters. In a pipeline filter, it can be combined with the `filter` and `fields` stages, for example `filter <condition> | fields <fields> | switch ....`
* `case` clauses are evaluated in the order they are defined. Only the first `case` whose condition evaluates to true is applied to an event.
* Only the `fields` stage is allowed inside a `case` or `default` clause. Other stages, such as `alter` or `filter`, are not allowed.
* Each `case` and `default` clause must contain at least one `fields` stage, and each `fields` stage must specify at least one field.
* Wildcards (`*`) and the `as` clause are not allowed in a `fields` stage inside the `switch` stage.
* System fields cannot be excluded, for example `_time`, `_insert_time`, `_raw_log`, `_id`, `_vendor`, `_product`, `_tag`, and the `_collector_* fields`. System field names are not case sensitive, so `- _TIME` is also rejected. You can still include system fields.
* Only one `default` clause is allowed, and it cannot include a condition.
* The `switch`, `case`, and `default` keywords are not case sensitive.
* The functions that you can use in `case` conditions are the same as those supported in Data Pipeline filters. Array functions are not supported.
* The `in` operator in a `case` condition accepts only a list of values, for example severity in (`"HIGH"`, `"MEDIUM"`). Inner queries, such as `in` (`dataset = ...`), are not supported.

## Examples

The examples use the following sample events:

| severity | user  | action | src\_ip  | debug\_info | raw\_payload |
| -------- | ----- | ------ | -------- | ----------- | ------------ |
| HIGH     | alice | BLOCK  | 10.0.0.1 | trace-01    | {...}        |
| MEDIUM   | bob   | ALLOW  | 10.0.0.2 | trace-02    | {...}        |
| LOW      | carol | ALLOW  | 10.0.0.3 | trace-03    | {...}        |

#### Example 1: Applying different field selections by severity

**Goal**: Keep only the key fields for high-severity events, remove the debugging field from medium-severity events, and remove the large fields from all other events.

**XQL code**:

```
switch
    case severity = "HIGH": (
        fields severity, user, action, src_ip
    )
    case severity = "MEDIUM": (
        fields - debug_info
    )
    default: (
        fields - debug_info, raw_payload
    )
```

**Explanation**: The `HIGH` event keeps only `severity`, `user`, `action`, and `src_ip`. The `MEDIUM` event keeps all fields except `debug_info`. The `LOW` event matches no `case`, so the `default` clause removes `debug_info` and `raw_payload`.

**Output**:

| severity | user  | action | src\_ip  | debug\_info | raw\_payload |
| -------- | ----- | ------ | -------- | ----------- | ------------ |
| HIGH     | alice | BLOCK  | 10.0.0.1 |             |              |
| MEDIUM   | bob   | ALLOW  | 10.0.0.2 |             | {...}        |
| LOW      | carol | ALLOW  | 10.0.0.3 |             |              |

#### Example 2: Using multiple fields stages in a case

**Goal**: Select a set of fields and then exclude one of them within the same case.

**XQL** code:

```
switch
    case severity = "HIGH" or severity = "MEDIUM": (
        fields severity, user, src_ip
        | fields - src_ip
    )
    default: (
        fields - raw_payload
    )
```

**Explanation**: For the `HIGH` and `MEDIUM` events, the first `fields` stage keeps `severity`, `user`, and `src_ip`, and the second `fields` stage removes `src_ip`. The `LOW` event is processed by the `default` clause, which removes `raw_payload`.

**Output**:

| severity | user  | action | src\_ip  | debug\_info | raw\_payload |
| -------- | ----- | ------ | -------- | ----------- | ------------ |
| HIGH     | alice |        |          |             |              |
| MEDIUM   | bob   |        |          |             |              |
| LOW      | carol | ALLOW  | 10.0.0.3 | trace-03    |              |

#### Example 3: Using switch without a default clause

**Goal**: Change only events that match a condition and leave all other events unchanged.

**XQL code**:

```
switch
    case action = "BLOCK": (
        fields - raw_payload
    )
```

**Explanation**: Only the event where action is `BLOCK` has `raw_payload` removed. The other events match no `case`, and because there is no `default` clause, they are returned unchanged.

**Output**:

| severity | user  | action | src\_ip  | debug\_info | raw\_payload |
| -------- | ----- | ------ | -------- | ----------- | ------------ |
| HIGH     | alice | BLOCK  | 10.0.0.1 | trace-01    |              |
| MEDIUM   | bob   | ALLOW  | 10.0.0.2 | trace-02    | {...}        |
| LOW      | carol | ALLOW  | 10.0.0.3 | trace-03    | {...}        |

#### Example 4: Combining switch with filter and fields

**Goal**: Drop low-severity events, remove a field from all remaining events, and then apply field selection by action.

**XQL code**:

```
filter severity != "LOW"
| fields - debug_info
| switch
    case action = "BLOCK": (
        fields severity, user, action, src_ip
    )
    default: (
        fields - raw_payload
    )
```

**Explanation**: The `filter` stage drops the `LOW` event, and the `fields` stage removes `debug_info` from the remaining events. The `switch` stage then keeps only the key fields for the `BLOCK` event and removes `raw_payload` from the other event.

**Output**:

| severity | user  | action | src\_ip  | raw\_payload |
| -------- | ----- | ------ | -------- | ------------ |
| HIGH     | alice | BLOCK  | 10.0.0.1 |              |
| MEDIUM   | bob   | ALLOW  | 10.0.0.2 |              |

#### Example 5: Invalid switch stages

**Goal**: Identify syntax that the `switch` stage does not support.

**XQL code**:

```
// alter is not allowed inside a case
switch case severity = "HIGH": (alter tag = "x")

// filter is not allowed inside a case
switch case severity = "HIGH": (fields user | filter action = "BLOCK")

// a fields stage must specify at least one field
switch case severity = "HIGH": (fields)

// wildcards are not allowed
switch case severity = "HIGH": (fields - debug*)

// the as clause is not allowed
switch case severity = "HIGH": (fields user as username)

// system fields cannot be excluded
switch case severity = "HIGH": (fields - _time)

// default cannot include a condition
switch case severity = "HIGH": (fields user) default severity = "LOW": (fields action)
```

**Explanation**: Each of these filters is rejected when you save or simulate the Data Pipeline filter.

## Related articles

* **Stages**: [fields](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/fields.md), [filter](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/filter.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/stages/switch.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.
