> For the complete documentation index, see [llms.txt](https://docs.valq.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.valq.com/model/formula-functions/logical-functions/if.md).

# IF

An IF statement returns a value depending on whether the condition that is being evaluated is TRUE or FALSE.

### Syntax

IF(logical\_test, value\_if\_true, value\_if\_false)

### Arguments

<table><thead><tr><th width="153">Arguments</th><th width="163">Data Type</th><th>Condition</th></tr></thead><tbody><tr><td>logical_test</td><td>Text / Number</td><td>The condition to be evaluated.</td></tr><tr><td>value_if_true</td><td>Node reference / Number</td><td>The value to be returned if the condition is met.</td></tr><tr><td>value_if_false</td><td>Node reference / Number</td><td>The value to be returned if the condition is not met.</td></tr></tbody></table>

### Return Value

Either *value\_if\_true* or *value\_if\_false.*

### Remarks

1. Returns Blank when the formula is misspelled.
2. The specified node should contain Numerical data.
3. The IF function returns error in any of the following cases:

* *value\_if\_false* argument is missing.
* The argument *value\_if\_true* or *value\_if\_false* are not of numeric data type.
* Not using appropriate operator in *logical\_test*.

### Example

Consider the **Revenue** and **Cost of Goods Sold** nodes to follow the trend as shown below.

<table><thead><tr><th width="137">Parameter</th><th align="center">Jan</th><th align="center">Feb</th><th align="center">Mar</th><th align="center">Apr</th><th align="center">May</th><th align="center">Jun</th></tr></thead><tbody><tr><td><mark style="color:purple;">Revenue</mark></td><td align="center">100</td><td align="center">124</td><td align="center">137</td><td align="center">0</td><td align="center">23</td><td align="center">45</td></tr><tr><td><mark style="color:orange;">Cost of Goods Sold</mark></td><td align="center">25</td><td align="center">25</td><td align="center">25</td><td align="center">25</td><td align="center">25</td><td align="center">25</td></tr></tbody></table>

Now, a new node **Gross Profit Margin** can be computed using the IF function. Gross Profit Margin is calculated as (Revenue-Cost of Goods Sold)/Revenue. In case Revenue has period value of 0, Gross Profit Margin should return 0.

To achieve this, the following formula should be written in the \[Gross Profit Margin] node:

<mark style="color:blue;">**`IF`**</mark>**`(`**<mark style="color:purple;">**`[Revenue]`**</mark>**`==0,0%,(`**<mark style="color:purple;">**`[Revenue]`**</mark>**`-`**<mark style="color:orange;">**`[Cost of Goods Sold]`**</mark>**`)/`**<mark style="color:purple;">**`[Revenue]`**</mark>**`)`**

### Result

<table><thead><tr><th width="134">Parameter</th><th align="center">Jan</th><th align="center">Feb</th><th align="center">Mar</th><th align="center">Apr</th><th align="center">May</th><th align="center">Jun</th></tr></thead><tbody><tr><td>Gross Profit Margin</td><td align="center">75%</td><td align="center">80%</td><td align="center">82%</td><td align="center">0%</td><td align="center">-9%</td><td align="center">44%</td></tr></tbody></table>

As you can see from the above table, if the logical test condition (Revenue==0) is True, it returns 0, i.e. the gross profit margin is 0 and if the logical test condition is False (Revenue is a non-zero number), it calculates the Gross Profit Margin from the formula and returns it.

{% hint style="info" %}
Use **SWITCH** function instead of nesting multiple IF functions.
{% endhint %}


---

# 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://docs.valq.com/model/formula-functions/logical-functions/if.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.
