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

Script Help - if then doing something

Hi all,

Assume I have the following data:

   

IDStatusTDateAmt
A01Open15/02/201535
A02Open13/02/201540
A03Open28/02/201525
A04Open20/02/201599
A01Close14/03/2015
A02Open18/03/201579
A04Close18/03/2015


Now, I need to do if the status is close then I need take the Amt from last month, else take Amt as a Close_Amt fro the particular ID(see following result).

Is it possible to make it at script? How can I come out the result at scripting?

    

IDStatusTDateAmtClose_Amt
A01Open15/02/20153535
A02Open13/02/20154040
A03Open28/02/20152525
A04Open20/02/20159999
A01Close14/03/2015 35
A02Open18/03/20157979
A04Close18/03/2015 99
1 Solution

Accepted Solutions
sunny_talwar

Try this:

Table:

LOAD ID,

    Status,

    TDate,

    Amt

FROM

[https://community.qlik.com/thread/200253]

(html, codepage is 1252, embedded labels, table is @1);

FinalTable:

LOAD *,

  If(Status = 'Close' and ID = Peek('ID'), Peek('Amt')) as Close_Amt

Resident Table

Order By ID, TDate;

DROP Table Table;

View solution in original post

8 Replies
Not applicable
Author

Yes it is possible in the script. Make sure that it is sorted accordingly.
Just made this mockup for you,

Include_PC:

Load * Inline [

Date, IncludePC, Navn, Amt

40000, 6042, MAre, 100

40500, 6020, Hordaland,200

41000, 6042, MAre, ];

PC_MAP:

load

Navn,

IncludePC,

Amt as Amt_T,

Date,

if(isnull(Amt) or trim(Amt)='',peek('Amt'),Amt) as Amt

resident Include_PC order by IncludePC,Navn,Date ASC;

drop table Include_PC;

You can either just rename the fields back in a resident, rename field or something like that as this produces 'Amt' and Amt_T.

Or expand the If " for Close amt into something like

PC_MAP:

load

Navn,

IncludePC,

Amt,

Date,

if(isnull(Amt) or trim(Amt)='',if(isnull(peek('Amt')) or trim(peek('Amt'))='', peek('Close_Amt'), peek('Amt')) ,Amt) as Close_Amt

//if(isnull(Amt) or trim(Amt)='',peek('Amt'),Amt) as Amt

resident Include_PC order by IncludePC,Navn,Date ASC;

drop table Include_PC;

sunny_talwar

Try this:

SET DateFormat='DD/MM/YYYY';

Table:

LOAD ID,

    Status,

    TDate,

    Amt

FROM

[https://community.qlik.com/thread/200253]

(html, codepage is 1252, embedded labels, table is @1);

FinalTable:

LOAD *,

  If(Status = 'Open', Amt,

  If(ID = Peek('ID'), Peek('Close_Amt'))) as Close_Amt

Resident Table

Order By ID, TDate;

DROP Table Table;


Capture.PNG

Not applicable
Author

Ops, my last suggestion forgot a peek on ID/IncludePC in the if statement.

Include_PC:

Load * Inline [

Date, IncludePC, Navn, Amt

40000, 6042, MAre, 100

40500, 6020, Hordaland,200

41000, 6042, MAre, ];

Test:

load

Navn,

IncludePC,

Amt,

Date,

if(isnull(Amt) or trim(Amt)='' and IncludePC=peek('IncludePC') ,if(isnull(peek('Amt')) or trim(peek('Amt'))='', peek('Close_Amt'), peek('Amt')) ,Amt) as Close_Amt

resident Include_PC order by IncludePC,Navn,Date ASC;

Not applicable
Author

Hey,

Try This.

set NullInterpret='';

TAB2:

//load ID,Status,TDate,Amt,if(Status='Open',Amt,)

LOAD * INLINE [

    ID, Status, TDate, Amt

    A01, Open, 15/02/2015, 35

    A02, Open, 13/02/2015, 40

    A03, Open, 28/02/2015, 25

    A04, Open, 20/02/2015, 99

    A01, Close, 14/03/2015,

    A02, Open, 18/03/2015, 79

    A04, Close, 18/03/2015,

];

TAB3:

load *,1

resident TAB2

order by ID,TDate desc;

load *,if(isnull(Amt),peek('Amt',-1),Amt) as NAmt

Resident TAB3;

drop table TAB2,TAB3;

drop field 1;

Anonymous
Not applicable
Author

Hi Sunny,

I' sorry.My user gt slightly change the requirement. He wan only show the last month amt when then status equal to close. Like following. Gt any idea to do this?

   

IDStatusTDateAmtClose_Amt
A01Open15/02/201535
A02Open13/02/201540
A03Open28/02/201525
A04Open20/02/201599
A01Close14/03/201535
A02Open18/03/201579
A04Close18/03/201599
Not applicable
Author

Hi Wong, couple of questions before answer your question ?

In your source data, do you have more than one row for previous month & current Month on ID and status combination? If Yes, you need write complex script to get out of this.

sunny_talwar

Try this:

Table:

LOAD ID,

    Status,

    TDate,

    Amt

FROM

[https://community.qlik.com/thread/200253]

(html, codepage is 1252, embedded labels, table is @1);

FinalTable:

LOAD *,

  If(Status = 'Close' and ID = Peek('ID'), Peek('Amt')) as Close_Amt

Resident Table

Order By ID, TDate;

DROP Table Table;

Anonymous
Not applicable
Author

Hi Dathu,

There has only one record for previous month and current month on ID and status. I fond the answer to to this.

Thanks for help.