cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
ENTHU
Level IV

How to use column name in the formula ?

I have a table as below.I need to find the sum of (TA008+SA2900 to SA2950+SA3400 to SA3500+TA99) for every item.How can have jsl do this?

ItemSA0100SA0801SA0804SA0817SA0818SA0871SA0874SA2930SA2950SA2982SA2983SA2984SA3403SA3404SA3422SA3423SA3424SA3452SA3474SA3476SA3477SA3491SA3534SA9706SA9787SA9798SA9901SA9908TA001TA008TA029TA034TA035TA088TA097TA099
8A63012 2                        7122     7
8A6311814  1                     11186     11
8A63212 1 31                     21125     21
A63381 1 22                  1  13815    113
A629249  1 2                  1  332493    133
A626380   23                     133805     13
A60011 1   1                1   3112    13
1 ACCEPTED SOLUTION

Accepted Solutions
Georg
Level VII

Re: How to use column name in the formula ?

Here's just another way how to deal with columns to make a formula:

// expression way
cdt=current data table();
sum_lst2 = {};
col_lst2 = cdt << get column names( Numeric );
For( i = 1, i <= N Items( col_lst2 ), i++,
	colname = Column( col_lst2[i] ) << get name;
	If( Contains( {"TA008", "TA099"}, colname ) | (colname >= "SA2900" & colname <= "SA2950") | (colname >= "SA3400" & colname <= "SA3550"),
		Insert Into( sum_lst2, col_lst2[i] )
	);
);
Eval( Eval Expr( cdt << New Column( "Sums_expr", formula( Sum( Expr( sum_lst2 ) ) ) ) ) );
Georg

View solution in original post

4 REPLIES 4
txnelson
Super User

Re: How to use column name in the formula ?

Names Default To Here( 1 );
dt = Current Data Table();
dt << New Column( "Sums",
	formula(
		Sum(
			:SA2930,
			:SA2950,
			:SA3403,
			:SA3404,
			:SA3422,
			:SA3423,
			:SA3424,
			:SA3452,
			:SA3474,
			:SA3476,
			:SA3477,
			:SA3491,
			:TA008,
			:TA099
		)
	)
);

The above will add a new column to your data table with the sum being added from the required columns into the new column

sums.PNG

Jim
ENTHU
Level IV

Re: How to use column name in the formula ?

The data set is dynamic.I could have columns with names SA3400 to SA3450 and SA2900 to SA2950.I would have to mention over 100 column names if I followed this approach.Trying to fins a way to simplify.

txnelson
Super User

Re: How to use column name in the formula ?

Ah......that requirement was not stated in the original question.

However, the code to do what you want is repetitive, but not difficult.

names default to here(1);
dt=current data table();

colNames = dt << get column names(string);

sumList = "";

if(contains(colNames,"TA008"),
	sumList = ":TA008";
);
For(i=2900, i<=2950, i++,
	If( contains(colNames,"SA" || char(i)),
		if( sumList != "", sumList = sumList || ",");
		sumList = sumList || ":SA" || char(i);
	)
);
For(i=3400, i<=3500, i++,
	If( contains(colNames,"SA" || char(i)),
		if( sumList != "", sumList = sumList || ",");
		sumList = sumList || ":SA" || char(i);
	)
);
if(contains(colNames,"TA099"),
	if( sumList != "", sumList = sumList || ",");
	sumList = sumList || ":TA099";
);
Eval(
	Parse(
		"dt << new column(\!"Sums\!",formula(sum( " || sumList ||
		" )));"
	)
);
Jim
Georg
Level VII

Re: How to use column name in the formula ?

Here's just another way how to deal with columns to make a formula:

// expression way
cdt=current data table();
sum_lst2 = {};
col_lst2 = cdt << get column names( Numeric );
For( i = 1, i <= N Items( col_lst2 ), i++,
	colname = Column( col_lst2[i] ) << get name;
	If( Contains( {"TA008", "TA099"}, colname ) | (colname >= "SA2900" & colname <= "SA2950") | (colname >= "SA3400" & colname <= "SA3550"),
		Insert Into( sum_lst2, col_lst2[i] )
	);
);
Eval( Eval Expr( cdt << New Column( "Sums_expr", formula( Sum( Expr( sum_lst2 ) ) ) ) ) );
Georg