Christine,
Here is a script that does what you are asking for. I tested it on the small snippet of data you provided, and it appears to accomplish what you want.
Names Default To Here( 1 );
dt = Current Data Table();
// Create a New column to track found groups
dt << New Column( "Grouping", set each value( . ) );
countGroup = 0;
// Step through all data
For( i = 1, i <= N Rows( dt ) - 1, i++,
// Look ahead to see if triger criteria has been met
// and if it has, then read backwards until it fails
If( dt:Name( "Target # Days from Previous" ) > dt:Name( "Target # Days from Previous" )[i + 1] & dt:Visit Sequence < dt :Visit Sequence[i + 1],
// Trigger has been found, so read backwards until not true
countGroup++;
compareStart = i;
While( dt:Name( "Target # Days from Previous" )[compareStart] > dt:Name( "Target # Days from Previous" )[i + 1] & dt:Visit Sequence[ compareStart] < dt:Visit Sequence[i + 1],
dt:Grouping[compareStart] = countGroup;
Selected( Row State( compareStart ) ) = 1;
compareStart--;
);
)
);
Jim