Skip to main content
Announcements
Global Transformation Awards! Applications are now open. Submit Entry
cancel
Showing results for 
Search instead for 
Did you mean: 
markgraham123
Specialist
Specialist

Compare Previous Values and create new field

Hi all,

I'm working on a requirement where i have to look at previous rows and create a new column. Please see below example and attached qvw.

If(Emp_ID=Previous(Emp_ID) and Bin <> Previous(Bin) and StartTime-Previous(StartTime) <= 2000, Previous(StartTime), StartTime) as NewColumn;

Here is example:

StartTime,                    Bin,                     Emp_ID

27011,          SINGLES-BULK-082,          A
27051,         SINGLES-BULK-055,           A

Output would be, 

StartTime,                    Bin,                     Emp_ID,    NewColumn

27011,          SINGLES-BULK-082,          A,                    27011
27051,         SINGLES-BULK-055,           A,                 27011

 

Any help would be highly appreciated.

Labels (1)
7 Replies
markgraham123
Specialist
Specialist
Author

sunny_talwar

Try this

T2:
LOAD StartTime,
	 Bin,
	 Emp_ID,
	 If(Emp_ID = Previous(Emp_ID) and Bin <> Previous(Bin) and StartTime-Previous(StartTime) <= 2000, Previous(StartTime), StartTime) as NewColumn
Resident T1
Order by Emp_ID;
markgraham123
Specialist
Specialist
Author

Sunny,

 

It is only picking first row and changing the value.

I want the values to be changed for all the values of the particular 'Bin'.

Please see attached.

sunny_talwar

May be this

T2:
LOAD RowNo() as RowNum,
	 StartTime,
	 Bin,
	 Emp_ID,
	 If(Emp_ID = Previous(Emp_ID),
	 	If(Bin = Previous(Bin), Peek('NewColumn'),
	 		If(StartTime-Previous(StartTime) <= 2000, Previous(StartTime), StartTime)), StartTime) as NewColumn
Resident T1
Order by Emp_ID;
markgraham123
Specialist
Specialist
Author

Sunny,

 

This is perfect. Meets my requirement.

What if i want to improvize this so that, the logic only wants to group 2 timings and give previous time for 2nd one.

Once it reaches 3rd time, my logic should treat 3rd time as new timing and look for 4th timing and group it to 3rd timing.

 

sunny_talwar

You would have to elaborate on this with an example and it's output

markgraham123
Specialist
Specialist
Author

@sunny_talwar    I apologize for the confusion...

 

Here is my sample data and expected output:

1. I sort it by Emp_ID and StartTime.

If(Emp_ID = Previous(Emp_ID),
If(Bin = Previous(Bin), Peek('NewColumn'),
If(StartTime-Previous(StartTime) <= 2000, Previous(StartTime), StartTime)), StartTime) as NewColumn

I can apply above logic only for 2 items for one timing. Once i reach 3rd item, i have to start fresh group with 3rd timing and run the same logic.

StartTimeBinEmp_IDNewColumn
27011SINGLES-BULK-082A27011
27948SINGLES-BULK-085A27011 (Since i reached 2 items, i will start logic from next timing now)
29308SINGLES-BULK-091A29308
29804SINGLES-BULK-066A29308
31604SINGLES-BULK-039A31604
35885SINGLES-BULK-095A35885
36444SINGLES-BULK-063A35885
37586SINGLES-BULK-097A37586
38016SINGLES-BULK-090A37586