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

Arranging table data to formulate start date and end date from different records of same table

Hi,

I have following subset of records from audit history table. I'm trying to rearrange data so I've start datetime and end datetime for records where there is a pair from NewValue and OldValue but OldValue has to match NewValue from earlier timestamp or TL Sequence # of OldValue record is higher than that of NewValue. Also if there are more than one pair of same value than it should match appropriately so that there is no duplication as well pairing is right.

I was planning to read/load CP Doc ID, EditDate as StartDate, Null() as EndDate, TL Sequence, FieldEvent, NewValue in one table and then do concatenate it with somehow matching appropriate record using FieldEvent, OldValue, EditDate, TL Sequence to assign EditDate as EndDate

I'm looking for help/guidance in this scripting functionality

 

CP Doc IDEditDateTL SequenceFieldEventNewValueOldValue
4880578/5/2016 12:36:00 PM657806Created--
4880578/5/2016 1:34:00 PM657807DocStatusUR by LDCPR of LCSD
4880578/9/2016 10:22:00 AM657808DocStatusHRCUR by LDC
4880578/15/2016 11:44:00 AM657809DocStatusPR of LCSDHRC
4880579/8/2016 11:00:00 AM657810DocStatusRS to LHRPR of LCSD
4880579/8/2016 11:01:00 AM657811DocStatusRS to LHR - HRUNRS to LHR
4880579/8/2016 11:17:00 AM657812DocStatusUR by LDCRS to LHR - HRUN
4880579/8/2016 1:28:00 PM657813DocStatusHRC

UR by LDC

1 Solution

Accepted Solutions
vvira1316
Specialist II
Specialist II
Author

Thank you all for looking at this question and providing ideas.

I've included new data set that is more comprehensive of the scenario.

I found my solution by doing some programming logic in scripting. Currently I built logic for HistoryFieldEvent = 'DocStatus'

[CP_DocHistory_Data]:
LOAD [CP Doc ID] as CPDocID,
[Edit Date] as HistoryEditDate,
If(Trim([Field / Event]) = 'Created.', 'DocStatus', Trim([Field / Event])) as HistoryFieldEvent,
If(Trim([Field / Event]) = 'Created.', AutoNumber([CP Doc ID]&[Edit Date]&'NR'),
If(Trim([Field / Event]) = 'DocStatus', AutoNumber([CP Doc ID]&[Edit Date]&[New Value]), AutoNumber([CP Doc ID]&[Edit Date]& Trim([Field / Event])))) as [TL Sequence],
If(Trim([Field / Event]) = 'Created.', 'NR',
If(Trim([Field / Event]) = 'DocStatus', [New Value], Trim([Field / Event]))) as [TL Phase],
[Old Value] as HistoryOldValue,
If(Trim([Field / Event]) = 'Created.', 'NR', [New Value]) as HistoryNewValue
From [..\Data\CP_DocHistory_Data.qvd](qvd)
Where [CP Doc ID] = '217242' or
[CP Doc ID] = '314023' or
[CP Doc ID] = '360394' or
[CP Doc ID] = '488057' ;

[DocStatusCount]:
LOAD CPDocID,
Count([CPDocID]) as DocStatusCnt
Resident [CP_DocHistory_Data]
Where HistoryFieldEvent = 'DocStatus'
Group By CPDocID
Order By CPDocID;

[NextStepCount]:
LOAD CPDocID,
Count([CPDocID]) as NextStepCnt
Resident [CP_DocHistory_Data]
Where HistoryFieldEvent = 'Next Step'
Group By CPDocID
Order By CPDocID;

DistDocStatusDocIDCount:
LOAD Count(DISTINCT [CPDocID]) as DistDocStatusDocIDCount
Resident [DocStatusCount];

LET vDistDocStatusDocIDCount = PEEK('DistDocStatusDocIDCount',-1,'DistDocStatusDocIDCount') - 1;

