if-then-else · if then else
---
config:
flowchart:
useMaxWidth: false
---
flowchart TB
classDef ok fill:#d4edda,stroke:#28a745,color:#000
classDef partial fill:#fff3cd,stroke:#ffc107,color:#000
classDef refused fill:#d4edda,stroke:#28a745,stroke-width:3px,stroke-dasharray: 2 2,color:#000
classDef missing fill:#f8d7da,stroke:#dc3545,color:#000
classDef diverge fill:#cce5ff,stroke:#007bff,color:#000
classDef block fill:#2b3a4a,stroke:#2b3a4a,color:#fff,font-weight:bold
classDef ext fill:#fff,stroke:#666,color:#000,stroke-dasharray: 5 5
O["if-then-else · if then else<br/>🟢5"]:::ok
subgraph G_ds["Dataset"]
direction LR
v_ds["if DS then DS else DS"]:::ok
v_ds_scalar_branch["if DS then DS#124;sc else sc#124;DS"]:::ok
end
subgraph G_comp["Component"]
direction LR
v_comp["if comp then comp else comp"]:::ok
v_comp_scalar_branch["if comp then sc else sc"]:::ok
end
subgraph G_sc["Scalar"]
direction LR
v_sc["if sc then sc else sc"]:::ok
end
O --- G_ds
O --- G_comp
O --- G_sc
style G_ds fill:#f8f9fa,stroke:#495057,stroke-width:2px
style G_comp fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 8 5
style G_sc fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 2 3
click v_ds href "#form-ds"
click v_ds_scalar_branch href "#form-ds-scalar-branch"
click v_comp href "#form-comp"
click v_comp_scalar_branch href "#form-comp-scalar-branch"
click v_sc href "#form-sc"
Types
Takes boolean, any scalar · Gives any type
Comes from: any operator that returns a value
Goes to: whatever takes its input's type
Forms
if DS then DS else DS 🟢 · boolean, any scalar → any type
Manual example 1 🟢 pandas ✅ · DuckDB ✅
- Tests:
a data set condition,
if ( DS_1#Id_4 = "F" ) then DS_2 else DS_3, that sends each data point to one of two data sets.- Manual:
if-then-else, Example 1, output verbatim: “returns the Data Point from thenOperand when the Data Point of condition having the same Identifiers’ values evaluates to true, and returns the Data Point from elseOperand otherwise”. The page’s introduction names the operands
DS_cond,DS_1andDS_2where the script usesDS_1,DS_2andDS_3, a slip of the prose and not of the example, and the page’sTimePeriodtype is writtenTime_Period, as vtlengine does.- Expected:
the page’s six data points: those of
DS_2whereId_4is F, forId_2S, F and I (25.8, null and 20.9), and those ofDS_3where it is M, forId_2B, G and S (0.12, 22.5 and 23.7). A data point that is missing from the branch it is sent to, such as B withId_4F inDS_2, is left out.
DS_r := if ( DS_1#Id_4 = "F" ) then DS_2 else DS_3;
| Id_1 | Id_2 | Id_3 | Id_4 | Me_1 |
|---|---|---|---|---|
| 2012 | B | Total | M | 5451780 |
| 2012 | B | Total | F | 5643070 |
| 2012 | G | Total | M | 5449803 |
| 2012 | G | Total | F | 5673231 |
| 2012 | S | Total | M | 23099012 |
| 2012 | S | Total | F | 23719207 |
| 2012 | F | Total | M | 31616281 |
| 2012 | F | Total | F | 33671580 |
| 2012 | I | Total | M | 28726599 |
| 2012 | I | Total | F | 30667608 |
| 2012 | A | Total | M | |
| 2012 | A | Total | F |
| Id_1 | Id_2 | Id_3 | Id_4 | Me_1 |
|---|---|---|---|---|
| 2012 | S | Total | F | 25.8 |
| 2012 | F | Total | F | |
| 2012 | I | Total | F | 20.9 |
| 2012 | A | Total | M | 6.3 |
| Id_1 | Id_2 | Id_3 | Id_4 | Me_1 |
|---|---|---|---|---|
| 2012 | B | Total | M | 0.12 |
| 2012 | G | Total | M | 22.5 |
| 2012 | S | Total | M | 23.7 |
| 2012 | A | Total | F |
| Id_1 | Id_2 | Id_3 | Id_4 | Me_1 |
|---|---|---|---|---|
| 2012 | S | Total | F | 25.8 |
| 2012 | F | Total | F | |
| 2012 | I | Total | F | 20.9 |
| 2012 | B | Total | M | 0.12 |
| 2012 | G | Total | M | 22.5 |
| 2012 | S | Total | M | 23.7 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · input/DS_2.csv · input/DS_2.json · input/DS_3.csv · input/DS_3.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
if DS then DS|sc else sc|DS 🟢 · boolean, any scalar → any type
Scalar else feeds all measures 🟢 pandas ✅ · DuckDB ✅
- Tests:
if ( DS_1#Me_1 > 10 ) then DS_1 else 0: a data set as thenOperand and a scalar as elseOperand, on a data set with two measures.- Manual:
if-then-else: “If one of the operands thenOperand or elseOperand is scalar, such a scalar value can be returned (depending on the outcome of the condition) and in this case it feeds the values of all the Measures of the result Data Point.”
- Expected:
ids 2 and 3, where
Me_1is above 10, keep their data point ofDS_1; id 1 gets the scalar in both measures, (1, 0, 0). The measures are Integer, like the scalar 0: “the Measures of the operand Data Set must be all of the same scalar type as the scalar operand”.
DS_r := if ( DS_1#Me_1 > 10 ) then DS_1 else 0;
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | 5 | 50 |
| 2 | 15 | 150 |
| 3 | 25 | 250 |
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | 0 | 0 |
| 2 | 15 | 150 |
| 3 | 25 | 250 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
Scalar then feeds all measures 🟢 pandas ✅ · DuckDB ✅
- Tests:
if ( DS_1#Me_1 > 10 ) then 0 else DS_1: the scalar as thenOperand and the data set as elseOperand, on the same data set with two measures.- Manual:
if-then-else: “at least one of thenOperand and elseOperand is a Data Set (the other one can be scalar)”, and the scalar “feeds the values of all the Measures of the result Data Point”.
- Expected:
ids 2 and 3 are true and get the scalar in both measures, (2, 0, 0) and (3, 0, 0); id 1 is false and keeps its data point of
DS_1, (1, 5, 50).
DS_r := if ( DS_1#Me_1 > 10 ) then 0 else DS_1;
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | 5 | 50 |
| 2 | 15 | 150 |
| 3 | 25 | 250 |
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | 5 | 50 |
| 2 | 0 | 0 |
| 3 | 0 | 0 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
if comp then comp else comp 🟢 · boolean, any scalar → any type
Larger of two components 🟢 pandas ✅ · DuckDB ✅
- Tests:
if Me_1 > Me_2 then Me_1 else Me_2insidecalc: component branches, in the page’s own syntaxif A > B then A else B.- Manual:
if-then-else: “For operations at Component level, the operation is applied for each Data Point of the unique input Data Set, the if-then-else operator returns the value from the thenOperand Component when condition evaluates to true, otherwise it returns the value from the elseOperand Component.”
- Expected:
Me_3is the larger measure: 10, 8 and 7. WhereMe_1 > Me_2is false, as in the last data point where both are 7, the result isMe_2.
DS_r := DS_1[calc Me_3 := if Me_1 > Me_2 then Me_1 else Me_2];
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | 10 | 5 |
| 2 | 3 | 8 |
| 3 | 7 | 7 |
| Id_1 | Me_1 | Me_2 | Me_3 |
|---|---|---|---|
| 1 | 10 | 5 | 10 |
| 2 | 3 | 8 | 8 |
| 3 | 7 | 7 | 7 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
if comp then sc else sc 🟢 · boolean, any scalar → any type
Flag from a threshold 🟢 pandas ✅ · DuckDB ✅
- Tests:
if Me_1 > 10 then 1 else 0insidecalc: scalar branches, the usual way to build a flag.- Manual:
if-then-else: “If one of the operands thenOperand or elseOperand is scalar, such a scalar value can be returned depending on the outcome of the condition.” Here both are.
- Expected:
Me_2is 0, 1 and 0: only 15 is above 10, and 10 itself is not.
DS_r := DS_1[calc Me_2 := if Me_1 > 10 then 1 else 0];
| Id_1 | Me_1 |
|---|---|
| 1 | 5 |
| 2 | 15 |
| 3 | 10 |
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | 5 | 0 |
| 2 | 15 | 1 |
| 3 | 10 | 0 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
if sc then sc else sc 🟢 · boolean, any scalar → any type
True condition gives then operand 🟢 pandas ✅ · DuckDB ✅
- Tests:
if SC_1 > SC_2 then 2 else 5withSC_1= 3 andSC_2= 0: a true condition on scalars.- Manual:
if-then-else: “The if operator returns thenOperand if condition evaluates to true, elseOperand otherwise”, for example “if x1 > x2 then 2 else 5, for x1 = 3, x2 =0 it returns 2” (
SC_1andSC_2stand for x1 and x2).- Expected:
SC_r = 2, an Integer:SC_1 > SC_2(3 > 0) is true, so the result is the thenOperand.
SC_r := if SC_1 > SC_2 then 2 else 5;
| SC_1 | SC_2 |
|---|---|
| 3 | 0 |
Name |
Type |
Value |
|---|---|---|
|
Integer |
|
Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗
False condition gives else operand 🟢 pandas ✅ · DuckDB ✅
- Tests:
if SC_1 > SC_2 then 2 else 5withSC_1= 0 andSC_2= 3: a false condition on scalars.- Manual:
if-then-else: “The if operator returns thenOperand if condition evaluates to true, elseOperand otherwise”, for example “if x1 > x2 then 2 else 5, for x1 = 0, x2 =3 it returns 5” (
SC_1andSC_2stand for x1 and x2).- Expected:
SC_r = 5, an Integer:SC_1 > SC_2(0 > 3) is false, so the result is the elseOperand.
SC_r := if SC_1 > SC_2 then 2 else 5;
| SC_1 | SC_2 |
|---|---|
| 0 | 3 |
Name |
Type |
Value |
|---|---|---|
|
Integer |
|
Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