2

How can I create a new attribute field which has results based on a simple combination of two other string fields, except when a third field gives a certain result, in which case I would like that result to be the output rather than the combination of the other two fields?

3
  • Welcome to GIS SE! What version of ArcGIS Desktop are you using? Can you give an example of what's in your fields and the output you would expect in the different situations?
    – Midavalo
    Mar 3, 2017 at 16:24
  • Are you asking how to create the field? Or just how to calculate it once you have the field?
    – Midavalo
    Mar 3, 2017 at 16:26
  • Please edit your question when adding/updating any information
    – Midavalo
    Mar 3, 2017 at 16:27

2 Answers 2

8

You could create the new text field, and then run the Field Calculator on it.

Using Python, you could do:

def combine(field1, field2, field3):
    if str(field3) != "CertainValue":
        return "{0}{1}".format(field1, field2)

    else:
        return str(field3)

So, this is saying, if the value in field3 is not equal to this CertainValue then return field1 and field2 concatenated. Otherwise, just return the field3 value.

Then at the bottom of the Field Calculator, you'd put:

combine(!yourField1!, !yourField2!, !yourField3!)

I'm assuming that by saying result to be the output - this is the value of the third field?

enter image description here

4
  • +1 Also return str(field1) + ' ' + str(field2) to include a space between the two text values
    – Midavalo
    Mar 3, 2017 at 16:39
  • @Midavalo yeah, I read it literally, so I didn't put a space, but they might want one, so thanks for the addition.
    – ianbroad
    Mar 3, 2017 at 16:42
  • 3
    I think I'd favor '{} {}'.format(field1, field2), personally.
    – jpmc26
    Mar 3, 2017 at 20:09
  • @jpmc26 yea, I agree.
    – ianbroad
    Mar 3, 2017 at 21:43
2

A Python script is the better way to do this but if you don't know Python and you only have two different conditions you need to deal with, you can do it manually pretty easily:

  1. Add the new field to your attribute table using the Add Field tool.
  2. Use Select by Attributes (under the Selection menu) to select the records in your table that match your "third field gives a certain result" condition.
  3. Use the field calculator to assign those records the value you want in the new field.
  4. Use Switch Selection to select the other records, which don't match your "third field gives a certain result" condition.
  5. Use field calculator again to assign those records the other value you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.