Finding Difference between two text fields

1301
9
Jump to solution
03-06-2014 07:23 AM
WahajHabib
New Contributor
I have two fields (text) which have the land use of two different years but the area is same...Now i want to calculate the difference in a new field....How can i do that....
PS:I tried with field calculator (-) command but nothing happens...
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor
Live automation is not easily implemented, but ModelBuilder can store a complex procedure that selects features adn processes multiple field calculations that you only need to define once.  After that as long as the fields are found, the model just needs to be run to do the update.  Running the model is done manually and can be triggered from a button. 

The model can be exported to a Python script if you want to do more sophisticated operations.  Pure arcpy Python can do operations like this with out using geoprocessing tools, but that requires a learning curve that you may not want to undertake for a simple task like this.  But Python does not listen for edit event triggers to run automatically either, but can be triggered from a button.

Anyway there are options that can save you from having to repeat many edit steps if they always operate according to the same rules.

For the Calculation, if you don't build a Select By Attribute step into your model (which is quicker than the calculator) you can calculate against all records with this VB Script codeblock:

If [rabbi2005] <> [rabbi2012] Then   Output = [rabbi2005] & " - " & [rabbi2012] Else   Output = [Your_New_Field] End If


expression:  Output

View solution in original post

9 Replies
RichardFairhurst
MVP Honored Contributor
I have two fields (text) which have the land use of two different years but the area is same...Now i want to calculate the difference in a new field....How can i do that....
PS:I tried with field calculator (-) command but nothing happens...


What are the two existing text fields named?  Please provide an example of two different values that can occur in these fields and how you want the different between them to appear in the new field.  Are both text values actually to be treated as numeric values and you want to subtract one from the other?

The Field Calculator can do a difference comparison between two fields in a single feature or record, but it cannot perform calculations between separate features or records without a join having first taken place to connect the two features/records.
0 Kudos
RobertBorchert
Frequent Contributor III
Check first to make sure you attributes are both the same type of data in the attribute properties.
i.e. String, Integer, ETC...


I have two fields (text) which have the land use of two different years but the area is same...Now i want to calculate the difference in a new field....How can i do that....
PS:I tried with field calculator (-) command but nothing happens...
WahajHabib
New Contributor
Check first to make sure you attributes are both the same type of data in the attribute properties.
i.e. String, Integer, ETC...

Both fields contain String value
0 Kudos
WahajHabib
New Contributor
[ATTACH=CONFIG]31981[/ATTACH]  Note the fields rabbi_2005 and rabbi_2012 both have same values but note the highlighted values the are different there are some more like that i want that differnce to be shown in a new field.....
0 Kudos
WahajHabib
New Contributor
I think I understand what you mean when looking at the data I see the different values in the two fields you highlighted. 

What should the difference field value be for the difference between 'Current Fellow' and  'Wheat' ???

I have written code in the past to compare string values and find the difference, but this can get a bit complicated, when comparing each character with the same positional value with the character from the other field.

Or do you want to concatenate (combine) the values where the field values differ?  If so, then you just need to select the features where the values are not equal and then use the field calculator on selected features to combine them.  Something like this: 

[<FIELD1 NAME>] & " - " & [<FIELD2 NAME>]


What type of data are you using? What version of ArcGIS?
ArcGIS Version 10.2.1....The combine method is good one...but is'nt there any automated process (rather then manually selecting the fields which differ) i want it to be calculated automatically with some code....Thanks
0 Kudos
RichardFairhurst
MVP Honored Contributor
ArcGIS Version 10.2.1....The combine method is good one...but is'nt there any automated process (rather then manually selecting the fields which differ) i want it to be calculated automatically with some code....Thanks


Attribute Assistant is the only option for people that don't do .Net programming for attribute calculation automation.  I don't use it, but search for it on the forum.  I don't do anything automated and just run QC script at regular intervals or prior to posting to publication servers.

ArcMap table fields have no ability to behave like a spreadsheet cell without an add-on extension that enables event listeners.  You cannot write a formula directly to a field that automatically updates in response to edits in other fields without an extension like Attribute Assistant or a custom .Net extension.  Even databases like Access don't do this without a user form that may grow to hundreds of lines of VBA code underneath.  Databases normally do not update in real time, but use scripts containing calculations, update queries or cursors triggered by the user or a scheduled timer.  Live automation requires much more effort to set up and done poorly can screw things up pretty easily.
WahajHabib
New Contributor
Have you tried an attribute query like this :

Select by Attributes
FIELD NAME1 <> FIELD NAME2


Then use the field calculator to calculate and combine the fields for those selected records only.  If the query works, you wouldn't need to do any custom programming, you should be able to use model builder and do it without manually selecting the fields that differ.  I know the query works with SDE/SQL, and I also tested the query with a file geodatabase and it worked  for me at 10.2.

Hope that helps...

Yes That's what i needed....thanks alot
0 Kudos
RichardFairhurst
MVP Honored Contributor
Live automation is not easily implemented, but ModelBuilder can store a complex procedure that selects features adn processes multiple field calculations that you only need to define once.  After that as long as the fields are found, the model just needs to be run to do the update.  Running the model is done manually and can be triggered from a button. 

The model can be exported to a Python script if you want to do more sophisticated operations.  Pure arcpy Python can do operations like this with out using geoprocessing tools, but that requires a learning curve that you may not want to undertake for a simple task like this.  But Python does not listen for edit event triggers to run automatically either, but can be triggered from a button.

Anyway there are options that can save you from having to repeat many edit steps if they always operate according to the same rules.

For the Calculation, if you don't build a Select By Attribute step into your model (which is quicker than the calculator) you can calculate against all records with this VB Script codeblock:

If [rabbi2005] <> [rabbi2012] Then   Output = [rabbi2005] & " - " & [rabbi2012] Else   Output = [Your_New_Field] End If


expression:  Output
RichardFairhurst
MVP Honored Contributor
Wahaj:

You can press the up arrow to give a point to as many responses in the thread as you want for any and all posts written by someone other than yourself.  But you can only choose one post to be the answer.  The answer post could be any post, including a post that you wrote if you answer your own question.  You may want to include points on some of the other posts that also helped to answer your question and keep that in mind if you get help from multiple users in your future posts.