Hi @jthi and @txnelson ,
Thank you both for the valuable feedback. All your suggestions gave me some ideas on what to work on and how to modify the existing code to get what I needed. I liked @txnelson 's idea of the start date and end date, but unfortunately, it doesn't allow non-continuous selection of months and or quarters. For example, a user might select Jan and Sep as months or Q1 and Q4 as quarters to then grab the subset from. So, I had to rewrite that to allow for non-continuous dates. I even threw in a check if the user was selecting months and quarters or YTD and any mix so they'd have to go back and select just one group or another.
My solution to this was actually to use Associative Arrays, and I was pleased with the outcome as well as utilizing AA's since I haven't really worked much with them. The code I came up with is below. I know it's not very elegant or compact, but it works and gets the job done, which is sometimes all that's needed, right?!
Thanks for all the help and quick feedback. I love being able to pose a problem on this forum and then less than 24hrs later have a functioning script that solves all the concerns I needed. Below is the code I ended up writing:
Names Default To Here( 1 );
dt = Current Data Table();
yr = Year( Today() );
MonthAA = Associative Array( {"M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8", "M9", "M10", "M11", "M12"}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} );
QAA = Associative Array( {"Q1", "Q2", "Q3", "Q4"}, {1, 2, 3, 4} );
YTDAA = Associative Array( {"YTD"}, {12} );
MonthList = {};
QList = {};
QMonths = {};
YTDList = {};
ClearExpr = Expr(
dt << Clear Select;
MonthList = {};
QList = {};
QMonths = {};
YTDList = {};
BB1 << Set( 0 );
BB2 << Set( 0 );
BB3 << Set( 0 );
BB4 << Set( 0 );
BB5 << Set( 0 );
BB6 << Set( 0 );
BB7 << Set( 0 );
BB8 << Set( 0 );
BB9 << Set( 0 );
BB10 << Set( 0 );
BB11 << Set( 0 );
BB12 << Set( 0 );
BB13 << Set( 0 );
BB14 << Set( 0 );
BB15 << Set( 0 );
BB16 << Set( 0 );
BB17 << Set( 0 );
);
OKExpr = Expr(
dt << Clear Select;
MonthList = {};
QList = {};
QMonths = {};
YTDList = {};
For( i = 1, i <= 17, i++,
tempBB = Eval( Parse( "BB" || Char( i ) ) ) << Get;
If( tempBB == 1,
If(
Substr( Eval( Parse( "Sel" || Char( i ) ) ), 1, 1 ) == "M", Insert Into( MonthList, Eval( Parse( "Sel" || Char( i ) ) ) ),
Substr( Eval( Parse( "Sel" || Char( i ) ) ), 1, 1 ) == "Q", Insert Into( QList, Eval( Parse( "Sel" || Char( i ) ) ) ),
Substr( Eval( Parse( "Sel" || Char( i ) ) ), 1, 1 ) == "Y", Insert Into( YTDList, Eval( Parse( "Sel" || Char( i ) ) ) )
)
);
);
If(
N Items( MonthList ) != 0 & N Items( QList ) != 0 | N Items( MonthList ) != 0 & N Items( YTDList ) != 0 | N Items( QList ) != 0 &
N Items( YTDList ) != 0,
ErrorWin = New Window( "Error Window",
<<Type( "Dialog" ),
V List Box(
Spacer Box(),
Text Box( "Invalid selection. Please 'Clear' and select month(s), quarter(s), OR YTD.", <<Set Font Size( 16 ) ),
H List Box( Spacer Box(), Button Box( "OK", ErrorWin << Close Window ), Spacer Box() )
)
);
Error = 1;
,
Error = 0
);
If( N Items( MonthList ) != 0 & Error == 0,
MonthNum = MonthAA << Get Values( MonthList );
For( m = 1, m <= N Items( MonthList ), m++,
dt << Select Where( :"Month Year[Date]"n == As Date( Date DMY( 1, MonthNum[m], yr ) ), Current Selection( "extend" ) )
);
dtSubset = dt << Subset( Selected Columns( 0 ), Selected Rows( 1 ), Suppress formula evaluation( 0 ) );
);
If( N Items( QList ) != 0 & Error == 0,
QNum = QAA << Get Values( QList );
For( q = 1, q <= N Items( QNum ), q++,
If(
QNum[q] == 1, Insert Into( QMonths, {"M1", "M2", "M3"} ),
QNum[q] == 2, Insert Into( QMonths, {"M4", "M5", "M6"} ),
QNum[q] == 3, Insert Into( QMonths, {"M7", "M8", "M9"} ),
QNum[q] == 4, Insert Into( QMonths, {"M10", "M11", "M12"} )
)
);
MonthNum = MonthAA << Get Values( QMonths );
For( m = 1, m <= N Items( QMonths ), m++,
dt << Select Where( :"Month Year[Date]"n == As Date( Date DMY( 1, MonthNum[m], yr ) ), Current Selection( "extend" ) )
);
dtSubset = dt << Subset( Selected Columns( 0 ), Selected Rows( 1 ), Suppress formula evaluation( 0 ) );
);
If( N Items( YTDList ) != 0 & Error == 0,
YTDNum = YTDAA << Get Values( YTDList );
For( m = 1, m <= YTDNum[1], m++,
dt << Select Where( :"Month Year[Date]"n == As Date( Date DMY( 1, m, yr ) ), Current Selection( "extend" ) )
);
dtSubset = dt << Subset( Selected Columns( 0 ), Selected Rows( 1 ), Suppress formula evaluation( 0 ) );
);
);
subwin = New Window( "Select Subset",
<<Type( "Dialog" ),
<<Size( 380, 200 ),
Panel Box( "Select Month(s) OR quarter(s) OR YTD in Current Year for Subset",
LB = Lineup Box( N Col( 5 ),
BB1 = Button Box( "Jan", Sel1 = "M1", <<Style( "Toggle" ) ),
BB2 = Button Box( "Feb", Sel2 = "M2", <<Style( "Toggle" ) ),
BB3 = Button Box( "March", Sel3 = "M3", <<Style( "Toggle" ) ),
Spacer Box(),
BB4 = Button Box( "Q1", Sel4 = "Q1", <<Style( "Toggle" ) ),
BB5 = Button Box( "April", Sel5 = "M4", <<Style( "Toggle" ) ),
BB6 = Button Box( "May", Sel6 = "M5", <<Style( "Toggle" ) ),
BB7 = Button Box( "June", Sel7 = "M6", <<Style( "Toggle" ) ),
Spacer Box(),
BB8 = Button Box( "Q2", Sel8 = "Q2", <<Style( "Toggle" ) ),
BB9 = Button Box( "July", Sel9 = "M7", <<Style( "Toggle" ) ),
BB10 = Button Box( "Aug", Sel10 = "M8", <<Style( "Toggle" ) ),
BB11 = Button Box( "Sep", Sel11 = "M9", <<Style( "Toggle" ) ),
Spacer Box(),
BB12 = Button Box( "Q3", Sel12 = "Q3", <<Style( "Toggle" ) ),
BB13 = Button Box( "Oct", Sel13 = "M10", <<Style( "Toggle" ) ),
BB14 = Button Box( "Nov", Sel14 = "M11", <<Style( "Toggle" ) ),
BB15 = Button Box( "Dec", Sel15 = "M12", <<Style( "Toggle" ) ),
Spacer Box(),
BB16 = Button Box( "Q4", Sel16 = "Q4", <<Style( "Toggle" ) ),
BB17 = Button Box( "YTD", Sel17 = "YTD", <<Style( "Toggle" ) ),
Spacer Box(),
Spacer Box(),
Spacer Box(),
Spacer Box(),
Spacer Box(),
Spacer Box(),
Button Box( "Clear", ClearExpr ),
Button Box( "OK", OKExpr ),
Button Box( "Cancel/Exit", subwin << Close Window )
)
)
);
Thanks!,
DS