Just combining/summarizing what was mentioned. Make sure you sort the data before hand or the Lag() function will have issues. Also, using a formula will be faster than a for each row() command. If you are worried about post sorting that may affect the lag() function later on, just delete the formula to avoid conflicts. Thus:
NamesDefaultToHere(1);
// Table
dt1 = New Table( "Sebastian",
New Column( "ID",
Numeric,
"Nominal",
Format( "Best", 12 ),
Set Values( [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 2, 2] )
),
New Column( "ID2",
Character,
"Nominal",
Set Values(
{"A", "B", "C", "D", "A", "B", "C", "C", "A", "B", "C", "D", "A", "B",
"C", "C", "C", "B"}
)
),
New Column( "Value",
Numeric,
"Nominal",
Format( "Best", 12 ),
Set Values(
[111, 222, 333, 444, 111, 222, 333, 555, 111, 222, 555, 444, 111, 222,
555, 666, 777, 888]
)
)
);
// the table needs to be sorted for the formula in the next step to work
dt1 << Sort(Replace table,
By( :ID, :ID2 ),
Order( Ascending, Ascending )
);
// Add an auxiliary column
dt1 << New Column( "Repeat",
Numeric,
"Nominal",
Format( "Best", 12 ),
Formula( If(:ID2 != Lag(:ID2, 1) | Row() == 1, 1, Lag(:Repeat, 1) + 1) )
);
:Repeat << Delete Formula; // faster than 'for each row()'
// Split
dt2 = dt1 << Split(
Split By( :ID2 ),
Split( :Value ),
Group( :ID, :Repeat )
);
// Delete aauxiliary column
dt2 << deleteColumn(:Repeat);