Select to view content in your preferred language

Using Conditional Formatting to Populate a New Field Using Field Calculator.

4015
5
09-25-2015 05:04 PM
BillFearrington
Emerging Contributor

I am trying to use conditional statement to populate a new field using the Field Calculator.  I have a column named "Days from Appt."  I would like the field to populate if less than 7 days "One week or less."  I named this new column Duration.  Can someone please help a newbie to programming with this?  Can either  be in VB or Python. Thanks.

Tags (1)
0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

Since you only have one condition why not make it quick

  • make a text field if not already done
  • query your Days from Appt field for values < 7 assuming it is numeric (won't matter if it is text unless >= 10)
  • go to your new field and use the field calculator to past in "One week or less"
  • switch the selection, type/select  str(  ) and select your source field  so it yields  str(!your really long field name above!) enclosed in ! marks if using Python...

or in totally untested code and assuming your source field is numeric

python parser

def appt(input_field):
    """no error checking"""
    if input_field < 7:  # assuming a numeric field otherwise int(input_field)
        val = "One week or less"
    else:
        val = str(input_field)
    return val

field expression  !your_field_here!

obviously changing !your_field_here! to you know, your field

0 Kudos
BillFearrington
Emerging Contributor

Thanks for your response.  I forgot to mention that I need to have several if statements.  I will have 8-14 days = Two weeks or less, 15-21 = Three weeks or less.  Also I have negative days which will be greater than one week, etc.

0 Kudos
BillFearrington
Emerging Contributor

Also I tried this before I left for the day.  I would put the code in the code block and what would I use

for the box under the code block?

0 Kudos
DanPatterson_Retired
MVP Emeritus

the !your_field_here! 

for homework...

if a < 7:

  val = do blah

elif (a >= 7) and (a < 14):

  val = more blah

...

...

else:

    val = final blah

return val

BillFearrington
Emerging Contributor

Thanks for your help.  I'll have to give it a try on Monday.  Wish Me Luck.  🙂

0 Kudos