Here is a rework of the code to incorporate Part ID into the mix
Names Default To Here( 1 );
dt = Current Data Table();
dtout = New Table( "Outputs",
New Column( "Part ID", character ),
New Column( "FirstMin" ),
New Column( "SecondMin" )
);
Current Data Table(dt);
For( i = 1, i <= N Rows( dt ) - 1, i++,
// At the start of the looping through the data and when the Part ID
// changes, the FirstMin and SecondMin values need to be set to
// missing values
If( i == 1,
FirstMin = .;
SecondMin = .;,
dt:Part ID != dt:Part ID[i - 1],
FirstMin = .;
SecondMin = .;
);
// If a value is found crossing zero, process the data
If( (DT:y >= 0 & DT:y[i + 1] <= 0) | (DT:y <= 0 & DT:y[i + 1] >= 0),
If( Abs( DT:y ) < Abs( DT:y[i + 1] ),
min = DT:y;
minrow = I;
,
min = DT:y[i + 1];
minrow = i + 1;
);
// If this is the frist crossing, set the values for firstMin
If( Is Missing( FirstMin ),
FirstMin = min;
FirstRow = minrow;,
// If this is the second crossing, set the values for SecondMin
// and also select the rows in the input data table
// and finally output first and second results to the output
// data table
// Because we are only collecting data for the first 2 crossings
// and are using the FirstMin and SecondMin values being
// missing as triggers in the script, once both First and Second
// minimums are set to actual values, all subsequent crossings
// will be ignored, until a Part ID change is detected
Is Missing( SecondMin ),
SecondMin = min;
SecondRow = minrow;
// Point back to the input data table to select the rows
current data table(dt);
Selected( Row State( Firstrow ) ) = 1;
Selected( Row State( Secondrow ) ) = 1;
dtout << add rows( 1 );
dtout:Part ID[N Rows( dtout )] = dt:Part ID[SecondRow];
dtout:FirstMin[N Rows(dtout)] = FirstMin;
dtout:SecondMin[N Rows(dtout)] = SecondMin;
);
);
);
Jim