# Add conditional column

The Add conditional column allows you to create a new column based on If...Then...Else schema with possibilities to add nested conditions in order to create conditional logic for data manipulation.

The condition is expressed in the `if` parameter with a condition object, which is the same object expected by the `condition` parameter of the [filter step](https://docs-v3.toucantoco.com/data-management-in-datahub/datasets-in-toucan/preparing-data/overview-of-youprep-tm/filter/filter-rows)). Conditions can be grouped and nested with logical operators `and` and `or`.

The `then` parameter only supports a string, that will be interpreted as a formula (cf. [formula step](https://docs-v3.toucantoco.com/data-management-in-datahub/datasets-in-toucan/preparing-data/overview-of-youprep-tm/add/add-formula-column)). If you want it to be interpreted strictly as a string and not a formula, you must escape the string with quotes (e.g. ‘“this is a text”’).

`if...then...else` blocks can be nested as the `else` parameter supports either a string that will be interpreted as a formula (cf. [formula step](https://docs-v3.toucantoco.com/data-management-in-datahub/datasets-in-toucan/preparing-data/overview-of-youprep-tm/add/add-formula-column)), or a nested if `if...then...else` object.

### Step parameters

1. `New Column` **(string)**: Enter a name for the new column that will be created based on your conditions and formulas.
2. `If Condition`: Define the condition(s) that will be evaluated.
3. `Then Formula`: Specify the formula to be applied when the condition is true.
4. `Else Formula`: Specify the formula to be applied when the condition is false.

### Example

**Input**

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-01e491be9cb84a436b42012a016439abd70c53ac%2Fifthenelse_input.png?alt=media" alt=""><figcaption><p>Add - Add conditional column input</p></figcaption></figure>

**Configuration**

```json
{
    "new_column": "Category",
    "if": {
            "and": [
                {
                        "column": "Price",
                        "operator": "ge",
                        "value": 50
                },
                {
                        "column": "Stock",
                        "operator": "gt",
                        "value": 10
                },
            ]
        },
    "then" "Premium",
    "else": "Standard"    
}
```

Where `gt`: greater than and `ge`: greater than or equal to

**Output**

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-622b2465821ce6860eea24ac3ac761efa0c9fa5b%2Fif_then_else.png?alt=media" alt=""><figcaption><p>Add - Add a conditional column output</p></figcaption></figure>

{% hint style="info" %}
**Defining Conditions**

You can create three types of conditions:

1. **Simple Condition**:
   * Column Name: Enter the name of the column you want to filter on.
   * Operator: Choose from operators like equals, not equals, greater than, less than, etc.
   * Value: Enter the value to compare against.
2. **AND Condition**: Combine multiple conditions that must all be true.
3. **OR Condition**: Combine multiple conditions where at least one must be true.
   {% endhint %}

{% hint style="info" %}
**Operators**

The following operators are available for conditions:

* eq (equals)
* ne (doesn't equal to)
* gt (is greater than)
* ge (is greater than or equal to)
* lt (is less than)
* le (is less than or equal to)
* in (is one of)
* nin (is not one of)
* matches (matches pattern)
* notmatches (doesn't match pattern)
* isnull (is null)
* notnull (is not null)
* from (starting in/on)
* until (ending in/on)
  {% endhint %}

{% hint style="info" %}
**Formulas**

For the "Then" and "Else" parts, you can enter formulas that will be applied based on the condition results. These can be simple values or complex expressions. See the [formula step](https://docs-v3.toucantoco.com/data-management-in-datahub/datasets-in-toucan/preparing-data/overview-of-youprep-tm/add/add-formula-column) for more information.
{% endhint %}

{% hint style="info" %}
**Nested Conditions**

You can create nested conditions by using another If...Then...Else structure in the "Else" part of your condition.
{% endhint %}
