Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

question about Previous()

Hi,

please, look at my script:

-------------------------------------

load

  id,

  text,

  balance,

  Previous(debt)-balance as debt

;

load * inline [

id, text, balance, debt

0,      ,       , 400

1, aaa, 250, 0

2, bbb, 200, 0

3, ccc, 100, 0

];

-------------------------------------------

I expected this:

0,      ,       ,  -

1, aaa, 250, 150

2, bbb, 200, -50

3, ccc, 100, -150

but, the result is

0,      ,       ,  -

1, aaa, 250, 150

2, bbb, 200, -200

3, ccc, 100, -100

what's wrong?

thanks in advance!

1 Solution

Accepted Solutions
kaushiknsolanki
Partner Ambassador/MVP
Partner Ambassador/MVP

Try this,

Data:

LOAD *,If(Rowno()=1, debt,Peek(Bal)-balance) as Bal;

load * inline [

id, text, balance, debt

0,,, 400

1, aaa, 250, 0

2, bbb, 200, 0

3, ccc, 100, 0

];

Final:

NoConcatenate

Load id,text,balance,debt,if(debt=Bal,0,Bal) as Value Resident Data;

drop Table Data;

Regards,

Kaushik Solanki

Please remember to hit the 'Like' button and for helpful answers and resolutions, click on the 'Accept As Solution' button. Cheers!

View solution in original post

3 Replies
kaushiknsolanki
Partner Ambassador/MVP
Partner Ambassador/MVP

Try this,

Data:

LOAD *,If(Rowno()=1, debt,Peek(Bal)-balance) as Bal;

load * inline [

id, text, balance, debt

0,,, 400

1, aaa, 250, 0

2, bbb, 200, 0

3, ccc, 100, 0

];

Final:

NoConcatenate

Load id,text,balance,debt,if(debt=Bal,0,Bal) as Value Resident Data;

drop Table Data;

Regards,

Kaushik Solanki

Please remember to hit the 'Like' button and for helpful answers and resolutions, click on the 'Accept As Solution' button. Cheers!
Not applicable
Author

Many thanks, Kaushik!

Works good!

aveeeeeee7en
Specialist III
Specialist III

Hi Oleg

Do you know why this has happened???

That Previous() function gives you a different Result from Peek() function.

This has happened because of the below Reson:

Previous() operates on the Input to the Load statement,

whereas Peek() operates on the Output of the Load statement.


So, when is it best to use each function?

  • The previous() and peek() functions could be used when a user needs to show the current value versus the previous value of a field that was loaded from the original file.

  • The peek() function would be better suited when the user is targeting either a field that has not been previously loaded into the table or if the user needs to target a specific row.

See the detailed description here:

Peek() vs Previous() – When to Use Each



Hope that has given you some conceptual Clarity.



Regards

Av7eN