if-then-else: if
Syntax
if condition then thenOperand else elseOperand
Input parameters
condition |
a Boolean condition (dataset, component or scalar) |
thenOperand |
the operand returned when condition evaluates to true |
elseOperand |
the operand returned when condition evaluates to false |
Examples of valid syntaxes
if A > B then A else B
Semantics for scalar operations
The if operator returns thenOperand if condition evaluates to true, elseOperand otherwise. For example, considering the statement:
if x1 > x2 then 2 else 5,
for x1 = 3, x2 =0 it returns 2
for x1 = 0, x2 =3 it returns 5
Input parameters type
condition
dataset { measure <boolean> _ }
| component<Boolean>
| boolean
thenOperand
dataset
| component
| scalar
elseOperand
dataset
| component
| scalar
Result type
result
dataset
| component
| scalar
Additional Constraints
The operands thenOperand and elseOperand must be of the same scalar type.
If the operation is at scalar level, thenOperand and elseOperand are scalar then condition must be scalar too (a boolean scalar).
If the operation is at Component level, at least one of thenOperand and elseOperand is a Component (the other one can be scalar) and condition must be a Component too (a boolean Component); thenOperand, elseOperand and the other Components referenced in condition must belong to the same Data Set.
If the operation is at Data Set level, at least one of thenOperand and elseOperand is a Data Set (the other one can be scalar) and condition must be a Data Set too (having a unique boolean Measure) and must have the same Identifiers as thenOperand or/and ElseOperand
If thenOperand and elseOperand are both Data Sets then they must have the same Components in the same roles
If one of thenOperand and elseOperand is a Data Set and the other one is a scalar, the Measures of the operand Data Set must be all of the same scalar type as the scalar operand.
Behaviour
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. If one of the operands thenOperand or elseOperand is scalar, such a scalar value can be returned depending on the outcome of the condition.
For operations at Data Set level, the if-then-else operator 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. 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.
The behaviour for two Data Sets can be procedurally explained as follows. First the condition Data Set is evaluated, then 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 (the condition ensures that there cannot be conflicts in the union).
Examples
Given the operand Data Sets DS_cond, DS_1 and DS_2:
Input DS_1 (see structure)
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 |
Input DS_2 (see structure)
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 |
Input DS_COND (see structure)
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 |
Example 1
DS_r := if ( DS_cond#Id_4 = "F" ) then DS_1 else DS_2;
results in (see structure):
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 |