We recommend switching to the latest versions of Edge, Firefox, Chrome or Safari. Using Internet Explorer will result in a loss of website functionality.

How to only show rows that pass an If else statement

Comments

2 comments

  • Avatar
    Adrian Williams

    Hi Jerome, welcome to Data3Sixty Analyze and Infogix.

    The Python code at the end of this post should output the records where the tested date is within the last week.

    The ConfigureFields script is executed once when the node initially executes and this occurs before any input records are processed. The ProcessRecords script is executed for each input record. Python module import statements should be included in the ConfigureFields script as the module only needs to be imported once. Similarly, as the first few statements in your code are creating constants it's usually better to define them in the ConfigureFields script too. You can also initialize values for variables here too and the variables are then available to the ProcessRecords script. If required you can alternatively use an if statement with the condition set to test the in-built 'node.fistExec' variable e.g.

    if node.firstExec:

      <Do something>

    However initializing variables and constants in the ConfigureFields statement is slightly more performant as the if statement does not have to be evaluated for each record.

     

    #########################

    #### ConfigureFields script ####

    ## import data package
    import datetime

    #Configure all fields from input 'in1' to be mapped
    #to the corresponding fields on the output 'out1'
    out1 += in1

    ## assign date variables
    today = datetime.date.today()

    ## define the offset period
    six_day = datetime.timedelta(days = 6)

    ## calculate the cut-off date for one week ago
    week = today - six_day

    #########################

    #### ProcessRecords Script ####

    if fields.JOBCODE_ENDDATE >= week and fields.JOBCODE_ENDDATE <= today:

      out1 += in1

    #########################

     

    Note that I modified the criteria for the if statement to match dates from today to today - 6 days, inclusive. 

     

    1
    Comment actions Permalink
  • Avatar
    Jerome Walker

    Thank you, Adrian.  

    0
    Comment actions Permalink

Please sign in to leave a comment.