Qlik Community

New to Qlik Sense

Discussion board where members can get started with Qlik Sense.

Highlighted
MCA
New Contributor

Counter or variable manipulation within load script?

Is it possible to update / change the value of a variable within the load script? 

Test:
LOAD * Inline [
ID, amount
1, 50
1, 100
99, 70
5, 25
5, 20
5, 10
5, 21
5, 69
]; 

With this data I would like to be able to sort it by ID and then amount.  I'd like the load script to analyze the ID; if it repeats it should add 1, then 2, and so on.  I've tried adding the data to a temporary table and using Peek() and Previous() along with a variable and counter but have had no luck.  With the data provided the goal would be to the following:

ID, amount, New Value

1, 50, 11

1, 100, 12

5, 10, 51

5, 20, 52

5, 21, 53

5, 25, 54

5, 69, 55

99, 70, 991

 

 

 

 

 

 

1 Solution

Accepted Solutions
lironbaram
Honored Contributor II

Re: Counter or variable manipulation within load script?

hi

this will do the trick 


Test:
LOAD * Inline [
ID, amount
1, 50
1, 100
99, 70
5, 25
5, 20
5, 10
5, 21
5, 69
];

table:
load *,num(ID&counter) as newValue;
load * , if(Previous(ID)=ID,RangeSum(peek('counter'),1),1) as counter
Resident Test
order by ID,amount;


drop Table Test;

2 Replies
lironbaram
Honored Contributor II

Re: Counter or variable manipulation within load script?

hi

this will do the trick 


Test:
LOAD * Inline [
ID, amount
1, 50
1, 100
99, 70
5, 25
5, 20
5, 10
5, 21
5, 69
];

table:
load *,num(ID&counter) as newValue;
load * , if(Previous(ID)=ID,RangeSum(peek('counter'),1),1) as counter
Resident Test
order by ID,amount;


drop Table Test;

MCA
New Contributor

Re: Counter or variable manipulation within load script?

That took care of it. Thank you for the quick solution!