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

Script to transform table?

Hi all,

I have a table with format below:

A, B, C columns indicates the [end date] of corresponding phase, for example, the end date of phase A is 4/21/2014

   

IDAB C
14/21/20144/30/20155/1/2015


I would like to convert table in this format when loading: (for each phase, End Date stays the same, Start Date default to previous phase End Date (if available))

   

IDPhaseStart DateEnd Date
1A 4/21/2014
2B4/21/20144/30/2015
3C4/30/2015 5/1/2015

 

Thanks for helping me.

Sherrie

1 Solution

Accepted Solutions
techvarun
Specialist II
Specialist II

Source:

Crosstable (Phase, [End Date], 1)

LOAD * INLINE [

    ID, A, B, C

    1, 4/21/2014, 4/30/2015, 5/1/2015

];

Load ID,Phase, previous([End Date]) as [Start Date],[End Date]

Resident Source;

DROP Table Source;

View solution in original post

6 Replies
shwethaa
Contributor III
Contributor III

Try below script-

Load ID,Phase, previous([End Date]) as [Start Date],[End Date]

From source;

techvarun
Specialist II
Specialist II

Source:

Crosstable (Phase, [End Date], 1)

LOAD * INLINE [

    ID, A, B, C

    1, 4/21/2014, 4/30/2015, 5/1/2015

];

Load ID,Phase, previous([End Date]) as [Start Date],[End Date]

Resident Source;

DROP Table Source;

songzixian2016
Contributor III
Contributor III
Author

This works for simple case, however, my actual scenario does not just contain 1 row. So when I convert this table

   

ID
AB C
14/21/20144/30/20155/1/2015 
21/1/20143/1/20154/1/2015 

The result using your script results in "ID2 PhaseA StartDate" filled with "ID1 Phase C End Date" while the expected result is "ID2 PhaseA StartDate" to be blank:

   

ID
PhaseStart DateEnd Date
1A 4/21/2014
1B4/21/20144/30/2015
1C4/30/20155/1/2015 
2A 1/1/2014
2B1/1/20143/1/2015
2C3/1/20154/1/2015 

How to modify the script to re resolve this error?

Frank_Hartmann
Master II
Master II

Source: 

Crosstable (Phase, [End Date], 1) 

LOAD * INLINE [ 

    ID, A, B, C 

    1, 4/21/2014, 4/30/2015, 5/1/2015

    2, 1/1/2014,  3/1/2015,  4/1/2015

    3, 5/1/2014,  7/1/2015,  9/1/2015

];  

 

Load

ID,

Phase,

if(Phase<>'A',previous([End Date]),Null()) as [Start Date],

[End Date]  

 

Resident Source;  

DROP Table Source; 

shwethaa
Contributor III
Contributor III

Replace previous([End Date]) with if(previous(ID)=ID,previous([End Date]))

captain89
Creator
Creator

if you have only 3 fields you can do it easily like this:

newtab:

load

rowno()*ID as ID, Phase, 0 as  [Start Date], A as [End Date]

resident yourtable;

concatenate

load

(rowno()+1)*ID as ID, Phase, A as  [Start Date], B as [End Date]

resident yourtable;

concatenate

load

(rowno()+2)*ID as ID, Phase, B as  [Start Date], C as [End Date]

resident yourtable;

drop table yourtable.