Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
andriesb
Creator II
Creator II

code simplicity loading using variables and loop through

I would like to simplify the following code :

Load

  Field_1                                        As Total_1,

  Field_1 + Field_2                          As Total_2,

  Field_1 + Field_2 + field_3           AS Total_3,

I do have in total 25 different fields where i would like to save the sum to a new field.

Maybe i could loop through the field vales using a variable?

Any ideas on how to simplify this code?

1 Reply
Not applicable

You might try something like this:

SET field_names='Field1, Field2, Field3';

LET total_field_names='';

LET running_total='';

LET csv = chr(39) & Replace(field_names, ',', chr(39)&','&chr(39)) & chr(39);

LET counter=1;

for each value in $(csv)

    If (len(total_field_names) > 0) then

        LET total_field_names = total_field_names & ',';

        LET running_total = running_total & '+';

    ENDIF

    LET running_total = running_total & value;

    LET total_field_names = total_field_names & running_total & ' AS Total' & counter;

    LET counter = counter + 1;

next value

LOAD

$(total_field_names)

Resident

table_name;