Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

if statement based on calculation

Hi,

I have a simple if statement which seem to be not working.

Im trying to create a new variable, which based on the results of 'Count' will assign a value to another one. If the count is 0, the value is D, otherwise it is A

I tried the following:

IF(COUNT([X_1])=' ','D','A') AS Var1

but it is not working. What is the correct way of doing this?

EDIT


to elaborate more on my question. I have 2 tables, which looks like this:

Table TD

CIDOID
1x12
1x23
1x32
2x41
2x53
3x62

The other table, looks like this:

Table CD

CIDCNAME
1John Smith
2Max Mustermann
3Edith Bingo
4Albert Reckel
5Sam Retro
6Teno

What i want to do, is to generate a new column for the 2nd table, which is based on the count of OID in the 1st table, i.e., this column should calculate, if count(OID) is NULL, the value should be D, else it should be A.

This is how my I try to do the left join at the moment:

left join (CD)

load

IF(COUNT([OID])=0,'Dormant','Active') AS Var1   

Resident TD

And this new Var1 variable, will be used in a plot afterwards.

Does this make things more clear now?

16 Replies
Anil_Babu_Samineni

Try this?

If(COUNT(X_1)=0,'D','A') AS Var1

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
matteo_mi
Partner - Contributor III
Partner - Contributor III

Hi,

go to settings, variable overview

and create the new variable with your needs.

Schermata 2017-10-18 alle 17.04.49.png

hope this  help

madhumitha
Creator
Creator

Hello Abdullah,

You cannot assign values to variables with 'AS' key word. It should be either 'LET' or 'SET'. You can also use variable overview tab to create new variables.

Hope this helps

Not applicable
Author

does not work.

Not applicable
Author

Im not assigning a value to a variable, I want the value to be calculated in the script based on this condition. Then I want to use this value for plotting.

Not applicable
Author

How can I then use this variable in a plot? will it adjust automatically to the dimension I use for the plot (e.g., the count for each ID should not be 0)?

matteo_mi
Partner - Contributor III
Partner - Contributor III

Hi Abdullah,

to use variable in this he script you should use  SET or LET as explained above by Madhu Mitha.

Maybe what you are trying to accomplish have to be done in other way, so if you want you can

explain more in detail what is your goal posting an example.

best regards

marcus_sommer

It's not quite clear what you are trying to do. Do you want check if this field-value is empty, NULL or contained a white-space then you need a check on the lenght, for example len(X_1) = 1 or len(trim(X_1)) = 0 or do you want to count the fieldvalues and check these amounts with something like >= AnyNumber. To count fieldvalues within a load meant you need to use an aggregation load with group by.

- Marcus

Peter_Cammaert
Partner - Champion III
Partner - Champion III

If you want to do this in your load script, then follow these steps. We are assuming that X_1 is a column in a resident table called (for example) Table_1. If the table has a different name, then change the code accordingly.

Table_Count_X_1:

LOAD IF (Count(X_1)=0, 'D', 'A') AS Flag_X_1

RESIDENT Table_1; // Modify if resident table has a different name

LET Var1 = Peek('Flag_X_1'); // Var1 now contains either D or A

DROP Table Table_Count_X_1;

If you want to count the number of unique values in X_1, then add a DISTINCT keyword before X_1.

Best,

Peter