Skip to main content
Announcements
New: No-code data prep in Qlik Cloud Analytics™ TAKE A TOUR
cancel
Showing results for 
Search instead for 
Did you mean: 
Serphentelm
Contributor III
Contributor III

Manage if-else statement in tmap that involves multiple fields

Hi, how can I easily manage this code sample in tmap?

If(my_input.field1 > 6){

my_output.field1 = ...

else if

(my_input.field3 > 6 && my_input.field2 != null)

my_output.field1 = ...

my_output.field2 = ...

my_output.field3 = ...

my_output.field4 = ...

} else if{...

}

and so on.

Do I have to cover all the path in whitch my code will fall?

for example: (my_input.field6 > 6 && my_input.field3 < 6 &&

my_input.field2 == null)?first_if_block:(all condition meeting second block):etc???

 

This is quite unintuitive being used to manage all the case with java code

Labels (2)
1 Solution

Accepted Solutions
anselmopeixoto
Partner - Creator III
Partner - Creator III

Hello @matteo marchesi​ 

 

If you have different conditions for each column on your mapping, you'll have to cover all the path in each output column just like in your example.

 

To simplify complex conditions, you can use the Var section of tMap and create variables to store the test results.

 

Let's say you have a condition to identify a valid transaction using the example you provided:

 

my_input.field6 > 6 && my_input.field3 < 6 && my_input.field2 == null

 

Then you create a boolean variable on Var section called "valid_transaction", use the condition above there and then you can reuse this variable on the output columns instead of rewriting the same condition for each output column you need it.

 

It will become something like this:

 

Var.valid_transaction? first_if_block:(all condition meeting second block): etc...

 

Now, if you have the same conditions applying for all output columns, I suggest you use a "join table" output.

 

Click on the "add" icon on the upper right section of tMap window, select the option "Create join table from" and select an existing output.

 

0695b00000kYQnQAAW.png 

Then you can define different conditions for each output schema using the expression filter, apply different transformations for each column of the schema and in the end, all these output schemas will be joined to a single tMap output row that is the output schema from which the others were derived.

 

0695b00000kYQnpAAG.png 

This is the closest to an if-else structure like this:

 

If(my_input.field1== 1) {

my_output.field1 = my_input.field1

my_output.field2 = my_input.field2 * 10

my_output.field3 = my_input.field3 * my_input.field4

my_output.field4 = my_input.field4.substring(0, 9)

}

else if (my_input.field1 == 2) {

my_output.field1 = my_input.field1

my_output.field2 = my_input.field2 * 20

my_output.field3 = my_input.field3 * my_input.field2

my_output.field4 = my_input.field4.substring(10, 19)

}

 

View solution in original post

2 Replies
anselmopeixoto
Partner - Creator III
Partner - Creator III

Hello @matteo marchesi​ 

 

If you have different conditions for each column on your mapping, you'll have to cover all the path in each output column just like in your example.

 

To simplify complex conditions, you can use the Var section of tMap and create variables to store the test results.

 

Let's say you have a condition to identify a valid transaction using the example you provided:

 

my_input.field6 > 6 && my_input.field3 < 6 && my_input.field2 == null

 

Then you create a boolean variable on Var section called "valid_transaction", use the condition above there and then you can reuse this variable on the output columns instead of rewriting the same condition for each output column you need it.

 

It will become something like this:

 

Var.valid_transaction? first_if_block:(all condition meeting second block): etc...

 

Now, if you have the same conditions applying for all output columns, I suggest you use a "join table" output.

 

Click on the "add" icon on the upper right section of tMap window, select the option "Create join table from" and select an existing output.

 

0695b00000kYQnQAAW.png 

Then you can define different conditions for each output schema using the expression filter, apply different transformations for each column of the schema and in the end, all these output schemas will be joined to a single tMap output row that is the output schema from which the others were derived.

 

0695b00000kYQnpAAG.png 

This is the closest to an if-else structure like this:

 

If(my_input.field1== 1) {

my_output.field1 = my_input.field1

my_output.field2 = my_input.field2 * 10

my_output.field3 = my_input.field3 * my_input.field4

my_output.field4 = my_input.field4.substring(0, 9)

}

else if (my_input.field1 == 2) {

my_output.field1 = my_input.field1

my_output.field2 = my_input.field2 * 20

my_output.field3 = my_input.field3 * my_input.field2

my_output.field4 = my_input.field4.substring(10, 19)

}

 

Serphentelm
Contributor III
Contributor III
Author

Hi @Anselmo Peixoto​ , kind as always.

I've tought on using variables in tMap to simplify the logic.

Didn't thought about the join tale in output.

 

Already with the variable thought it's way more readable. I'll evaluate the join options if I'll get more complex if-else.