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

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
mgk123
Partner - Contributor
Partner - Contributor

Is there a way to load a script based on an if statement?

SAP1 ,SAP2, BASE1 ,BASE2 are all dates from another load table.
I thought something like the below would work but it doesn't. Is there a way to create this type of logic?
Thank you in advance.
 
 
IF (SAP1 >=  SAP2
AND BASE1 >= BASE2)
    
THEN
LOAD * FROM [lib://30_QVD_Extract/SAP1_BASE1.QVD] (qvd);
    
ELSEIF (SAP1 <  SAP2
AND BASE1 < BASE2)
        
THEN
LOAD * FROM [lib://30_QVD_Extract/SAP2_BASE2.QVD] (qvd);
    
        
ELSEIF (SAP1 >=  SAP2
AND BASE1 < BASE2)
 
THEN
LOAD * FROM [lib://30_QVD_Extract/SAP1_BASE2.QVD] (qvd);
    
    
ELSEIF (SAP1 <  SAP2
AND BASE1 >= BASE2)
 
THEN
 
LOAD * FROM [lib://30_QVD_Extract/SAP2_BASE1.QVD] (qvd);
        
 
END IF;
 
Labels (4)
4 Replies
Mark_Little
Luminary
Luminary

Yes look at subroutines.

https://help.qlik.com/en-US/sense/August2023/Subsystems/Hub/Content/Sense_Hub/Scripting/ScriptContro...

Basically wrap each part of your script in a sub function, then build your if around the calls

mgk123
Partner - Contributor
Partner - Contributor
Author

What would that look like with the query above?

 

cotiso_hanganu
Partner - Creator III
Partner - Creator III

Hi there,

First things first:
In order to use the content of a cell within a column within a table (matrix of values) in an if statement, you have to take it from the table into a variable (single value recipient).

let vSAP1 = peek('SAP1',-1,'TableNameHavingSAP1');
... and so on ...

Notes:

1.In order to avoid confusion, it is a good practice to use a small v at the beginning of the names of the variables.)

2. I made an assumption that TableNameHavingSAP1 has only one row. I used -1 to get the LAST row . You can use 0 to get the FIRST row (if you have only one row in the table, they are equivalent, but do not use 1, because it will look for the 2nd row, which might not exist , if you have only one row in the table)

3. peek function requires single quotes for the names of Field and Table

4. peek can be used in other cool ways  (even within inter record ops in load instructions) 

 

And after that,  you can can really use the if statement...

if vSAP1 >= vSAP2 and vBASE1 >= vBASE2 then

          load whatever;

else if vSAP1 <= vSAP2 and vBASE1 >= vBASE2 then

          load other;

else 

        load 3rd option;

end if

Notes:

1. mandatory to have on a single ROW the whole if .... then 

2. else if can be used multiple times, of course. Or not at all.
else only once at the end, or never.

3.  you could also make the if statement directly:
if peek('SAP1',-1,'TableNameHavingSAP1')>=peek('SAP2',-1,'TableNameHavingSAP2') then
...

but it's harder to debug, in case you do not "peek" the right thing:

 

Good luck !

cotiso_hanganu
Partner - Creator III
Partner - Creator III

Can't see a direct connection between your answer and the issue raised, but I would like to get more details, to understand.