Case · case when

🟢 7 of 7 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc2 (90ee9376) · 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["Case · case when<br/>🟢7"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_ds["case when DS then DS else DS"]:::ok
        v_ds_scalar_branch["case when DS then DS#124;sc else sc#124;DS"]:::ok
        v_ds_multi_when["case when DS then DS when DS then DS ... else DS"]:::ok
    end
    subgraph G_comp["Component"]
        direction LR
        v_comp["case when comp then comp else comp"]:::ok
        v_comp_multi_when["case when comp then sc when comp then sc ... else sc"]:::ok
    end
    subgraph G_sc["Scalar"]
        direction LR
        v_sc["case when sc then sc else sc"]:::ok
        v_sc_multi_when["case when sc then sc when 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_ds_multi_when href "#form-ds-multi-when"
    click v_comp href "#form-comp"
    click v_comp_multi_when href "#form-comp-multi-when"
    click v_sc href "#form-sc"
    click v_sc_multi_when href "#form-sc-multi-when"
    

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

case when DS then DS else DS 🟢 · boolean, any scalar → any type

Inline dataset condition 🟢 pandas ✅ · DuckDB ✅
Tests:

case when DS_1 > 10 then DS_2 else DS_3: a data set condition written inline, with data sets as branches.

Manual:

Case: “The same rules apply as for the if-then-else operator”, and “its true Data Points are inner joined with thenOperand and its false Data Points are inner joined with elseOperand, finally the union is made of these two partial results”.

Expected:

(1, 1), (2, 200) and (3, 300): id 1 is false (5), so it comes from DS_3; ids 2 and 3 are true (15 and 25), so they come from DS_2. All three ids are in both branches, so none is left out.

script.vtl
DS_r := case when DS_1 > 10 then DS_2 else DS_3;
Input DS_1
Id_1Me_1
15
215
325
Input DS_2
Id_1Me_1
1100
2200
3300
Input DS_3
Id_1Me_1
11
22
33
Expected DS_r
Id_1Me_1
11
2200
3300

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 ↗

Unmatched data point left out 🟢 pandas ✅ · DuckDB ✅
Tests:

case when DS_c then DS_2 else DS_3, where the condition DS_c := DS_1 > 10 has a data point, Id_1 = 1, that DS_3 lacks. The condition is named, so that the example shows only how the branches are joined and does not depend on how an engine handles an inline condition.

Manual:

Case: “its true Data Points are inner joined with thenOperand and its false Data Points are inner joined with elseOperand, finally the union is made of these two partial results”; and “Data Points that are not coupled are left out” (Typical behaviours).

Expected:

(2, 200), (3, 300) and (4, 44): ids 2 and 3 are true and come from DS_2; ids 1 and 4 are false, and DS_3 has 4 but not 1, so id 1 is left out. DS_3’s values for ids 2 and 3 are not used.

script.vtl
DS_c := DS_1 > 10;
DS_r := case when DS_c then DS_2 else DS_3;
Input DS_1
Id_1Me_1
15
215
325
48
Input DS_2
Id_1Me_1
1100
2200
3300
4400
Input DS_3
Id_1Me_1
222
333
444
Expected DS_r
Id_1Me_1
2200
3300
444

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 ↗

case when DS then DS|sc else sc|DS 🟢 · boolean, any scalar → any type

Scalar else feeds all measures 🟢 pandas ✅ · DuckDB ✅
Tests:

case when DS_c then DS_1 else 0, where DS_c := DS_1#Me_1 > 10: a data set as thenOperand and a scalar as elseOperand, on a data set with two measures. The condition is named, so that the example is about the scalar branch only.

Manual:

Case: “The same rules apply as for the if-then-else operator”, and a scalar operand “feeds the values of all the Measures of the result Data Point”.

Expected:

(1, 0, 0), (2, 15, 150) and (3, 25, 250): id 1, where 5 is not above 10, gets the scalar in both measures; ids 2 and 3 keep their data point of DS_1. The measures are Integer, like the scalar 0.

script.vtl
DS_c := DS_1#Me_1 > 10;
DS_r := case when DS_c 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:

case when DS_c then 0 else DS_1, where DS_c := DS_1#Me_1 > 10: the scalar as thenOperand and the data set as elseOperand. The condition is named, so that the example is about the scalar branch only.

Manual:

Case: the operator “returns the Data Point from elseOperand if none of the when conditions evaluates to true”, and a scalar operand “feeds the values of all the Measures of the result Data Point”.

Expected:

(2, 0, 0) and (3, 0, 0), where the condition is true, and (1, 5, 50), the data point of DS_1, where no when is true.

script.vtl
DS_c := DS_1#Me_1 > 10;
DS_r := case when DS_c 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 ↗

case when DS then DS when DS then DS ... else DS 🟢 · boolean, any scalar → any type

Overlapping inline conditions 🟢 pandas ✅ · DuckDB ✅
Tests:

case when DS_1 > 20 then DS_2 when DS_1 > 10 then DS_3 else DS_1: two overlapping conditions written inline, on data sets, to see which true condition wins.

Manual:

Case: at data set level the operator “returns the Data Point from the thenOperand when the first Data Point of condition having the same Identifiers’ values evaluates to true; returns the Data Point from elseOperand if none of the when conditions evaluates to true”.

Expected:

(1, 5), (2, 2) and (3, 300): id 1 meets no condition, so it keeps DS_1; id 2 meets only the second, so it comes from DS_3; id 3 meets both, and the first wins, so DS_2’s 300, not DS_3’s 3. No recorded team decision applies, so the expected output follows the manual’s “first”.

script.vtl
DS_r := case when DS_1 > 20 then DS_2 when DS_1 > 10 then DS_3 else DS_1;
Input DS_1
Id_1Me_1
15
215
325
Input DS_2
Id_1Me_1
1100
2200
3300
Input DS_3
Id_1Me_1
11
22
33
Expected DS_r
Id_1Me_1
15
22
3300

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 ↗

Overlapping named conditions 🟢 pandas ✅ · DuckDB ✅
Tests:

the two overlapping conditions of “Overlapping inline conditions”, named: DS_c1 := DS_1 > 20 and DS_c2 := DS_1 > 10. The example shows only the order in which they are tried, and does not depend on how an engine handles an inline condition.

Manual:

Case: at data set level the operator “returns the Data Point from the thenOperand when the first Data Point of condition having the same Identifiers’ values evaluates to true; returns the Data Point from elseOperand if none of the when conditions evaluates to true”.

Expected:

(1, 5), (2, 2) and (3, 300): id 3 meets both conditions, and the first wins, so DS_2’s 300, not DS_3’s 3. No recorded team decision applies, so the expected output follows the manual’s “first”.

script.vtl
DS_c1 := DS_1 > 20;
DS_c2 := DS_1 > 10;
DS_r := case when DS_c1 then DS_2 when DS_c2 then DS_3 else DS_1;
Input DS_1
Id_1Me_1
15
215
325
Input DS_2
Id_1Me_1
1100
2200
3300
Input DS_3
Id_1Me_1
11
22
33
Expected DS_r
Id_1Me_1
15
22
3300

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 ↗

case when comp then comp else comp 🟢 · boolean, any scalar → any type

Smaller of two components 🟢 pandas ✅ · DuckDB ✅
Tests:

case when Me_1 < Me_2 then Me_1 else Me_2 inside calc: one when with component branches, so the conditions cannot overlap.

Manual:

Case: at Component level “the operation is applied for each Data Point of the unique input Data Set, the case operator returns the value from the thenOperand Component whose corresponding condition evaluates to true; if none of the when conditions evaluates to true, it returns the value from the elseOperand Component”.

Expected:

Me_3 is the smaller measure: 5, 3 and 7. Me_1 < Me_2 is true only for 3 < 8; for 10 and 5, and for 7 and 7, the else gives Me_2.

script.vtl
DS_r := DS_1[calc Me_3 := case when 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
11055
2383
3777

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

case when comp then sc when comp then sc ... else sc 🟢 · boolean, any scalar → any type

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

case with three when clauses inside calc, returning scalars, on a measure that has a null.

Manual:

Case, Example 1, output verbatim: “the case operator returns the value from the thenOperand Component whose corresponding condition evaluates to true; if none of the when conditions evaluates to true, it returns the value from the elseOperand Component”.

Expected:

Me_2 is 0, 1, 10 and 100: each non-null Me_1 meets one condition, and the null meets none, as a comparison with null is not true, so the else gives 100. The three conditions exclude each other, so this example cannot tell the first true when from the last: “Overlapping descending thresholds” does.

script.vtl
DS_r := DS_1
   [calc Me_2 := 
         case when Me_1 <= 1 then 0
				      when Me_1 > 1 and Me_1 <= 10 then 1							
              when Me_1 > 10  then 10
              else 100];
Input DS_1
Id_1Me_1
10.12
23.5
310.7
4
Expected DS_r
Id_1Me_1Me_2
10.120
23.51
310.710
4100

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

Overlapping descending thresholds 🟢 pandas ✅ · DuckDB ✅
Tests:

case when Me_1 > 10 then 10 when Me_1 > 1 then 1 else 0 inside calc: two conditions that overlap, the usual descending-threshold form, to tell the first true when from the last.

Manual:

Case: “The case operator returns the first thenOperand whose corresponding condition evaluates to true, elseOperand if none of the when conditions evaluates to true.” At Component level “the operation is applied for each Data Point of the unique input Data Set”.

Expected:

Me_2 is 0, 1 and 10: for 15 both conditions hold, and the first gives 10, not 1. No recorded team decision applies, so the expected output follows the manual’s “first”.

script.vtl
DS_r := DS_1[calc Me_2 := case when Me_1 > 10 then 10 when Me_1 > 1 then 1 else 0];
Input DS_1
Id_1Me_1
10
25
315
Expected DS_r
Id_1Me_1Me_2
100
251
31510

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

case when sc then sc else sc 🟢 · boolean, any scalar → any type

True condition gives then operand 🟢 pandas ✅ · DuckDB ✅
Tests:

case when SC_1 > SC_2 then 2 else 5 with SC_1 = 3 and SC_2 = 0: one when, with a true condition.

Manual:

Case: “The case operator returns the first thenOperand whose corresponding condition evaluates to true, elseOperand if none of the when conditions evaluates to true.”

Expected:

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

script.vtl
SC_r := case when 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:

case when SC_1 > SC_2 then 2 else 5 with SC_1 = 0 and SC_2 = 3: one when, with a false condition.

Manual:

Case: “The case operator returns the first thenOperand whose corresponding condition evaluates to true, elseOperand if none of the when conditions evaluates to true.”

Expected:

SC_r = 5, an Integer: SC_1 > SC_2, that is 0 > 3, is false and there is no other when, so the result is the elseOperand.

script.vtl
SC_r := case when 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 ↗

case when sc then sc when sc then sc ... else sc 🟢 · boolean, any scalar → any type

First condition true 🟢 pandas ✅ · DuckDB ✅
Tests:

case when SC_1 > SC_2 then 2 when SC_1 = SC_2 then 0 else 5 with SC_1 = 3 and SC_2 = 0: two when clauses, the first one true.

Manual:

Case: “case when x1 > x2 then 2 when x1 = x2 then 0 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: the first condition SC_1 > SC_2, that is 3 > 0, is true, so the result is its thenOperand.

script.vtl
SC_r := case when SC_1 > SC_2 then 2 when SC_1 = SC_2 then 0 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 ↗

Second condition true 🟢 pandas ✅ · DuckDB ✅
Tests:

case when SC_1 > SC_2 then 2 when SC_1 = SC_2 then 0 else 5 with SC_1 = 3 and SC_2 = 3: the first condition false and the second one true.

Manual:

Case: “case when x1 > x2 then 2 when x1 = x2 then 0 else 5; for x1 = x2 = 3 it returns 0” (SC_1 and SC_2 stand for x1 and x2).

Expected:

SC_r = 0, an Integer: SC_1 > SC_2 (3 > 3) is false and SC_1 = SC_2 (3 = 3) is true, so the result is the thenOperand of the second when.

script.vtl
SC_r := case when SC_1 > SC_2 then 2 when SC_1 = SC_2 then 0 else 5;
Input scalars
SC_1SC_2
33
Expected scalars

Name

Type

Value

SC_r

Integer

"0"

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

No condition true 🟢 pandas ✅ · DuckDB ✅
Tests:

case when SC_1 > SC_2 then 2 when SC_1 = SC_2 then 0 else 5 with SC_1 = 0 and SC_2 = 3: no condition true.

Manual:

Case: “case when x1 > x2 then 2 when x1 = x2 then 0 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: neither SC_1 > SC_2 (0 > 3) nor SC_1 = SC_2 (0 = 3) is true, so the result is the elseOperand.

script.vtl
SC_r := case when SC_1 > SC_2 then 2 when SC_1 = SC_2 then 0 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 ↗

Both conditions true 🟢 pandas ✅ · DuckDB ✅
Tests:

case when SC_1 > 10 then 10 when SC_1 > 1 then 1 else 0 with SC_1 = 15: two conditions that are both true, the usual descending-threshold form, to tell the first true when from the last. In the other examples of this form at most one condition is true.

Manual:

Case: “The case operator returns the first thenOperand whose corresponding condition evaluates to true, elseOperand if none of the when conditions evaluates to true.”

Expected:

SC_r = 10, an Integer: SC_1 > 10 and SC_1 > 1 are both true for 15, and the first gives 10, not the 1 of the second. No recorded team decision applies, so the expected output follows the manual’s “first”.

script.vtl
SC_r := case when SC_1 > 10 then 10 when SC_1 > 1 then 1 else 0;
Input scalars
SC_1
15
Expected scalars

Name

Type

Value

SC_r

Integer

"10"

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