<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Model Builder - Caluclate Value in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/model-builder-caluclate-value/m-p/747394#M57784</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello All&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm using the Calculate Value Tool within Model Builder to take a user variable (a date) to create a where clause to apply as the Expression on a Make Query Table. Please see below the values I've used for my Calculate Value Tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run the model I'm getting the following error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000539: Error running expression: x(str(Start_Date)) &amp;lt;type 'exceptions.NameError'&amp;gt;: name 'Start_Date' is not defined&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (CalcQueryWhere).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It looks to me that the Python script is not receiving the user variable as intended. Can any body help me with this? I really hope so, it would be very much appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Chris Mathtews&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;x("Start_Date")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code Block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;def x(Start_Date):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strDay = Start_Date[:2]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strMonth = Start_Date[2:-4]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strFYear = Start_Date[-4:]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; intTYear = int(strFYear) + 2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strTYear = str(intTYear)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strFromDate = "'" + strFYear + "-" + strDay + "-" + strMonth + "'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strToDate = "'" + strTYear + "-" + strDay + "-" + strMonth + "'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strWhere = "qryELM.elmPK = tblELM.elmPK AND dbo.tblELM.LoggedDate &amp;gt;= " + strFromDate + " AND dbo.tblELM.LoggedDate &amp;lt;= DATEADD(year, 1," + strToDate + ") AND dbo.tblELM.EnquiryFK IN (4,22,25,61)"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; return strWhere &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Data type:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;String&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 24 Dec 2013 11:57:12 GMT</pubDate>
    <dc:creator>ChrisMatthews</dc:creator>
    <dc:date>2013-12-24T11:57:12Z</dc:date>
    <item>
      <title>Model Builder - Caluclate Value</title>
      <link>https://community.esri.com/t5/python-questions/model-builder-caluclate-value/m-p/747394#M57784</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello All&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm using the Calculate Value Tool within Model Builder to take a user variable (a date) to create a where clause to apply as the Expression on a Make Query Table. Please see below the values I've used for my Calculate Value Tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run the model I'm getting the following error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000539: Error running expression: x(str(Start_Date)) &amp;lt;type 'exceptions.NameError'&amp;gt;: name 'Start_Date' is not defined&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (CalcQueryWhere).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It looks to me that the Python script is not receiving the user variable as intended. Can any body help me with this? I really hope so, it would be very much appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Chris Mathtews&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;x("Start_Date")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code Block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;def x(Start_Date):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strDay = Start_Date[:2]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strMonth = Start_Date[2:-4]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strFYear = Start_Date[-4:]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; intTYear = int(strFYear) + 2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strTYear = str(intTYear)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strFromDate = "'" + strFYear + "-" + strDay + "-" + strMonth + "'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strToDate = "'" + strTYear + "-" + strDay + "-" + strMonth + "'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; strWhere = "qryELM.elmPK = tblELM.elmPK AND dbo.tblELM.LoggedDate &amp;gt;= " + strFromDate + " AND dbo.tblELM.LoggedDate &amp;lt;= DATEADD(year, 1," + strToDate + ") AND dbo.tblELM.EnquiryFK IN (4,22,25,61)"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; return strWhere &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Data type:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;String&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 24 Dec 2013 11:57:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/model-builder-caluclate-value/m-p/747394#M57784</guid>
      <dc:creator>ChrisMatthews</dc:creator>
      <dc:date>2013-12-24T11:57:12Z</dc:date>
    </item>
    <item>
      <title>Re: Model Builder - Caluclate Value</title>
      <link>https://community.esri.com/t5/python-questions/model-builder-caluclate-value/m-p/747395#M57785</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;ERROR 000539: Error running expression: x(str(Start_Date)) &amp;lt;type 'exceptions.NameError'&amp;gt;: name 'Start_Date' is not defined&lt;BR /&gt;Failed to execute (CalcQueryWhere).&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Try using:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;x(str(!Start_Date!))&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; ... instead. &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000004m000000"&gt;See the help information here&lt;/A&gt;&lt;SPAN&gt;. Make sure to specify that the calculation is using python, also.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 24 Dec 2013 13:00:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/model-builder-caluclate-value/m-p/747395#M57785</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2013-12-24T13:00:59Z</dc:date>
    </item>
    <item>
      <title>Re: Model Builder - Caluclate Value</title>
      <link>https://community.esri.com/t5/python-questions/model-builder-caluclate-value/m-p/747396#M57786</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello Chris,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Firstly, if you haven't already, make sure you are using proper indentation on your code book (although you probably did in your tool).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You also need to call the user variable in the expression. Use the % signs around the variable. In your case I think you'll use %Start_Date%. It's also a good idea to use str(%Start_Date%) to convert whatever the user provides to a string.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've also attached a screenshot if it helps.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let me know how it goes!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 24 Dec 2013 13:06:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/model-builder-caluclate-value/m-p/747396#M57786</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2013-12-24T13:06:12Z</dc:date>
    </item>
    <item>
      <title>Re: Model Builder - Caluclate Value</title>
      <link>https://community.esri.com/t5/python-questions/model-builder-caluclate-value/m-p/747397#M57787</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Many thanks Doug&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You didn't quite answer the right question, but you gave me enough to resolve the issue.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For anyone who reads this thread here is the link to the correct online help page.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A _jive_internal="true" href="https://community.esri.com/help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Calculate_Value/0017000000v8000000"&gt;//http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Calculate_Value/0017000000v8000000/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The solution to the problem was to enclose the parameter in %'s, whilst converting the variable to a string.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;x(str(%Start_Date%))&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 24 Dec 2013 13:36:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/model-builder-caluclate-value/m-p/747397#M57787</guid>
      <dc:creator>ChrisMatthews</dc:creator>
      <dc:date>2013-12-24T13:36:59Z</dc:date>
    </item>
    <item>
      <title>Re: Model Builder - Caluclate Value</title>
      <link>https://community.esri.com/t5/python-questions/model-builder-caluclate-value/m-p/747398#M57788</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;&lt;HR /&gt;[/HR]&lt;/SPAN&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hello Chris,&lt;BR /&gt;&lt;BR /&gt;Firstly, if you haven't already, make sure you are using proper indentation on your code book (although you probably did in your tool).&lt;BR /&gt;&lt;BR /&gt;You also need to call the user variable in the expression. Use the % signs around the variable. In your case I think you'll use %Start_Date%. It's also a good idea to use str(%Start_Date%) to convert whatever the user provides to a string.&lt;BR /&gt;&lt;BR /&gt;I've also attached a screenshot if it helps.&lt;BR /&gt;&lt;BR /&gt;Let me know how it goes!&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks Hua, you must have posted whilst I was posting. Your answer is what I've ended up doing.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 24 Dec 2013 13:39:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/model-builder-caluclate-value/m-p/747398#M57788</guid>
      <dc:creator>ChrisMatthews</dc:creator>
      <dc:date>2013-12-24T13:39:41Z</dc:date>
    </item>
  </channel>
</rss>

