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

Formula columns generated through script do not have the correct source column name

Hi everyone, I have a script that creates new numerical columns for any columns that has very small values (i.e. transform a column that has unit meters into mm or µm for better readability).

 

It seemed to be working fine; the columns were generated without issue. But then I got some issues later on when joining with other tables, suddenly the columns showed no data anymore. When I check the formula in the new columns I see that instead of the name of the source column, it has a colnames[i] reference from the loop in the script (see screenshot).

 

Names Default To Here( 1 );
dt = Current Data Table();
colnames = dt << get column names( numeric, string );
For( i = 1, i <= N Items( colnames ), i++, 
	If( (Col Max( column(dt,colnames[i]))-Col Min( column(dt,colnames[i])))<1e-4 & (Col Max( column(dt,colnames[i]))-Col Min( column(dt,colnames[i])))>0,	
		If( !Contains( colnames, colnames[i] || "_µm"),
		dt << New Column( colnames[i] || "_µm", Formula( As Column( colnames[i] ) * 1e6 ) ) )
	,
	If( (Col Max( column(dt,colnames[i]))-Col Min( column(dt,colnames[i])))<1e-1 & (Col Max( column(dt,colnames[i]))-Col Min( column(dt,colnames[i])))>1e-4,
		If( !Contains( colnames, colnames[i] || "mm"),
		dt<<New Column(colnames[i]||"_mm",Formula(As Column(colnames[i])*1e3)))
	)
	),	
);
1 ACCEPTED SOLUTION

Accepted Solutions
ih
Super User (Alumni) ih
Super User (Alumni)

Re: Formula columns generated through script do not have the correct source column name

Hi @mvanderaa1,

 

This is a pretty common problem, can you try one of the options listed at the link below?

 

Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute

 

My go-to is the top one:

//Eval Expr
For( i = 1, i <= N Items(cols), i++,
       Eval( Eval Expr(
              dt << New Column( 
                    "2 * " || Char( cols[i] ),
                    Numeric,
                    "Continuous",
                    Formula( As Column( Expr( Char( cols[i] ) ) ) * 2)
              )
       ) )
);

 

View solution in original post

2 REPLIES 2
ih
Super User (Alumni) ih
Super User (Alumni)

Re: Formula columns generated through script do not have the correct source column name

Hi @mvanderaa1,

 

This is a pretty common problem, can you try one of the options listed at the link below?

 

Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute

 

My go-to is the top one:

//Eval Expr
For( i = 1, i <= N Items(cols), i++,
       Eval( Eval Expr(
              dt << New Column( 
                    "2 * " || Char( cols[i] ),
                    Numeric,
                    "Continuous",
                    Formula( As Column( Expr( Char( cols[i] ) ) ) * 2)
              )
       ) )
);

 

mvanderaa1
Level IV

Re: Formula columns generated through script do not have the correct source column name

That works! Thanks