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

Announcements
Join us in NYC Sept 4th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
zagzebski
Creator
Creator

If statement with functions

Is it possible to use an if statement with functions in a script. Bascially I want a certain code in place based on an if statement. Is this possible?  For example.

If( $(vMyVariable) = 1, Concatentate, NoConcatenate)

or

If($(vMyVariable) = 1, Drop Table Prod, '')

thanks in advance,

Steve

1 Solution

Accepted Solutions
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

I would think the Let should be in the loop and you have the concatenate logic reversed. Try like this



For e = 1 to 2

Let  vMyConcatenate = if($(e)=1,'NoConcatenate','concatenate (ProdTest)');
ProdTest:
$(vMyConcatenate)
Load *
Resident Prod
$(e);
Drop table Prod$(e);
next e



Here's a way you can skip the entire issue of having to set the Concatenate/NoConcatenate


For e = 1 to 2

ProdTest:
Load *, 1 dummyField
Resident Prod
$(e);
Drop table Prod$(e);
next e

DROP FIELD dummyField;

View solution in original post

9 Replies
MK_QSL
MVP
MVP

something like

If $(vMyVariable) = 1 THEN

     T1:

     Load * From Table1;

     Concatenate

     Load * From Table2;

ElseIf

     T1:

     Load * From Table1

     NoConcatenate

     T2:

     Load * From Table2;

Else

EndIF


Or

If $(vMyVariable) = 1 Then

     Drop Table TableName;

Else

EndIF



    

nagaiank
Specialist III
Specialist III

You may define a variable using if() and use that variable in the script. In some cases, you may use If .. Then .. EndIf

Examples 1

Let vMyConcatenate = if($(MyVariable)=1,'Concatenate','Noconcatenate');

$(vMyCocatenate) LOAD ...

Example 2

If $(vMyVariable)=1 Then

Drop Table Prod;

EndIf

its_anandrjs
Champion III
Champion III

Hi,

Yes you can do this in load script by use of if statements and the

1. For first see this example.

Ex:-

T1:

LOAD * Inline
[
ColA,ColB
A,1
B,2
C,3
]
;

LET vMyVariable = 1;

If $(vMyVariable) = 1 then

Concatenate
T2:
LOAD * Inline
[
ColA,ColB
1,ab
2,bc
3,cd
4,de
]
;

ELSEIF $(vMyVariable) <> 1 then

NoConcatenate

T2:
LOAD * Inline
[
ColA,ColB
1,ab
2,bc
3,cd
4,de
]
;

ENDIF

2. And for Second try some thing this ways

If $(vMyVariable) = 1 then

DROP Table T1;


Regards

Anand

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

Yes, you can do what you are after but in a slightly different form. You need to assign the script test to a variable and then use the variable on a script line by itself. For example:

LET vConcat = if(NoOfRows('mytab')>0, 'CONCATNATE (mytab)', 'mytab:');

$(vConcat)

LOAD X,Y...

For your DROP TABLE example:

LET vDrop = If($(vMyVariable) = 1, 'Drop Table Prod;', '');

$(vDrop)

Note you can also do the DROP TABLE like

IF '$(vMyVariable)' = 1 THEN

  DROP TABLE Prod;

ENDIF

or

WHEN '$(vMyVariable)' = 1 DROP TABLE Prod;

-Rob

http://masterssummit.com

http://robwunderlich.com

zagzebski
Creator
Creator
Author

Rob so I did something like this:

Let vMyConcat = If($(vTest), Concatenate, NoConcatenate)

but getting a "Script Line Error" memory

its_anandrjs
Champion III
Champion III

Hi,

Then try this way

T1:
LOAD * Inline [
Field1,Field2
A,1
B,2
C,3  ]
;

LET vTest = 1;
Let vMyConcat = If($(vTest)=1, 'Concatenate T1', 'NoConcatenate T1');

$(vMyConcat):
LOAD * Inline [
Field1,Field2
B,23
C,45
D,24
E,66  ]
;

Regards

Anand

zagzebski
Creator
Creator
Author

So I got past the variable but I am getting the error "Table not Found" when I run this:

Let  vMyConcatenate = if($(e)=1,'Concatenate','Noconcatenate');

For e = 1 to 2
ProdTest:
$(vMyConcatenate)
Load *
Resident Prod
$(e);
Drop table Prod$(e);
next e

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

I would think the Let should be in the loop and you have the concatenate logic reversed. Try like this



For e = 1 to 2

Let  vMyConcatenate = if($(e)=1,'NoConcatenate','concatenate (ProdTest)');
ProdTest:
$(vMyConcatenate)
Load *
Resident Prod
$(e);
Drop table Prod$(e);
next e



Here's a way you can skip the entire issue of having to set the Concatenate/NoConcatenate


For e = 1 to 2

ProdTest:
Load *, 1 dummyField
Resident Prod
$(e);
Drop table Prod$(e);
next e

DROP FIELD dummyField;

zagzebski
Creator
Creator
Author

Thanks Rob!