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

A function to add columns with names that has a prefix

 
Hi community,
 
I want to write a function to add columns that have names that has a given prefix, and give values to the columns based on given conditions. This is the script that I have, but it errors out "Unresolved Column". I double checked everything and don't know where went wrong...
 
Much appreciated for the help!
 
add_extra_columns = Function({dt, prefix},
	{Default Local},
	dt << New Column("Data Usable?",
		Numeric,
		"Continuous",
		Format("Best", 12), 
 
 
		Set each value(
			If(
				!Is Missing(
					Column(dt, add_prefix_to_col_name("Image", prefix)),
					If(Column(dt, add_prefix_to_col_name("Status", prefix)) == "Failed",
						0,
						If(
							Is Missing(Column(dt, add_prefix_to_col_name("Area Ratio", prefix)))
							 & Column(dt, add_prefix_to_col_name("Shape", prefix)) != "Square",
							0,
							If(
								Column(dt, add_prefix_to_col_name("Status", prefix)) ==
								"Succeeded" | Column(
									dt,
									add_prefix_to_col_name("Status", prefix)
								) == "Warning",
								1
							)
						)
					)
				)
			)
		)
	)
);
 
 
add_prefix_to_col_name = Function({col_name, prefix}, // folder_path: the "\" at the end of the string is necessary
	{Default Local}, 
 
	If(prefix == "",
		new_name = prefix || col_name,
		new_name = prefix || " " || col_name
	);
 
	Return(new_name);
);
5 REPLIES 5
jthi
Super User

Re: A function to add columns with names that has a prefix

You seem to be creating single column named "Data Usable?" and no columns which have specific prefixes. Generally you would maybe want to change Column() to As Column() inside your Set Each Value but I'm not exactly sure what you are trying to do.

-Jarmo

Re: A function to add columns with names that has a prefix

Hi Jarmo,

 

Thanks for replying. Sorry that I was not clear with my question. The new column name is just "Data Usable?". But the part within the "Set each value" is giving me problems. I want to set the value of the new column name based on column names that has a certain prefix. 

jthi
Super User

Re: A function to add columns with names that has a prefix

Try changing Column() to As Column() or Column()[]

Column(dt, add_prefix_to_col_name("Image", prefix)

to

AsColumn(dt, add_prefix_to_col_name("Image", prefix)

or

Column(dt, add_prefix_to_col_name("Image", prefix)[]
-Jarmo

Re: A function to add columns with names that has a prefix

Unfortunately, all do not work, regardless AsColumn or Column()[]

jthi
Super User

Re: A function to add columns with names that has a prefix

Then I would try to simplify your function add_extra_columns. Make it have only one if statement and after you get that working start adding more.

-Jarmo