atoti.function.where module

atoti.where(condition: ConditionOperation, true_value: _OperationLike, false_value: Optional[_OperationLike] = None) TernaryOperation
atoti.where(condition: Union[BooleanMeasure, Condition, Measure], true_value: MeasureLike, false_value: Optional[MeasureLike] = None) MeasureDescription

Return a conditional measure.

This function is like an if-then-else statement:

  • Where the condition is True, the new measure will be equal to true_value.

  • Where the condition is False, the new measure will be equal to false_value.

If false_value is not None, true_value and false_value must either be both numerical, both boolean or both objects.

If one of the values compared in the condition is None, the condition will be considered False.

Different types of conditions are supported:

  • Measures compared to anything measure-like:

    m["Test"] == 20
    
  • Levels compared to levels, (if the level is not expressed, it is considered None):

    l["source"] == l["destination"]
    
  • Levels compared to literals of the same type:

    l["city"] == "Paris"
    l["date"] > datetime.date(2020,1,1)
    l["age"] <= 18
    
  • A conjunction or disjunction of conditions using the & operator or | operator:

    (m["Test"] == 20) & (l["city"] == "Paris")
    (l["Country"] == "USA") | (l["Currency"] == "USD")
    
Parameters

Example

>>> 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),
...     ],
... )
>>> table = session.read_pandas(df, keys=["Id"], table_name="filter example")
>>> cube = session.create_cube(table)
>>> l, m = cube.levels, cube.measures
>>> m["Paris value"] = tt.where(l["City"] == "Paris", m["Value.SUM"], 0)
>>> cube.query(m["Paris value"], levels=[l["City"]])
       Paris value
City
London         .00
Paris         8.00
Return type

Union[MeasureDescription, TernaryOperation]