if-then-else · if then else

🟢 5 of 5 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc3 (9e1c81a6) · pandas 2.3.3 · DuckDB 1.5.5
🟢 all ok🟡 partially implemented🛡️ refused with an error🔴 not implemented or crash🔵 engines differ ✅ match · ❌ wrong result · 🛡 guarded error (code) · 💥 crash · 📌 team decision
Dataset formsComponent formsScalar forms
        ---
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_1 and DS_2 where the script uses DS_1, DS_2 and DS_3, a slip of the prose and not of the example, and the page’s TimePeriod type is written Time_Period, as vtlengine does.

Expected:

the page’s six data points: those of DS_2 where Id_4 is F, for Id_2 S, F and I (25.8, null and 20.9), and those of DS_3 where it is M, for Id_2 B, 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 with Id_4 F in DS_2, is left out.

script.vtl
DS_r := if ( DS_1#Id_4 = "F" ) then DS_2 else DS_3;
Input DS_1
Id_1Id_2Id_3Id_4Me_1
2012BTotalM5451780
2012BTotalF5643070
2012GTotalM5449803
2012GTotalF5673231
2012STotalM23099012
2012STotalF23719207
2012FTotalM31616281
2012FTotalF33671580
2012ITotalM28726599
2012ITotalF30667608
2012ATotalM
2012ATotalF
Input DS_2
Id_1Id_2Id_3Id_4Me_1
2012STotalF25.8
2012FTotalF
2012ITotalF20.9
2012ATotalM6.3
Input DS_3
Id_1Id_2Id_3Id_4Me_1
2012BTotalM0.12
2012GTotalM22.5
2012STotalM23.7
2012ATotalF
Expected DS_r
Id_1Id_2Id_3Id_4Me_1
2012STotalF25.8
2012FTotalF
2012ITotalF20.9
2012BTotalM0.12
2012GTotalM22.5
2012STotalM23.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_1 is above 10, keep their data point of DS_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”.

script.vtl
DS_r := if ( DS_1#Me_1 > 10 ) then DS_1 else 0;
Input DS_1
Id_1Me_1Me_2
1550
215150
325250
Expected DS_r
Id_1Me_1Me_2
100
215150
325250

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).

script.vtl
DS_r := if ( DS_1#Me_1 > 10 ) then 0 else DS_1;
Input DS_1
Id_1Me_1Me_2
1550
215150
325250
Expected DS_r
Id_1Me_1Me_2
1550
200
300

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_2 inside calc: component branches, in the page’s own syntax if 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_3 is the larger measure: 10, 8 and 7. Where Me_1 > Me_2 is false, as in the last data point where both are 7, the result is Me_2.

script.vtl
DS_r := DS_1[calc Me_3 := if Me_1 > Me_2 then Me_1 else Me_2];
Input DS_1
Id_1Me_1Me_2
1105
238
377
Expected DS_r
Id_1Me_1Me_2Me_3
110510
2388
3777

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 0 inside calc: 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_2 is 0, 1 and 0: only 15 is above 10, and 10 itself is not.

script.vtl
DS_r := DS_1[calc Me_2 := if Me_1 > 10 then 1 else 0];
Input DS_1
Id_1Me_1
15
215
310
Expected DS_r
Id_1Me_1Me_2
150
2151
3100

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 5 with SC_1 = 3 and SC_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_1 and SC_2 stand for x1 and x2).

Expected:

SC_r = 2, an Integer: SC_1 > SC_2 (3 > 0) is true, so the result is the thenOperand.

script.vtl
SC_r := if SC_1 > SC_2 then 2 else 5;
Input scalars
SC_1SC_2
30
Expected scalars

Name

Type

Value

SC_r

Integer

"2"

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 5 with SC_1 = 0 and SC_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_1 and SC_2 stand for x1 and x2).

Expected:

SC_r = 5, an Integer: SC_1 > SC_2 (0 > 3) is false, so the result is the elseOperand.

script.vtl
SC_r := if SC_1 > SC_2 then 2 else 5;
Input scalars
SC_1SC_2
03
Expected scalars

Name

Type

Value

SC_r

Integer

"5"

Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