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: 
johnnyjohn
Creator
Creator

Load field when variable equals a string

So I have a variable _Prefix.

I want to load a field only when the value for that variable is 'n1_co_'.

 

I tried the below, but get this error Field 'n1_co_' not found

IF($(_Prefix) = 'n1_co_', $(_Prefix)SenderSubId, '') as $(_Prefix)SenderSubId,

 

I tried the below, but the condition doesn't seem to apply (i.e. when the var is equal something else, it still tries to load $(_Prefix)SenderSubId)

IF('$(_Prefix)' = 'n1_co_', $(_Prefix)SenderSubId, '') as $(_Prefix)SenderSubId,

 

I tried the below, but it doesnt work, gives the error  Field '_Prefix' not found

IF(_Prefix = 'n1_co_', $(_Prefix)SenderSubId, '') as $(_Prefix)SenderSubId,

 

 

Any ideas what I'm doing wrong? 

Thanks

Labels (2)
3 Replies
marcus_sommer

All specified fields within a load must exists within the source and this means within a load you couldn't check if a certain field exists or not and further if such things are necessary you will need additionally measures like using several specialized load-statements or doing the wanted transformation some load-steps later or using any pre-load to check which fields are there or similar measures.

- Marcus

johnnyjohn
Creator
Creator
Author

Thanks for coming back Marcus. I may have oversimplified the issue. Below is the simplest version of the full loader

So I'm creating a function that builds tables with different names based on the arguments being passed. I want to make it so that when I call this function with a given _Prefix value, I only load the SenderSubId when i pass n1_co_ and not n2_ca_. hope that makes more sense

Sub CreateOrderTable(_TableName, _ResidentTable, _Prefix)
    [$(_TableName)]:
    
    LOAD
        %$(_Prefix)id,
        IF(_Prefix = 'n1_co_', $(_Prefix)SenderSubId, '') as $(_Prefix)SenderSubId
        
    RESIDENT [$(_ResidentTable)];
    Drop Table [$(_ResidentTable)];
    _TableName=;
    _ResidentTable=;
    _Prefix=;     
End Sub

Call CreateOrderTable('ClientOrder', 'client_orders', 'n1_co_');
Call CreateOrderTable('ClientAmend', 'client_amends', 'n2_ca_');

 

marcus_sommer

In general such sub-routine will work but all specified tables and fields must exists. For me it's not quite clear what do you want to reach with the prefix? Some kind of qualifying? If so the regular qualify-statement might be more practicable. I know that qualifying-approaches are very common within databases-logic but within a Qlik data-model it has IMO much more disadvantages then benefits - therefore you may re-think the usefulness of it. 

Beside of this you may outsource this prefix-approach from the load-statement, means something like:

sub x(p1, p2, p3)

if  '$(p3)' = 'x' then
   let var = ...;
end if

load ... $(var) resident ...;

end sub

- Marcus