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

How to add new columns in script? Caculate date interval.

Hello everyone,

I have a table describe the test, include 4 fields (ID, TestType, Result, TestDate). I want to add new field when etl which can show last test date and how many months from last test.

The source table has 4 columns.

How to add 2 new columns (red color) in script when ETL?

Thank you very much.

I will demo next week, Please give me some idea. Thanks again.

IDTestTypeResultTestDatelastTestDateTestInterval(in month)
1A12231/1/2013  
2A1321/1/2013  
3A121/1/2013  
4A1231/1/2013  
5A2341/1/2013  
2A22/1/20131/1/20131
3A1232/1/20131/1/20131
4A133/1/20131/1/20132
1A1237/1/20131/1/20136
5A2348/1/20131/1/20137
1A238/1/20137/1/20131
2A12319/1/20132/1/20137
1 Solution

Accepted Solutions
Not applicable
Author

Hi,

     try this script

     LOAD

       *,

       Month(TestDate) - Month(LastTestDate) AS TestInterval;

     LOAD

       *,

       If(Peek('ID') = ID AND Peek('TestType') = TestType, Peek('TestDate')) AS LastTestDate

          Resident tempInterval

     Order By ID, TestType, TestDate;

I hope this will help you.

Thanks,

Ashutosh

View solution in original post

14 Replies
kaushiknsolanki
Partner Ambassador/MVP
Partner Ambassador/MVP

Hi,

     Try this.

     Load *,interval(LastTestDate - TestDate,'D') as Days Interval

     From xyz;

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

Hi,

     try this,

1.  First  load this to get lastTestDate and TestInterval field:

    

     tempTable:

     LOAD

       *,

       Month(TestDate) - Month(lastTestDate) AS TestInterval;

     LOAD

       *,

       If(Peek('ID') = ID, Peek('TestDate')) AS lastTestDate

     Resident xyz

     Order By ID,TestDate;

2. After  this, load again to get result according to your Order By:

     LOAD

       *

     Resident tempTable

     Order By TestDate;

Regards,

Ashutosh

Not applicable
Author

The question is there is no lastTestDate field in the source table.

How to get the field lastTestDate?

Not applicable
Author

hi,

   Yes, I understand what  you are asking.

   Check again my script. In this script, first lastTestDate field will create and after that TestInterval filed will be create according to both date fields.

First try this script then let me know.

Thanks

Ashutosh

mdmukramali
Specialist III
Specialist III

Dear,

Kindly find the attachment.

I hope it will help you.

Thanks,

Mukram.

Not applicable
Author

I have tried your script. It works! Thank you and one more question

If TestType is A B C D.... I want get lasttestDate depent on testType. How can I edit your script.

mdmukramali
Specialist III
Specialist III

Dear ,

can you try below script ,

First:

LOAD *

      Inline

[

ID,TestType ,Result ,TestDate

1,A,1223,1/1/2013

2,A,132,1/1/2013

3,A,12,1/1/2013

4,B,123,1/1/2013

5,A,234,1/1/2013

2,A,2,2/1/2013

3,A,123,2/1/2013

4,B,13,3/1/2013

1,A,123,7/1/2013

5,A,234,8/1/2013

1,A,23,8/1/2013

2,A,1231,9/1/2013

3,C,1456,3/5/2013

3,C,1343,5/11/2013

3,C,34,7/11/2013

];

Second:

  LOAD *,

                                      //     If(Peek('ID')= ID, Peek('TestDate'),'-') AS LastTestDate,

If(Peek('ID')= ID and Peek(TestType)=TestType, Peek('TestDate'),'-') AS LastTestDate,

                                     //     (Month(TestDate)-Month(If(Peek('ID') = ID, Peek('TestDate'),'-'))) as TimeInterval

  (Month(TestDate)-Month(If(Peek('ID')= ID and Peek(TestType)=TestType, Peek('TestDate'),'-'))) as TimeInterval

     Resident First

     Order By ID,TestDate;

     DROP Table First;

Thanks,

Mukram.

Not applicable
Author

Hi Mukram,

Thanks for your reply.

I have tried your script, The field TimeInterval has no value in any rows...

Not applicable
Author

Hi,

     first  clear my question. Suppose for ID = 1,  2 rows for testType A and 2 rows for testType B seperately. So you want max TestDate for testType A and Max testDate for testType B...am I right ?

Thanx,

Ashutosh