cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.

How to find max/min values from columns with certain keyword in the column names

ssg
ssg
Level I

Hi, I am trying to process data sets to find the max values of certain columns(based on certain keyword in the column names). The number of columns may differ for each data set, but the column name "keywords" will remain the same. Trying to create a script that could work on different data sets. Below is a simple example of a data table to illustrate it:

 

Rowdata1_volt_grp1data1_volt_grp2data2_volt_grp1data2_volt_grp2data_current_A_grp1data_current_A_grp2data2_current_A_grp1data2_current_A_grp2max_volt_grp1max_current_grp2
A0.50.80.70.21.21.40.91.80.71.8
B0.60.90.30.411.11.50.80.61.1

 

1. Need to find max values of columns with column name consisting  volt & grp1 in the naming. And populate this data in new column "max_volt_grp1".

2. Need to find max values of columns with column name consisting  current & grp2 in the naming. And populate this data in new column "max_current_grp2".

3. The outcome expected are on the last 2 columns. 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How to find max/min values from columns with certain keyword in the column names

My mistake, try this method.  I don't like using this Eval(Parse()) methodology, but it was the only way I could make it work.

The code creates a character string that contains the JSL required to create the new column, and then runs the JSL

Names Default To Here( 1 );
dt = Current Data Table();
colNames = dt << get column names( string );

// Build a list of the columns to be evaluated
theCols = "";
For Each( {col}, colNames,
	If( Contains( col, "volt" ) & Contains( col, "grp1" ),
		theCols = theCols || ", :" || col
	)
);

// Create the new column and formula command, adding in the list of
// columns ceated in the above loop and then executing it
Eval(
	Parse(
		"dt << new column(\!"max_volt_grp1\!",formula(Max(" ||
		Substr( theCols, 2 ) || ")));"
	)
);
Jim

View solution in original post

4 REPLIES 4
txnelson
Super User

Re: How to find max/min values from columns with certain keyword in the column names

Here is how I would do this to create column Max_volt_grp

txnelson_0-1741697711040.png

Names Default To Here( 1 );
dt = Current Data Table();
colNames = dt << get column names( string );
valueMatrix = [];
//col = "data1_volt_grp1"
For Each( {col}, colNames,
	If( Contains( col, "volt" ) & Contains( col, "grp1" ),
		valueMatrix = valueMatrix |/ Matrix( Column( col ) << get values )
	)
);
If( Length( valueMatrix ) > 0,
	dt << New Column( "Max_volt_grp1" );
	:Max_volt_grp1[1] = Max( valueMatrix );
);
Jim
ssg
ssg
Level I

Re: How to find max/min values from columns with certain keyword in the column names

Hi Jim, 

The solution gets the max value on those column for the entire number of rows. I am looking for the script to have the column max value populated for each row separately (the number of rows will also differ based on the dataset). Expecting output for each row as in the highlighted column. Thanks for your help.

ssg_0-1741744300825.png

 

txnelson
Super User

Re: How to find max/min values from columns with certain keyword in the column names

My mistake, try this method.  I don't like using this Eval(Parse()) methodology, but it was the only way I could make it work.

The code creates a character string that contains the JSL required to create the new column, and then runs the JSL

Names Default To Here( 1 );
dt = Current Data Table();
colNames = dt << get column names( string );

// Build a list of the columns to be evaluated
theCols = "";
For Each( {col}, colNames,
	If( Contains( col, "volt" ) & Contains( col, "grp1" ),
		theCols = theCols || ", :" || col
	)
);

// Create the new column and formula command, adding in the list of
// columns ceated in the above loop and then executing it
Eval(
	Parse(
		"dt << new column(\!"max_volt_grp1\!",formula(Max(" ||
		Substr( theCols, 2 ) || ")));"
	)
);
Jim
ssg
ssg
Level I

Re: How to find max/min values from columns with certain keyword in the column names

Thanks Jim. The scripts works as expected.