# if - スクリプトおよびチャート関数

if 関数は、与えられた条件が True または False のどちらに評価されるかによって異なる値を返します。

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

構文:

if(condition , then [, else])

このトピックの例のいくつかは、インライン ロードを使用しています。詳細については、「[インライン ロードを使用したデータのロード](https://help.qlik.com/ja-JP/sense/November2024/Subsystems/Hub/Content/Sense_Hub/Scripting/inline-loads-qs.htm)」を参照してください。

引数

| 引数 | 説明 |
| --- | --- |
| condition | 論理的に解釈された数式。 |
| then | 数式は任意の型に指定できます。condition が True の場合、if 関数は then 数式の値を返します。 |
| else | 数式は任意の型に指定できます。condition が False の場合、if 関数は else 数式の値を返します。<br><br>このパラメータはオプションです。condition が False の場合、else を指定しないと NULL が返されます。 |

例

| 例 | 結果 |
| --- | --- |
| `if( Amount>=<br>						0, 'OK', 'Alarm' )` | この数式は、Amount が正の数 (0 以上) かどうかテストし、正の数であれば 'OK' を返します。Amount が 0 未満であれば、'Alarm' を返します。 |

## 例 - if を使用したロード スクリプト

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

#### 例: ロード スクリプト

ロード スクリプト

If は、他のメソッドやオブジェクト (変数を含む) とともにロード スクリプトで使用できます。たとえば、変数 threshold を設定し、このしきい値に基づいてデータ モデルに項目を含める場合、以下を実行できます。

データ ロード エディタで新しいタブを作成し、次のデータをインライン ロードとしてロードします。結果を確認するには、以下の Qlik Sense のテーブルを作成します。

```
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;
```

結果

ロード スクリプトで ifif 関数を使用した結果の出力を示す Qlik SenseQlik Sense のテーブル。

| transaction\_id | しきい値と比較 |
| --- | --- |
| 3750 | 100 未満 |
| 3751 | 100 超 |
| 3752 | 100 未満 |
| 3753 | 100 超 |
| 3754 | 100 超 |
| 3756 | 100 未満 |
| 3757 | 100 超 |

## 例 - if を使用したチャートの数式

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

#### 例: チャートの数式

チャートの数式 1

ロード スクリプト

データ ロード エディタで新しいタブを作成し、次のデータをインライン ロードとしてロードします。データをロードした後、以下のチャートの数式の例を Qlik Sense テーブルに作成します。

```
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 のテーブルは、チャートの数式における ifif 関数の例を示しています。

| 日付 | 場所 | インシデント | if(Incidents&gt;=10, 'Critical', 'Ok' ) | if(Incidents&gt;=10, 'Critical', If( Incidents&gt;=1 and Incidents&lt;10, 'Warning', 'Ok')) |
| --- | --- | --- | --- | --- |
| 1/3/2016 | 北京 | 0 | OK | OK |
| 1/3/2016 | ボストン | 12 | [Critical] (重大) | [Critical] (重大) |
| 1/3/2016 | ストックホルム | 3 | OK | 警告 |
| 1/3/2016 | トロント | 0 | OK | OK |
| 1/4/2016 | 北京 | 0 | OK | OK |
| 1/4/2016 | ボストン | 8 | OK | 警告 |

チャートの数式 2

新しいアプリで、データ ロード エディターの新しいタブに次のスクリプトを追加してから、データをロードします。次に、以下のチャートの数式を使用してテーブルを作成できます。

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

Qlik SenseQlik Sense のテーブルは、チャートの数式における ifif 関数の例を示しています。

| 日付 | WeekDay(Date) | If(WeekDay(Date)&gt;=5,'WeekEnd','Normal Day') |
| --- | --- | --- |
| 1/1/2022 | 土 | WeekEnd |
| 1/2/2022 | 日 | WeekEnd |
| 1/3/2022 | 月 | Normal Day |
| 1/4/2022 | 火 | Normal Day |
| 1/5/2022 | 水 | Normal Day |
| 1/6/2022 | 木 | Normal Day |
| 1/7/2022 | 金 | Normal Day |
| 1/8/2022 | 土 | WeekEnd |
| 1/9/2022 | 日 | WeekEnd |
| 1/10/2022 | 月 | Normal Day |
| 1/11/2022 | 火 | Normal Day |
| 1/12/2022 | 水 | Normal Day |
| 1/13/2022 | 木 | Normal Day |
| 1/14/2022 | 金 | Normal Day |