Jump to content

Expression with empty doc property to sum columns if they exist


Przemek Stasica

Recommended Posts

Hi I'm trying to create a calculated column to sum values from 2 columns but only if they exist in a table.

It seems the expressison is always preevaluated when setting up so the engine disallows it.

This is contrary to Spotfire troubleshooting guide which seems to be advising this is possible.

e.g.Sum(10, [${EmptyProperty}])

But in reality when propery is empty I'm getting "Could not find column" message.

I need something to allow me to work with columns but only if they exist.

Any ideas

Many thanks

Link to comment
Share on other sites

You could try to create the column with an Iron Python script

This worked for me. Even though the system threw an exception, the calculated column is created and empty. I used the 'iris' dataset as example. However if I then try to edit it via Data>Column Properties, it shows as red.

 

from Spotfire.Dxp.Data import CalculatedColumn

 

col1='Sepal_Length'

col2='NonExistentColumn'

expression = '['+col1+'] + ['+col2+']'

print (expression)

colname='MyNewColumn'

 

cols = Document.Data.Tables["iris"].Columns

 

if cols.TryGetValue(colname)[0] == True:

pass

print('column ',colname,'already exists')

else:

try:

cols.AddCalculatedColumn(colname,expression);

except:

pass

 

#check column was created anyway

for cc in Document.Data.Tables["iris"].Columns:

print (cc.Name)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...