# if - script and chart function

The if function returns a value depending on whether the condition provided with the function evaluates as True or False.

[If Function](https://play.vidyard.com/qnkaRddq32LnAGUTTDGdLA)

Syntax:

if(condition , then [, else])

Arguments

| Argument | Description |
| --- | --- |
| condition | Expression that<br>is interpreted logically. |
| then | Expression that can be of any type. If the condition<br>is True, then the if function returns the value of the then expression. |
| else | Expression that can be of any type.<br>If the condition is False, then the if function<br>returns the value of the else expression.<br><br>This parameter is optional.<br>If the condition is False,<br>NULL is returned if you have not specified else. |

Some of the examples in this topic use inline loads. For more information, see [Using inline loads to load data](https://help.qlik.com/en-US/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/inline-loads-qs.htm).

Example

| Example | Result |
| --- | --- |
| `if( Amount>=<br>						0, 'OK', 'Alarm' )` | This expression tests if the amount is a positive number (0 or larger) and returns `OK` if it is. If the amount is less than 0, `Alarm` is returned. |

## Example - Load script using if

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/ConditionalFunctions/if.htm)

#### Example: Load script

Load script

If can be used in load script with other methods and objects, including variables. For example, if you set a variable threshold and want to include a field in the data model based on that threshold, you can do the following.

Create a new tab in the data load editor, and then load the following data as an inline load. Create the table below in Qlik Sense to see the results.

```
Transactions:
Load * Inline [
transaction_id, transaction_date, transaction_amount, transaction_quantity, customer_id, size, color_code
3750, 20180830, 23.56, 2, 2038593, L, Red
3751, 20180907, 556.31, 6, 203521, m, orange
3752, 20180916, 5.75, 1, 5646471, S, blue
3753, 20180922, 125.00, 7, 3036491, l, Black
3754, 20180922, 484.21, 13, 049681, xs, Red
3756, 20180922, 59.18, 2, 2038593, M, Blue
3757, 20180923, 177.42, 21, 203521, XL, Black
];
 
set threshold = 100;
					
/* Create new table called Transaction_Buckets 
Compare transaction_amount field from Transaction table to threshold of 100.
Output results into a new field called Compared to Threshold							
 */					
 
Transaction_Buckets:
Load
   transaction_id,
   If(transaction_amount > $(threshold),'Greater than $(threshold)','Less than $(threshold)') as [Compared to Threshold]
Resident Transactions;
```

Results

Qlik SenseQlik Sense table showing the output from using the ifif function in the load script.

| transaction\_id | Compared to Threshold |
| --- | --- |
| 3750 | Less than 100 |
| 3751 | Greater than 100 |
| 3752 | Less than 100 |
| 3753 | Greater than 100 |
| 3754 | Greater than 100 |
| 3756 | Less than 100 |
| 3757 | Greater than 100 |

## Examples - Chart expressions using if

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/ConditionalFunctions/if.htm)

#### Examples: Chart expressions

Chart expression 1

Load script

Create a new tab in the data load editor, and then load the following data as an inline load. After loading the data, create the chart expression examples below in a Qlik Sense table.

```
MyTable:
LOAD * inline [Date, Location, Incidents
1/3/2016, Beijing, 0
1/3/2016, Boston, 12
1/3/2016, Stockholm, 3
1/3/2016, Toronto, 0
1/4/2016, Beijing, 0
1/4/2016, Boston, 8];
```

Qlik SenseQlik Sense table showing examples of the ifif function in a chart expression.

| Date | Location | Incidents | if(Incidents&gt;=10, 'Critical', 'Ok' ) | if(Incidents&gt;=10, 'Critical', If( Incidents&gt;=1 and<br>Incidents&lt;10, 'Warning', 'Ok')) |
| --- | --- | --- | --- | --- |
| 1/3/2016 | Beijing | 0 | Ok | Ok |
| 1/3/2016 | Boston | 12 | Critical | Critical |
| 1/3/2016 | Stockholm | 3 | Ok | Warning |
| 1/3/2016 | Toronto | 0 | Ok | Ok |
| 1/4/2016 | Beijing | 0 | Ok | Ok |
| 1/4/2016 | Boston | 8 | Ok | Warning |

Chart expression 2

In a new app, add the following script in a new tab in the data load editor, and then load the data. You can then create the table with the chart expressions below.

```
SET FirstWeekDay=0;
Load 
Date(MakeDate(2022)+RecNo()-1) as Date
Autogenerate 14;
```

Qlik SenseQlik Sense table showing an example of the ifif function in a chart expression.

| Date | WeekDay(Date) | If(WeekDay(Date)&gt;=5,'WeekEnd','Normal Day') |
| --- | --- | --- |
| 1/1/2022 | Sat | WeekEnd |
| 1/2/2022 | Sun | WeekEnd |
| 1/3/2022 | Mon | Normal Day |
| 1/4/2022 | Tue | Normal Day |
| 1/5/2022 | Wed | Normal Day |
| 1/6/2022 | Thu | Normal Day |
| 1/7/2022 | Fri | Normal Day |
| 1/8/2022 | Sat | WeekEnd |
| 1/9/2022 | Sun | WeekEnd |
| 1/10/2022 | Mon | Normal Day |
| 1/11/2022 | Tue | Normal Day |
| 1/12/2022 | Wed | Normal Day |
| 1/13/2022 | Thu | Normal Day |
| 1/14/2022 | Fri | Normal Day |