DistNextStepDocIDCount:
LOAD Count(DISTINCT [CPDocID]) as DistNextStepDocIDCount
Resident [NextStepCount];

LET vDistNextStepDocIDCount = PEEK('DistNextStepDocIDCount',-1,'DistNextStepDocIDCount');

SET vCPDocID=0;
SET vTLSeq=0;

FOR DocStatusDocIDCounter = 0 to $(vDistDocStatusDocIDCount)

LET vCPDocID = Peek('CPDocID',$(DocStatusDocIDCounter),'DocStatusCount');
LET vDocStatusCnt = Peek('DocStatusCnt',$(DocStatusDocIDCounter),'DocStatusCount') - 1;

IF $(vDocStatusCnt) > 1 then

FOR DocStatusCountCounter = 0 to $(vDocStatusCnt)

CPDocHistTemp:
LOAD CPDocID,
Min([TL Sequence]) as TLSequence
Resident [CP_DocHistory_Data]
Where CPDocID = $(vCPDocID)
and HistoryFieldEvent = 'DocStatus'
and [TL Sequence] > $(vTLSeq)
Group By CPDocID
Order By CPDocID, [TL Sequence];

LET vTLSeq = Peek('TLSequence',0,'CPDocHistTemp');
LET vCPDID = Peek('CPDocID',0,'CPDocHistTemp');

DROP Table CPDocHistTemp;

CPDocHistTemp:
LOAD CPDocID,
[TL Sequence] as TLSequence,
HistoryEditDate,
HistoryFieldEvent,
[TL Phase] as TLPhase,
HistoryOldValue,
HistoryNewValue
Resident [CP_DocHistory_Data]
Where CPDocID = $(vCPDocID)
and [TL Sequence] = $(vTLSeq)
Order By CPDocID, [TL Sequence];

LET vStartDateTime = Peek('HistoryEditDate',0,'CPDocHistTemp');
LET vFieldEvent = Peek('HistoryFieldEvent',0,'CPDocHistTemp');
LET vPhase = Peek('TLPhase',0,'CPDocHistTemp');
LET vNewValue = Peek('HistoryNewValue',0,'CPDocHistTemp');
LET vOldValue = Peek('HistoryOldValue',0,'CPDocHistTemp');

DROP Table CPDocHistTemp;

CPDocHistTemp:
LOAD CPDocID,
Min([TL Sequence]) as TLSequence
Resident [CP_DocHistory_Data]
Where CPDocID = $(vCPDocID)
and HistoryFieldEvent = 'DocStatus'
and HistoryOldValue = '$(vNewValue)'
and [TL Sequence] > $(vTLSeq)
Group By CPDocID
Order By CPDocID, [TL Sequence];

LET vSecTLSeq = Peek('TLSequence',0,'CPDocHistTemp');

DROP Table CPDocHistTemp;

IF IsNum($(vSecTLSeq)) then

CPDocHistTemp:
LOAD CPDocID,
[TL Sequence] as TLSequence,
HistoryEditDate
Resident [CP_DocHistory_Data]
Where CPDocID = $(vCPDocID)
and [TL Sequence] = $(vSecTLSeq)
Order By CPDocID, [TL Sequence];

LET vEndDateTime = Peek('HistoryEditDate',0,'CPDocHistTemp');

[TLTable]:
LOAD *
Inline [CPDID, TLS, HFE, VALUE, STRATDTTM, ENDDTTM
$(vCPDocID), $(vTLSeq), $(vFieldEvent), $(vNewValue), $(vStartDateTime), $(vEndDateTime)]
;

DROP Table CPDocHistTemp;

ELSE

CPDocHistTemp:
LOAD CPDocID,
[TL Sequence] as TLSequence,
HistoryEditDate,
HistoryFieldEvent,
[TL Phase] as TLPhase,
HistoryOldValue,
HistoryNewValue
Resident [CP_DocHistory_Data]
Where CPDocID = $(vCPDocID)
and [TL Sequence] = $(vTLSeq)
Order By CPDocID, [TL Sequence];

