Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
MAlex1
Contributor
Contributor

An issue with AND operator in an expression

Hello everybody,

 

I am struggling with some issues in tMap expression. There is the table with those fields:

 

ANRED (the title of person) with those values:

MR 1
MISS 2
MRS 3
MS 4
DR null

 

GESCH (gender key) with those values:

Female 2
Male 1

 

For the case when ANRED value is null I need to use this rule for output:

 

IF ANRED = null AND GESCH = 2 then output_value = 4 ELSE output_value = 1.

 

This is my expression:

(Basic_G2_Structure2.ANRED==null)&&(Basic_G2_Structure2.GESCH!="2")?"1":"4"

 

0683p000009M8O1.png

 

Unfortunately, the output from the expression is always '1' for all ANRED values although the values are correct on input.

 

0683p000009M97I.png

 

I guess I have something wrong in the expression but I do not know what.

 

Could you someone advise me, please?

 

Thank you.

 

/Martin

Labels (2)
1 Solution

Accepted Solutions
Ganshyam
Creator II
Creator II

Hello @MAlex 

I tried below condition to get your expected result.Try it out and validate against your input data.

 

(row1.ANRED.equals("null")|| row1.ANRED.equals("1"))&& row1.GESCH.equals("1")?"1" :
row1.ANRED.equals("null")&& !row1.GESCH.equals("1")?"4":row1.ANRED

 

Regards

 

View solution in original post

17 Replies
TRF
Champion II
Champion II

Nevers use == or != to compare 2 strings, use String.equal() method instead.
(Basic_G2_Structure2.ANRED==null)&&(!"2".equals(Basic_G2_Structure2.GESCH))?"1":"4"
MAlex1
Contributor
Contributor
Author

Hi TRF,

 

thank you for your quick reply. Yes, the issue was caused by the incorrect expression - thanks for your advice! However, it still doesn't work as it should.  It seems that not all of the context was clear from my post.

 

By the expression are changed all values which should be excluded for transformation. Here is an example. Let's have those five records in the input table (Basic_G2_Structure2😞

 

Basic_G2_Structure2

ID

ANRED

GESCH

100

1

1

101

4

2

103

null

1

104

2

2

105

null

2

 

By using the expression:

 

(Basic_G2_Structure2.ANRED==null)&&("2".equals(Basic_G2_Structure2.GESCH))?"1":"4"

 

I would expect this output in the output table (Final_G2_Structure)

 

Final_G2_Structure

ID

ANRED

GESCH

100

1

1

101

4

2

103

1

1

104

2

2

105

4

1

 

because the condition is ANRED=null AND GESCH=2 (both have to be TRUE).

 

Unfortunately, the output is the following:

 

Final_G2_Structure

ID

ANRED

GESCH

100

4

1

101

4

2

103

4

1

104

4

2

105

4

1

 

For some reason are impacted records which should be excluded from evaluation.

 

Thank you for your help again.

Ganshyam
Creator II
Creator II

Hi,

 

Condition in Tmap - Component

row1.ANRED.equals("")&& row1.GESCH.equals("1")?"1" :
row1.ANRED.equals("")&& !row1.GESCH.equals("1")?"4":row1.ANRED

 

0683p000009M8xi.png

 

Hope this solves your purpose.

Ganshyam

MAlex1
Contributor
Contributor
Author

Hi Ganshyam,

 

thank you for your reply. I am sorry for the late response but I was busy with another critical issue in the past two weeks.

 

I have tried to use your expression:

 

Basic_G2_Structure2.ANRED.equals("")&& Basic_G2_Structure2.GESCH.equals("1")?"1":Basic_G2_Structure2.ANRED.equals("")&& !Basic_G2_Structure2.GESCH.equals("1")?"4":Basic_G2_Structure2.ANRED

 

but the execution of the job already finishes with the error:

 

Starting job G2_Data_IT0002 at 17:40 09/03/2020.

[statistics] connecting to socket on port 3892
[statistics] connected
[FATAL]: xxx_uk.g2_data_it0002_0_1.G2_Data_IT0002 - tMap_3 null
java.lang.NullPointerException
at xxx_uk.g2_data_it0002_0_1.G2_Data_IT0002.tFileInputExcel_2Process(G2_Data_IT0002.java:10764)
at xxx_uk.g2_data_it0002_0_1.G2_Data_IT0002.tFileInputExcel_1Process(G2_Data_IT0002.java:2131)
at xxx_uk.g2_data_it0002_0_1.G2_Data_IT0002.tPrejob_1Process(G2_Data_IT0002.java:14596)
at xxx_uk.g2_data_it0002_0_1.G2_Data_IT0002.runJobInTOS(G2_Data_IT0002.java:15015)
at xxx_uk.g2_data_it0002_0_1.G2_Data_IT0002.main(G2_Data_IT0002.java:14683)
Exception in component tMap_3 (G2_Data_IT0002)
java.lang.NullPointerException
at xxx_uk.g2_data_it0002_0_1.G2_Data_IT0002.tFileInputExcel_2Process(G2_Data_IT0002.java:10764)
at xxx_uk.g2_data_it0002_0_1.G2_Data_IT0002.tFileInputExcel_1Process(G2_Data_IT0002.java:2131)
at xxx_uk.g2_data_it0002_0_1.G2_Data_IT0002.tPrejob_1Process(G2_Data_IT0002.java:14596)
at xxx_uk.g2_data_it0002_0_1.G2_Data_IT0002.runJobInTOS(G2_Data_IT0002.java:15015)
at xxx_uk.g2_data_it0002_0_1.G2_Data_IT0002.main(G2_Data_IT0002.java:14683)
[statistics] disconnected
Job G2_Data_IT0002 ended at 17:40 09/03/2020. [exit code=1]

 

