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

# switch()

<span id="atoti.switch" />

> atoti.switch(<br />
>     *subject*: VariableMeasureConvertible,<br />
>     *cases*: Mapping\[MeasureConvertible | [None](https://docs.python.org/3/library/constants.html#None) | AbstractSet\[MeasureConvertible | [None](https://docs.python.org/3/library/constants.html#None)], MeasureConvertible],<br />
>     /,<br />
>     \*,<br />
>     *default*: MeasureConvertible | [None](https://docs.python.org/3/library/constants.html#None) = `None`,<br />
> ) → MeasureDefinition

Return a measure equal to the value of the first case for which *subject* is equal to the case’s key.

*cases*’s values and *default* must either be all numerical, all boolean or all objects.

### Parameters

<h4 id="atoti.switch.subject">
  *subject*
</h4>

The measure or level to compare to *cases*’ keys.

<h4 id="atoti.switch.cases">
  *cases*
</h4>

A mapping from keys to compare with *subject* to the values to return if the comparison is `True`.

<h4 id="atoti.switch.default">
  *default*
</h4>

The measure to use when none of the *cases* matched.

***

```pycon theme={"languages":{"custom":["/engine/python-sdk/0.9/languages/pycon.tmLanguage.json"]}}
>>> df = pd.DataFrame(
...     columns=["Id", "City", "Value"],
...     data=[
...         (0, "Paris", 1.0),
...         (1, "Paris", 2.0),
...         (2, "London", 3.0),
...         (3, "London", 4.0),
...         (4, "Paris", 5.0),
...         (5, "Singapore", 7.0),
...         (6, "NYC", 2.0),
...     ],
... )
>>> table = session.read_pandas(df, keys={"Id"}, table_name="Switch example")
>>> cube = session.create_cube(table)
>>> l, m = cube.levels, cube.measures
>>> m["Continent"] = tt.switch(
...     l["City"],
...     {
...         frozenset({"Paris", "London"}): "Europe",
...         "Singapore": "Asia",
...         "NYC": "North America",
...     },
... )
>>> cube.query(m["Continent"], levels=[l["City"]])
               Continent
City
London            Europe
NYC        North America
Paris             Europe
Singapore           Asia
>>> m["Europe & Asia value"] = tt.agg.sum(
...     tt.switch(
...         m["Continent"],
...         {frozenset({"Europe", "Asia"}): m["Value.SUM"]},
...         default=0.0,
...     ),
...     scope=tt.OriginScope({l["Id"], l["City"]}),
... )
>>> cube.query(m["Europe & Asia value"], levels=[l["City"]])
          Europe & Asia value
City
London                   7.00
NYC                       .00
Paris                    8.00
Singapore                7.00
>>> cube.query(m["Europe & Asia value"])
  Europe & Asia value
0               22.00
```

<Callout icon="link">
  **See also**:
  [`atoti.where()`](./atoti.function.where#atoti.where).
</Callout>