LET vStartDateTime = Peek('HistoryEditDate',0,'CPDocHistTemp');
LET vFieldEvent = Peek('HistoryFieldEvent',0,'CPDocHistTemp');
LET vPhase = Peek('TLPhase',0,'CPDocHistTemp');
LET vNewValue = Peek('HistoryNewValue',0,'CPDocHistTemp');
LET vOldValue = Peek('HistoryOldValue',0,'CPDocHistTemp');

LET vEndDateTime = Null();

[TLTable]:
LOAD *
Inline [CPDID, TLS, HFE, VALUE, STRATDTTM, ENDDTTM
$(vCPDocID), $(vTLSeq), $(vFieldEvent), $(vNewValue), $(vStartDateTime), $(vEndDateTime)]
;

DROP Table CPDocHistTemp;

ENDIF;

NEXT DocStatusCountCounter;

ENDIF;

NEXT DocStatusDocIDCounter;

DROP Table DistDocStatusDocIDCount;
DROP Table DistNextStepDocIDCount;

RestructuredData.png

View solution in original post

6 Replies
sinanozdemir
Specialist III
Specialist III

Hi,

Would you happen to have sample data to share here?

Thanks

vvira1316
Specialist II
Specialist II
Author

Hi,

It is the same data that I had included in the question but I'm including the excel file of it.

Vijay

sinanozdemir
Specialist III
Specialist III

Ok.

Something like this:

Capture.PNG

For this to work, you need to sort your data by EditDate and TL Sequence, and use Below() function.

Please see the attached.

Thanks

vvira1316
Specialist II
Specialist II
Author

Hi,,

Thanks for your input but I'm looking to create a table in script that will look something like table provided below.

Best Regards,

   

CP Doc IDTL SequencePhaseStartDateEndDate
488057657806Created8/5/2016 12:36:00 PM
488057657807UR by LDC8/5/2016 1:34:00 PM8/9/2016 10:22:00 AM
488057657808HRC8/9/2016 10:22:00 AM8/15/2016 11:44:00 AM
488057657809PR of LCSD8/15/2016 11:44:00 AM9/8/2016 11:00:00 AM
488057657810RS to LHR9/8/2016 11:00:00 AM9/8/2016 11:01:00 AM
488057657811RS to LHR - HRUN9/8/2016 11:01:00 AM9/8/2016 11:17:00 AM
488057657812UR by LDC9/8/2016 11:17:00 AM9/8/2016 1:28:00 PM
488057657813HRC9/8/2016 1:28:00 PM
Not applicable

Hi,

Can you try this once...

arrange:

NoConcatenate

LOAD Min([CP Doc ID]) as [CP Doc ID],

     Min(EditDate) as EditDate,

     [TL Sequence] as [TL Sequence],

     FieldEvent,

     min(NewValue) as NewValue1,

     min(OldValue) as OldValue1

FROM TableData.xlsx (ooxml, embedded labels, table is Sheet1)

group by FieldEvent, [TL Sequence];

left join

LOAD [CP Doc ID] as [CP Doc ID],

     EditDate as EditDate1,

     [TL Sequence]-1 as [TL Sequence],

     FieldEvent as FieldEvent,

     (NewValue) as NewValue,

     OldValue as Phase

FROM TableData.xlsx (ooxml, embedded labels, table is Sheet1);

The resultant table will be below:

CP Doc ID TL Sequence FieldEvent Start Date End Date NewValue Phase
488057657806Created8/5/2016 12:36:00 PM
488057657807DocStatus8/5/2016 1:34:00 PM8/9/2016 10:22:00 AMHRCUR by LDC
488057657808DocStatus8/9/2016 10:22:00 AM8/15/2016 11:44:00 AMPR of LCSDHRC
488057657809DocStatus8/15/2016 11:44:00 AM9/8/2016 11:00:00 AMRS to LHRPR of LCSD
488057657810DocStatus9/8/2016 11:00:00 AM9/8/2016 11:01:00 AMRS to LHR - HRUNRS to LHR
488057657811DocStatus9/8/2016 11:01:00 AM9/8/2016 11:17:00 AMUR by LDCRS to LHR - HRUN
488057657812DocStatus9/8/2016 11:17:00 AM9/8/2016 1:28:00 PMHRCUR by LDC
488057657813DocStatus9/8/2016 1:28:00 PM

