Dear @Markku ,
i also struggle to create dynamic scripts and parse them into a column formula. In the past i had a similar situation to yours, where the number of repeated measures (in your case batches repeat each day) could change from one observation (batch) to another.
my solution may look a bit rough for your needs but i think it should work.
please do check if it robust to different scenarios you may have before usage.
with your data table open run this script and inspect the results.
Names Default To Here( 1 );
dt = Current Data Table();
// get list of columns
Cols = dt << get column names ( Continuous );
maxlist = {};
For( i = 1, i <= N Items( cols ), i++,
If( Contains( cols[i], "Max" ),
Insert Into( maxlist, cols[i] )
)
);
dt2 = dt << Stack(
columns(maxlist ),
Source Label Column( "Time" ),
Stacked Data Column( "Data" )
);
dt2 << Sort(
By( :Batch#, :Time ),
Order( Ascending, Descending ),
replace table
);
dt2 <<New Column( "Index",
Numeric,
"Continuous",
Format( "Best", 12 ),
Formula( Sum( :Batch#[Index( 1, Row() )] == :Batch# ) )
);
dt2:Index << Delete Formula;
dt2 << Select where ( and ( ismissing (:Data), :Index == colmin (:Index, :Batch#) ) );
dt2 << Go To (:Batch#);
dt2 << Select Matching Cells();
dt2 << invert row selection ();
dt2 << delete rows ();
dt2 << select where (! ismissing (:Data))<< delete rows ();
dt2 <<New Column( "Index2",
Numeric,
"Continuous",
Format( "Best", 12 ),
Formula( Sum( :Batch#[Index( 1, Row() )] == :Batch# ) )
);
dt2:Index2 << Delete Formula;
dt2 << select where (:Index > :Index2 ) << delete rows ();
dt3 = dt2 << Summary(
Group( :Batch# ),
Freq( "None" ),
Weight( "None" ),
Link to original data table( 0 ),
);
close (dt2, no save);
dt3:N Rows << set name ("No of last days missing");
dt << new column ("No of last days missing", set each value (0));
dt << Update(
With( dt3 ),
Match Columns( :Batch# = :Batch# )
);
close (dt3, no save);
//
dt5 = dt << Stack(
columns(maxlist ),
Source Label Column( "Time" ),
Stacked Data Column( "Data" )
);
dt5 << select where (ismissing (:Data)) << delete rows;
dt5 << select where (:Data<=0.02) << delete rows;
dt6 = dt5 << Summary(
Group( :Batch# ),
Freq( "None" ),
Weight( "None" ),
Link to original data table( 0 )
);
close (dt5, no save);
dt6:N Rows << set name ("Days over 0.02");
dt << new column("Days over 0.02", set each value(0));
dt<< Update(
With( dt6 ),
Match Columns( :Batch# = :Batch# )
);
close (dt6, no save);
let us know if it works,
Ron