Luis Carmona Martinez
Luis Carmona Martinez

Reputation: 119

Why is this Loop not working in qlik sense script?

I am having a problem trying to do a FOR LOOP as it produces no values.

I am doing 3 steps, but not sure what is the problem, I have attached the APP.

/// 1. These are the FOR values to PASS for the variables below.

for i= -1 to -7 ;
 for j=-8 to -15;
 for z= -16 to -21

//// 2.These are variable FUNCTIONS with same structure
LET
     V_result1=(sum(Peek(Result_1,$i))*0.45+sum(Peek(Result_1,$j))*0.35+sum(Peek(Result_1,$z))*0.2)*1/5;
     V_result2=(sum(Peek(Result_2,$i))*0.45+sum(Peek(Result_2,$j))*0.35+sum(Peek(Result_2,$z))*0.2)*1/5;


//// 3. The table where to apply those VARIABLES from a RESIDENT table.
DATE_PRODUCTION_4;
LOAD 
    "Date",
    Sum($(V_result1)) as Forecast1,
    sum($(V_result2)) as Forecast2

Resident [DATE_PRODUCTION_3]
GROUP BY "Date";

APP TEST

Upvotes: 0

Views: 1516

Answers (1)

CvP
CvP

Reputation: 364

Couple of things going wrong here:

  1. If we look here, we see this:
Argument      Description
field_name    Name of the field for which the return value is required. Input value must be given as a string (for example, quoted literals).

"Input value must be given as a string (for example, quoted literals)."

So instead of:

Peek(Result_1,$i)

You should use:

Peek('Result_1', $i)
  1. If we look here, we see this:

When using a variable for text replacement in the script or in an expression, the following syntax is used:

$(variablename)

So building on step one, instead of this

Peek('Result_1', $i)

You should use:

Peek('Result_1', $(i))
  1. Your for loop starts at -1 and goes to -7, but in the app you added, -7 will always return NULL, since your data only consists of 4 rows. So change your for loop to a smaller range and first start of with one loop, then nest another for loop and then nest another. That way you can solve it step-by-step.

Upvotes: 1

Related Questions