Thanks

Sujana

vvira1316
Specialist II
Specialist II
Author

Thank you all for looking at this question and providing ideas.

I've included new data set that is more comprehensive of the scenario.

I found my solution by doing some programming logic in scripting. Currently I built logic for HistoryFieldEvent = 'DocStatus'

[CP_DocHistory_Data]:
LOAD [CP Doc ID] as CPDocID,
[Edit Date] as HistoryEditDate,
If(Trim([Field / Event]) = 'Created.', 'DocStatus', Trim([Field / Event])) as HistoryFieldEvent,
If(Trim([Field / Event]) = 'Created.', AutoNumber([CP Doc ID]&[Edit Date]&'NR'),
If(Trim([Field / Event]) = 'DocStatus', AutoNumber([CP Doc ID]&[Edit Date]&[New Value]), AutoNumber([CP Doc ID]&[Edit Date]& Trim([Field / Event])))) as [TL Sequence],
If(Trim([Field / Event]) = 'Created.', 'NR',
If(Trim([Field / Event]) = 'DocStatus', [New Value], Trim([Field / Event]))) as [TL Phase],
[Old Value] as HistoryOldValue,
If(Trim([Field / Event]) = 'Created.', 'NR', [New Value]) as HistoryNewValue
From [..\Data\CP_DocHistory_Data.qvd](qvd)
Where [CP Doc ID] = '217242' or
[CP Doc ID] = '314023' or
[CP Doc ID] = '360394' or
[CP Doc ID] = '488057' ;

[DocStatusCount]:
LOAD CPDocID,
Count([CPDocID]) as DocStatusCnt
Resident [CP_DocHistory_Data]
Where HistoryFieldEvent = 'DocStatus'
Group By CPDocID
Order By CPDocID;

[NextStepCount]:
LOAD CPDocID,
Count([CPDocID]) as NextStepCnt
Resident [CP_DocHistory_Data]
Where HistoryFieldEvent = 'Next Step'
Group By CPDocID
Order By CPDocID;

DistDocStatusDocIDCount:
LOAD Count(DISTINCT [CPDocID]) as DistDocStatusDocIDCount
Resident [DocStatusCount];

LET vDistDocStatusDocIDCount = PEEK('DistDocStatusDocIDCount',-1,'DistDocStatusDocIDCount') - 1;

DistNextStepDocIDCount:
LOAD Count(DISTINCT [CPDocID]) as DistNextStepDocIDCount
Resident [NextStepCount];

LET vDistNextStepDocIDCount = PEEK('DistNextStepDocIDCount',-1,'DistNextStepDocIDCount');

SET vCPDocID=0;
SET vTLSeq=0;

FOR DocStatusDocIDCounter = 0 to $(vDistDocStatusDocIDCount)

LET vCPDocID = Peek('CPDocID',$(DocStatusDocIDCounter),'DocStatusCount');
LET vDocStatusCnt = Peek('DocStatusCnt',$(DocStatusDocIDCounter),'DocStatusCount') - 1;

IF $(vDocStatusCnt) > 1 then

FOR DocStatusCountCounter = 0 to $(vDocStatusCnt)

CPDocHistTemp:
LOAD CPDocID,
Min([TL Sequence]) as TLSequence
Resident [CP_DocHistory_Data]
Where CPDocID = $(vCPDocID)
and HistoryFieldEvent = 'DocStatus'
and [TL Sequence] > $(vTLSeq)
Group By CPDocID
Order By CPDocID, [TL Sequence];

