Case · case when
---
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 fromDS_2. All three ids are in both branches, so none is left out.
DS_r := case when DS_1 > 10 then DS_2 else DS_3;
| Id_1 | Me_1 |
|---|---|
| 1 | 5 |
| 2 | 15 |
| 3 | 25 |
| Id_1 | Me_1 |
|---|---|
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
| Id_1 | Me_1 |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| Id_1 | Me_1 |
|---|---|
| 1 | 1 |
| 2 | 200 |
| 3 | 300 |
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 conditionDS_c := DS_1 > 10has a data point,Id_1 = 1, thatDS_3lacks. 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, andDS_3has 4 but not 1, so id 1 is left out.DS_3’s values for ids 2 and 3 are not used.
DS_c := DS_1 > 10;
DS_r := case when DS_c then DS_2 else DS_3;
| Id_1 | Me_1 |
|---|---|
| 1 | 5 |
| 2 | 15 |
| 3 | 25 |
| 4 | 8 |
| Id_1 | Me_1 |
|---|---|
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
| 4 | 400 |
| Id_1 | Me_1 |
|---|---|
| 2 | 22 |
| 3 | 33 |
| 4 | 44 |
| Id_1 | Me_1 |
|---|---|
| 2 | 200 |
| 3 | 300 |
| 4 | 44 |
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, whereDS_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.
DS_c := DS_1#Me_1 > 10;
DS_r := case when DS_c 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:
case when DS_c then 0 else DS_1, whereDS_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 nowhenis true.
DS_c := DS_1#Me_1 > 10;
DS_r := case when DS_c 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 ↗
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 fromDS_3; id 3 meets both, and the first wins, soDS_2’s 300, notDS_3’s 3. No recorded team decision applies, so the expected output follows the manual’s “first”.
DS_r := case when DS_1 > 20 then DS_2 when DS_1 > 10 then DS_3 else DS_1;
| Id_1 | Me_1 |
|---|---|
| 1 | 5 |
| 2 | 15 |
| 3 | 25 |
| Id_1 | Me_1 |
|---|---|
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
| Id_1 | Me_1 |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| Id_1 | Me_1 |
|---|---|
| 1 | 5 |
| 2 | 2 |
| 3 | 300 |
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 > 20andDS_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, notDS_3’s 3. No recorded team decision applies, so the expected output follows the manual’s “first”.
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;
| Id_1 | Me_1 |
|---|---|
| 1 | 5 |
| 2 | 15 |
| 3 | 25 |
| Id_1 | Me_1 |
|---|---|
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
| Id_1 | Me_1 |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| Id_1 | Me_1 |
|---|---|
| 1 | 5 |
| 2 | 2 |
| 3 | 300 |
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_2insidecalc: onewhenwith 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_3is the smaller measure: 5, 3 and 7.Me_1 < Me_2is true only for 3 < 8; for 10 and 5, and for 7 and 7, theelsegivesMe_2.
DS_r := DS_1[calc Me_3 := case when 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 | 5 |
| 2 | 3 | 8 | 3 |
| 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 ↗
case when comp then sc when comp then sc ... else sc 🟢 · boolean, any scalar → any type
Manual example 1 🟢 pandas ✅ · DuckDB ✅
- Tests:
casewith threewhenclauses insidecalc, 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_2is 0, 1, 10 and 100: each non-nullMe_1meets one condition, and the null meets none, as a comparison with null is not true, so theelsegives 100. The three conditions exclude each other, so this example cannot tell the first truewhenfrom the last: “Overlapping descending thresholds” does.
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];
| Id_1 | Me_1 |
|---|---|
| 1 | 0.12 |
| 2 | 3.5 |
| 3 | 10.7 |
| 4 |
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | 0.12 | 0 |
| 2 | 3.5 | 1 |
| 3 | 10.7 | 10 |
| 4 | 100 |
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 0insidecalc: two conditions that overlap, the usual descending-threshold form, to tell the first truewhenfrom 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_2is 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”.
DS_r := DS_1[calc Me_2 := case when Me_1 > 10 then 10 when Me_1 > 1 then 1 else 0];
| Id_1 | Me_1 |
|---|---|
| 1 | 0 |
| 2 | 5 |
| 3 | 15 |
| Id_1 | Me_1 | Me_2 |
|---|---|---|
| 1 | 0 | 0 |
| 2 | 5 | 1 |
| 3 | 15 | 10 |
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 5withSC_1= 3 andSC_2= 0: onewhen, 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 is3 > 0, is true, so the result is its thenOperand.
SC_r := case when 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:
case when SC_1 > SC_2 then 2 else 5withSC_1= 0 andSC_2= 3: onewhen, 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 is0 > 3, is false and there is no otherwhen, so the result is the elseOperand.
SC_r := case when 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 ↗
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 5withSC_1= 3 andSC_2= 0: twowhenclauses, 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_1andSC_2stand for x1 and x2).- Expected:
SC_r = 2, an Integer: the first conditionSC_1 > SC_2, that is3 > 0, is true, so the result is its thenOperand.
SC_r := case when SC_1 > SC_2 then 2 when SC_1 = SC_2 then 0 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 ↗
Second condition true 🟢 pandas ✅ · DuckDB ✅
- Tests:
case when SC_1 > SC_2 then 2 when SC_1 = SC_2 then 0 else 5withSC_1= 3 andSC_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_1andSC_2stand for x1 and x2).- Expected:
SC_r = 0, an Integer:SC_1 > SC_2(3 > 3) is false andSC_1 = SC_2(3 = 3) is true, so the result is the thenOperand of the secondwhen.
SC_r := case when SC_1 > SC_2 then 2 when SC_1 = SC_2 then 0 else 5;
| SC_1 | SC_2 |
|---|---|
| 3 | 3 |
Name |
Type |
Value |
|---|---|---|
|
Integer |
|
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 5withSC_1= 0 andSC_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_1andSC_2stand for x1 and x2).- Expected:
SC_r = 5, an Integer: neitherSC_1 > SC_2(0 > 3) norSC_1 = SC_2(0 = 3) is true, so the result is the elseOperand.
SC_r := case when SC_1 > SC_2 then 2 when SC_1 = SC_2 then 0 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 ↗
Both conditions true 🟢 pandas ✅ · DuckDB ✅
- Tests:
case when SC_1 > 10 then 10 when SC_1 > 1 then 1 else 0withSC_1= 15: two conditions that are both true, the usual descending-threshold form, to tell the first truewhenfrom 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 > 10andSC_1 > 1are 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”.
SC_r := case when SC_1 > 10 then 10 when SC_1 > 1 then 1 else 0;
| SC_1 |
|---|
| 15 |
Name |
Type |
Value |
|---|---|---|
|
Integer |
|
Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