# Attribute Rule Calculation off of a field in another table

260
5
01-17-2022 11:58 AM
New Contributor III

Hello All,

I am trying to do something that I am rather unfamiliar with. I do not know attribute rules too well and I am looking at the best way to have a calculation off of a field on another table. There are GlobalID fields on both tables that are being used as my join field so I can tell what to update what. For example: I would like it when you update [FieldA] on [Table1] I would like it to gather the sum of [FieldA] on [Table2] and populate [FieldB] on [Table1], this would be done by using [GlobalID] on [Table1] and [Table2GlobalID] on [Table2] as my joining fields.

I am not sure if I need a join set up to allow if to be seen. I am not exactly sure what the best way to do this is.

I hope this explains what I am trying to do.

Thank you
1 Solution

Accepted Solutions
New Contributor III

Okay I did a little bit more  research and realized my error. I was not doing anything correctly. I want to have Elevation - InvertDownField in my new field which is currently blank.

Elevation is [Table2].[FieldA]

InvertDownField is [Table1].[FieldA]

Here is what  I have

```// Calculation Attribute Rule on Table1
// field: FieldB, Table1
// triggers: insert, update// load the related features of Table2 using one of these methods:var fs_table2 = FeatureSetByName(\$datastore, "[Table2]", ["*"], false)var key = \$feature.GlobalIDfs_table2 = Filter(fs_table2, "Table2GlobalID = @key")// get the minus of the two tables//This is from Table 2var t2_a_minus1 = Sum(fs_table2, "Elevation") //This is from Table 1var t2_a_minus2 = \$feature.InvertDownField// returnreturn t2_a_minus1 - t2_a_minus2```

Thank you
5 Replies
MVP Regular Contributor
``````// Calculation Attribute Rule on Table1
// field: FieldB
// triggers: insert, update

// load the related features of Table2 using one of these methods:

// if you have a relationship class between the tables:
var fs_table2 = FeatureSetByRelationshipName(\$datastore, "NameOfTheRelationshipClass", ["*"], false)
// if not:
var fs_table2 = FeatureSetByName(\$datastore, "Table2", ["*"], false)
var key = \$feature.GlobalID
fs_table2 = Filter(fs_table2, "Table2GlobalID = @key")

// get the sum of Table2.FieldA
var t2_a_sum = Sum(fs_table2, "FieldA")

// return
return t2_a_sum``````

Have a great day!
Johannes
New Contributor III

Johannes,

Thank you so much for getting back to me on this.

I am not getting this to work it keeps populating everything with "nan" I am thinking it is because of null values in either of  the fields.

They also wanted minus but I do not think that is the issue because it does not seem to be working with sum either.

`Here is what I have can you further assist? // load the related features of Table2 using one of these methods:var fs_table2 = FeatureSetByName(\$datastore, "[Database].[TableName]", ["*"], false)var key = \$feature.GlobalIDfs_table2 = Filter(fs_table2, "Table2GlobalID = @key")// get the minus of the two tablesvar t2_a_sum = iif (\$feature.invertDownField < 1, 0,iif ("Elevation" < 1, 0, "Elevation" - \$feature.invertDownField)))// returnreturn t2_a_sum `

Thank you
MVP Regular Contributor

OK, that probably gets a little more complicated. But I can't make sense of your equation.

Am I correct in guessing that Elevation is a field in Table2, and invertDownField is a field in Table1?

Can you describe what you want to calculate?

Have a great day!
Johannes
New Contributor III

Okay I did a little bit more  research and realized my error. I was not doing anything correctly. I want to have Elevation - InvertDownField in my new field which is currently blank.

Elevation is [Table2].[FieldA]

InvertDownField is [Table1].[FieldA]

Here is what  I have

```// Calculation Attribute Rule on Table1
// field: FieldB, Table1
// triggers: insert, update// load the related features of Table2 using one of these methods:var fs_table2 = FeatureSetByName(\$datastore, "[Table2]", ["*"], false)var key = \$feature.GlobalIDfs_table2 = Filter(fs_table2, "Table2GlobalID = @key")// get the minus of the two tables//This is from Table 2var t2_a_minus1 = Sum(fs_table2, "Elevation") //This is from Table 1var t2_a_minus2 = \$feature.InvertDownField// returnreturn t2_a_minus1 - t2_a_minus2```

Thank you
New Contributor III

Th code above worked. I was getting an error  but after closing and reopening Pro it worked. thanks for your help.

Thank you