It looks the expression is returning a null value:

0683p000009MA2J.png

But maybe my listing of the expression is bad.

Thank you for your support.

manodwhb
Champion II
Champion II

@MAlex , below the below way.

 

("").equals(row1.ANRED)&& ("1").equals(row1.GESCH)?"1" :
("").equals(row1.ANRED)&& !("1").equals(row1.GESCH)?"4":row1.ANRED

MAlex1
Contributor
Contributor
Author

Hi Manohar,

 

thank you. The output on the console is without error now. Unfortunately, the result is not still met my expectations, my explanation is not enough accurate probably.

 

I have those values in the input (Basic_G2_Structure):

 

|=----+-----+-----+-----+-----+-----+-----+
|RECTY|CLIID|CNAME|RUFNM|KNZNM|ANRED|GESCH|
|=----+-----+-----+-----+-----+-----+-----+
|P0002|10201|null |null |null |1    |1    |
|P0002|42762|null |null |null |null |1    |
|P0002|43875|null |null |null |null |2    |

|=----+-----+-----+-----+-----+-----+-----+

 

and I need this output:

 

|=----+-----+-----+-----+-----+-----+-----+
|RECTY|CLIID|CNAME|RUFNM|KNZNM|ANRED|GESCH|
|=----+-----+-----+-----+-----+-----+-----+
|P0002|10201|null |null |null |1    |1    |
|P0002|42762|null |null |null |   |1    |
|P0002|43875|null |null |null |4    |2    |

|=----+-----+-----+-----+-----+-----+-----+

 

So,  some expression like this (pseudo-code):

 

IF ANRED = null AND IF GESCH = "1" THEN ANRED = "1" ELSE ANRED = "4"

 

Thank you for your support.

 

/Martin

 

 

 

 

 

manodwhb
Champion II
Champion II

@MAlex , try the below one and let me know.

 

(row1.ANRED==null || ("").equals(row1.ANRED))&& ("1").equals(row1.GESCH)?"1" :"4"

MAlex1
Contributor
Contributor
Author

Hi Manohar,

 

the result is better but another issue has appeared now.

 

Here is my input  (Basic_G2_Structure):

 

|=----+-----+-----+-----+-----+-----+-----+

|RECTY|CLIID|CNAME|RUFNM|KNZNM|ANRED|GESCH|

|=----+-----+-----+-----+-----+-----+-----+

|P0002|10201|null |null |null |1    |1    |

|P0002|42762|null |null |null |null |1    |

|P0002|43875|null |null |null |null |2    |

|=----+-----+-----+-----+-----+-----+-----+

 

here is the expression:

 

(Basic_G2_Structure2.ANRED==null || ("").equals(Basic_G2_Structure2.ANRED))&& ("1").equals(Basic_G2_Structure2.GESCH)?"1" :"4"

 

and here is the output after processing:

 

|=----+-----+-----+-----+-----+-----+-----+

|RECTY|CLIID|CNAME|RUFNM|KNZNM|ANRED|GESCH|

|=----+-----+-----+-----+-----+-----+-----+

|P0002|10201|null |null |null |4    |1    |

|P0002|42762|null |null |null |1    |1    |

|P0002|43875|null |null |null |4    |2    |

|=----+-----+-----+-----+-----+-----+-----+

 

So, for CLIID 42762 and 43875 is the result according to my expectation. But, unfortunately, for the CLIID 10201, the value for ANRED was changed to '4':

|=----+-----+-----+-----+-----+-----+-----+

|RECTY|CLIID|CNAME|RUFNM|KNZNM|ANRED|GESCH|

|=----+-----+-----+-----+-----+-----+-----+

|P0002|10201|null |null |null |4    |1    |

|P0002|42762|null |null |null |1    |1    |

|P0002|43875|null |null |null |4    |2    |

|=----+-----+-----+-----+-----+-----+-----+

 

although according to condition statement it should be excluded from processing because:

 

IF ANRED = null AND IF GESCH = "1" THEN ANRED = "1" ELSE ANRED = "4".

 

So, the correct output should be:

|=----+-----+-----+-----+-----+-----+-----+

|RECTY|CLIID|CNAME|RUFNM|KNZNM|ANRED|GESCH|

|=----+-----+-----+-----+-----+-----+-----+

|P0002|10201|null |null |null |1    |1    |

|P0002|42762|null |null |null |1    |1    |

|P0002|43875|null |null |null |4    |2    |

|=----+-----+-----+-----+-----+-----+-----+

Could you help me overcome this last obstacle, please?

Thank you.

/Martin

manodwhb
Champion II
Champion II

@MAlex ,if you want that you then your expression should as below .

 

(Basic_G2_Structure2.ANRED==null || ("").equals(Basic_G2_Structure2.ANRED)|| ("1").equals(Basic_G2_Structure2.ANRED))&& ("1").equals(Basic_G2_Structure2.GESCH)?"1" :"4"

 

Input

|=----+-----+-----+-----+-----+-----+-----+

|RECTY|CLIID|CNAME|RUFNM|KNZNM|ANRED|GESCH|

|=----+-----+-----+-----+-----+-----+-----+

|P0002|10201|null |null |null |1    |1    |

|P0002|42762|null |null |null |null |1    |

|P0002|43875|null |null |null |null |2    |

|=----+-----+-----+-----+-----+-----+-----+

Output

|=----+-----+-----+-----+-----+-----+-----+

|RECTY|CLIID|CNAME|RUFNM|KNZNM|ANRED|GESCH|

|=----+-----+-----+-----+-----+-----+-----+

|P0002|10201|null |null |null |1    |1    |

|P0002|42762|null |null |null |1    |1    |

|P0002|43875|null |null |null |4    |2    |

|=----+-----+-----+-----+-----+-----+-----+