# IF
URL: https://docs.valsight.ai/if/
Description: The IF function evaluates a condition and returns one value when the condition is true and another when it is false. For every row of the input, the condition is evaluated independently.
**Category:** [Logical functions](/logical-functions/)

## Overview

The **IF** function **evaluates a condition and returns one value when the condition is true and another when it is false.** For every row of the input, the condition is evaluated independently.

Use this function to implement business rules such as thresholds, caps, floors, or other conditional logic.

## Syntax

* `IF(Condition)` returns the condition's value where true and no value where false.
* `IF(Condition, ValueIfTrue)` returns ValueIfTrue where true and no value where false.
* `IF(Condition, ValueIfTrue, ValueIfFalse)` returns one value for true and another for false.

**Example usage:** `IF('Margin' < 0, 0, 'Margin')`

## Parameters

| Parameter        | Description                                                                                                                                     | Type                     | Required |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ | -------- |
| **Condition**    | A node evaluated to true or false. `0` is interpreted as false; all other values as true. Comparisons can be configured with logical functions. | Node reference           | Yes      |
| **ValueIfTrue**  | Value returned when the condition is true.                                                                                                      | Node reference or number | No       |
| **ValueIfFalse** | Value returned when the condition is false.                                                                                                     | Node reference or number | No       |

## Building conditions

Conditions can be written as operators or with logical functions. Most users write the operator form directly in formulas.

| Operator | Function          | Meaning               |
| -------- | ----------------- | --------------------- |
| `=`      | [EQ (=)](/eq/)    | equal                 |
| `!=`     | [NEQ (!=)](/neq/) | not equal             |
| `>`      | [GT (>)](/gt/)    | greater than          |
| `>=`     | [GTE (>=)](/gte/) | greater than or equal |
| `<`      | [LT (<)](/lt/)    | less than             |
| `<=`     | [LTE (<=)](/lte/) | less than or equal    |
| `&&`     | [AND (&&)](/and/) | both conditions true  |
| `\|\|`   | [OR (\|\|)](/or/) | at least one true     |
|          | [NOT](/not/)      | invert condition      |
|          | [IS\_NA](/is-na/) | value is undefined    |

For details and pitfalls, see [Comparisons & boolean operators](/comparisons-boolean-operators/).

## Output shape

| Aspect         | Behavior                                                                                                                                                                                                                                   |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Dimensionality | Union of all input dimensionalities. Per dimension, the finest granularity across the condition, ValueIfTrue, and ValueIfFalse is used.                                                                                                    |
| Row count      | Depends on the form used. With all 3 arguments, the output contains one row per condition row (true rows get ValueIfTrue, false rows get ValueIfFalse). With 1 or 2 arguments, rows where the condition is false are dropped (become N/A). |
| Values         | The condition is evaluated independently for each row                                                                                                                                                                                      |

## Watch out

* When all three arguments are provided, if ValueIfTrue or ValueIfFalse has finer dimensionality than the condition, both must have the same dimensionality. This check does not apply to the 1- or 2-argument form.
* 0 is interpreted as false, any other value as true.
* When IF returns N/A, the cell appears empty in tables and is excluded from charts. This is different from returning 0. N/A means "no value exists," while 0 is a real numeric value.

## Examples

### Dividend payout

Dividends are only paid in years with **positive net income**.
The payout equals **50 % of net income**.

**Input node:** `'Dividend Payouts / Net Income'`

| Value |
| ----- |
| 50%   |

**Input node:** `'Net Income'`

| Year | Value |
| ---- | ----- |
| 2025 | -30   |
| 2026 | 20    |
| 2027 | -10   |
| 2028 | 0     |
| 2029 | 100   |

**Formula:** `IF('Net Income' > 0, 'Net Income' * 'Dividend Payouts / Net Income', 0)`

| Year | IF Result |
| ---- | --------- |
| 2025 | 0         |
| 2026 | 10        |
| 2027 | 0         |
| 2028 | 0         |
| 2029 | 50        |

Dividends are only paid in years with positive net income.

Example calculations:

* 2026 → 20 × 50 % = 10
* 2029 → 100 × 50 % = 50

### Floor at zero

Negative margins should be reported as 0.

**Input node:** `'Margin'`

| Year | Value |
| ---- | ----- |
| 2025 | 15    |
| 2026 | -8    |
| 2027 | 22    |
| 2028 | -3    |

**Formula:** `IF('Margin' < 0, 0, 'Margin')`

| Year | IF Result |
| ---- | --------- |
| 2025 | 15        |
| 2026 | 0         |
| 2027 | 22        |
| 2028 | 0         |

### IF used as a condition check

When only the condition is provided, IF returns 1 when the condition is true and no value (N/A) when it is false.

**Input node:** `'Revenue'`

| Year | Revenue |
| ---- | ------- |
| 2025 | 800     |
| 2026 | 1200    |
| 2027 | 1500    |
| 2028 | 600     |

**Formula:** `IF('Revenue' > 1000)`

| Year | IF Result |
| ---- | --------- |
| 2026 | 1         |
| 2027 | 1         |

In this example, the comparison `'Revenue' > 1000` evaluates to 1, which is what IF returns. If you use `IF('Revenue')` directly, it returns Revenue's own value for non-zero rows.

This pattern is useful when creating **binary helper tables** or **flags** for further filtering or visualization.

### Using IF(Node) to remove zero values

When a node itself is used as the condition, IF checks whether the value is true (non-zero) or false (zero).

Because IF interprets 0 as false, those entries become N/A in the result.

**Input node:** `'Revenue'`

| Year | Revenue |
| ---- | ------- |
| 2025 | 0       |
| 2026 | 120     |
| 2027 | 0       |
| 2028 | 300     |

**Formula:** `IF('Revenue')`

| Year | IF Result |
| ---- | --------- |
| 2026 | 120       |
| 2028 | 300       |

This technique replaces zeros with N/A values, which can help avoid misleading totals and reduce visible rows in tables and charts.

## Related

| Function                          | When to use instead                                                          |
| --------------------------------- | ---------------------------------------------------------------------------- |
| [FILTER](/filter/)                | Removes non-matching rows entirely instead of returning an alternative value |
| [TRUE](/true/) / [FALSE](/false/) | Boolean constants usable inside conditions                                   |