LET vTLSeq = Peek('TLSequence',0,'CPDocHistTemp');
LET vCPDID = Peek('CPDocID',0,'CPDocHistTemp');

DROP Table CPDocHistTemp;

CPDocHistTemp:
LOAD CPDocID,
[TL Sequence] as TLSequence,
HistoryEditDate,
HistoryFieldEvent,
[TL Phase] as TLPhase,
HistoryOldValue,
HistoryNewValue
Resident [CP_DocHistory_Data]
Where CPDocID = $(vCPDocID)
and [TL Sequence] = $(vTLSeq)
Order By CPDocID, [TL Sequence];

LET vStartDateTime = Peek('HistoryEditDate',0,'CPDocHistTemp');
LET vFieldEvent = Peek('HistoryFieldEvent',0,'CPDocHistTemp');
LET vPhase = Peek('TLPhase',0,'CPDocHistTemp');
LET vNewValue = Peek('HistoryNewValue',0,'CPDocHistTemp');
LET vOldValue = Peek('HistoryOldValue',0,'CPDocHistTemp');

DROP Table CPDocHistTemp;

CPDocHistTemp:
LOAD CPDocID,
Min([TL Sequence]) as TLSequence
Resident [CP_DocHistory_Data]
Where CPDocID = $(vCPDocID)
and HistoryFieldEvent = 'DocStatus'
and HistoryOldValue = '$(vNewValue)'
and [TL Sequence] > $(vTLSeq)
Group By CPDocID
Order By CPDocID, [TL Sequence];

LET vSecTLSeq = Peek('TLSequence',0,'CPDocHistTemp');

DROP Table CPDocHistTemp;

IF IsNum($(vSecTLSeq)) then

CPDocHistTemp:
LOAD CPDocID,
[TL Sequence] as TLSequence,
HistoryEditDate
Resident [CP_DocHistory_Data]
Where CPDocID = $(vCPDocID)
and [TL Sequence] = $(vSecTLSeq)
Order By CPDocID, [TL Sequence];

LET vEndDateTime = Peek('HistoryEditDate',0,'CPDocHistTemp');

[TLTable]:
LOAD *
Inline [CPDID, TLS, HFE, VALUE, STRATDTTM, ENDDTTM
$(vCPDocID), $(vTLSeq), $(vFieldEvent), $(vNewValue), $(vStartDateTime), $(vEndDateTime)]
;

DROP Table CPDocHistTemp;

ELSE

CPDocHistTemp:
LOAD CPDocID,
[TL Sequence] as TLSequence,
HistoryEditDate,
HistoryFieldEvent,
[TL Phase] as TLPhase,
HistoryOldValue,
HistoryNewValue
Resident [CP_DocHistory_Data]
Where CPDocID = $(vCPDocID)
and [TL Sequence] = $(vTLSeq)
Order By CPDocID, [TL Sequence];

LET vStartDateTime = Peek('HistoryEditDate',0,'CPDocHistTemp');
LET vFieldEvent = Peek('HistoryFieldEvent',0,'CPDocHistTemp');
LET vPhase = Peek('TLPhase',0,'CPDocHistTemp');
LET vNewValue = Peek('HistoryNewValue',0,'CPDocHistTemp');
LET vOldValue = Peek('HistoryOldValue',0,'CPDocHistTemp');

LET vEndDateTime = Null();

[TLTable]:
LOAD *
Inline [CPDID, TLS, HFE, VALUE, STRATDTTM, ENDDTTM
$(vCPDocID), $(vTLSeq), $(vFieldEvent), $(vNewValue), $(vStartDateTime), $(vEndDateTime)]
;

DROP Table CPDocHistTemp;

ENDIF;

NEXT DocStatusCountCounter;

ENDIF;

NEXT DocStatusDocIDCounter;

DROP Table DistDocStatusDocIDCount;
DROP Table DistNextStepDocIDCount;

RestructuredData.png