You have to calculate the group counts in different manner. One quite simple option could be to add new column to your dt which combines leg and layer
dt << new column("LEGLAYER", Character, Nominal, Formula(:Leg ||:Layer));
then use that in the summary
dt_summary = dt << Summary(
Group(:SampleGroup),
Max(:LSL),
//Mean(:Target),
Min(:USL),
N Categories(:LEGLAYER),
Freq("None"),
Weight("None"),
statistics column name format("column"),
Link to original data table(0)
);
and get the curcount from that
curcount = Sum(dt_summary[i, {"LEGLAYER"}]);

-Jarmo