I've noticed that some queries seem to be too complex within the SQL Expression Editor; previously I had issues running a calculation from a datefield that would not work on anything less than 6 months. Here's an example that I'm running into today:
I have a map that needs to be updated bi-weekly, it's based on the following query:
2 week range >= and >
a diff that calculates one business day turnaround
according to two complaint flags
OpenDate <SPAN class="operator token">>=</SPAN> <SPAN class="string token">'2020-07-27 00:00:00'</SPAN> <SPAN class="operator token">And</SPAN> OpenDate <SPAN class="operator token"><</SPAN> <SPAN class="string token">'2020-08-10 00:00:00'</SPAN>
<SPAN class="operator token">AND</SPAN> <SPAN class="punctuation token">(</SPAN>DATEDIFF<SPAN class="punctuation token">(</SPAN>Day<SPAN class="punctuation token">,</SPAN> OpenDate<SPAN class="punctuation token">,</SPAN> CaseInspectionDate<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">-</SPAN> DATEDIFF<SPAN class="punctuation token">(</SPAN>Week<SPAN class="punctuation token">,</SPAN> OpenDate<SPAN class="punctuation token">,</SPAN> CaseInspectionDate<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="operator token"><=</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">AND</SPAN> CaseReported <SPAN class="operator token">IN</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Citizen Complaint'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'City Council'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Then when I go to make a simple edit, changing the string for date:
OpenDate <SPAN class="operator token">>=</SPAN> <SPAN class="string token">'2020-08-10 00:00:00'</SPAN> <SPAN class="operator token">And</SPAN> OpenDate <SPAN class="operator token"><</SPAN> <SPAN class="string token">'2020-08-24 00:00:00'</SPAN>
<SPAN class="operator token">AND</SPAN> <SPAN class="punctuation token">(</SPAN>DATEDIFF<SPAN class="punctuation token">(</SPAN>Day<SPAN class="punctuation token">,</SPAN> OpenDate<SPAN class="punctuation token">,</SPAN> CaseInspectionDate<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">-</SPAN> DATEDIFF<SPAN class="punctuation token">(</SPAN>Week<SPAN class="punctuation token">,</SPAN> OpenDate<SPAN class="punctuation token">,</SPAN> CaseInspectionDate<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="operator token"><=</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">AND</SPAN> CaseReported <SPAN class="operator token">IN</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Citizen Complaint'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'City Council'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
It flips out like so:

Pretty horrible right?
Is this a bug?
Now if I were limited to the GUI that would be bad. So I went ahead and used the new mp module in ArcPy, because I'm in the process of automating this map anyway:
<SPAN class="keyword token">import</SPAN> arcpy
old_query01 <SPAN class="operator token">=</SPAN> <SPAN class="string token">"OpenDate >= '2020-07-27 00:00:00' And OpenDate < '2020-08-10 00:00:00' AND (DATEDIFF(Day, OpenDate, CaseInspectionDate) - DATEDIFF(Week, OpenDate, CaseInspectionDate) * 2 <= 1 ) AND CaseReported IN ('Citizen Complaint', 'City Council')"</SPAN>
old_query02 <SPAN class="operator token">=</SPAN> <SPAN class="string token">"OpenDate >= '2020-07-27 00:00:00' And OpenDate < '2020-08-10 00:00:00'"</SPAN>
old_query03 <SPAN class="operator token">=</SPAN> <SPAN class="string token">"OpenDate >= '2020-07-27 00:00:00' And OpenDate < '2020-08-10 00:00:00' AND CaseReported IN ('Citizen Complaint', 'City Council')"</SPAN>
new_query01 <SPAN class="operator token">=</SPAN> <SPAN class="string token">"OpenDate >= '2020-08-10 00:00:00' And OpenDate < '2020-08-24 00:00:00' AND (DATEDIFF(Day, OpenDate, CaseInspectionDate) - DATEDIFF(Week, OpenDate, CaseInspectionDate) * 2 <= 1 ) AND CaseReported IN ('Citizen Complaint', 'City Council')"</SPAN>
new_query02 <SPAN class="operator token">=</SPAN> <SPAN class="string token">"OpenDate >= '2020-08-10 00:00:00' And OpenDate < '2020-08-24 00:00:00'"</SPAN>
new_query03 <SPAN class="operator token">=</SPAN> <SPAN class="string token">"OpenDate >= '2020-08-10 00:00:00' And OpenDate < '2020-08-24 00:00:00' AND CaseReported IN ('Citizen Complaint', 'City Council')"</SPAN>
aprx <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>ArcGISProject<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"C:\Users\gisspellblade\Desktop\ArcProjects\SolidWaste_EnergovDemo.aprx"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> m <SPAN class="keyword token">in</SPAN> aprx<SPAN class="punctuation token">.</SPAN>listMaps<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"Map: {m} Layers"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> m<SPAN class="punctuation token">.</SPAN>listLayers<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"\t{lyr.name}"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>supports<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"DEFINITIONQUERY"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Complaint Response One Business Day:"</SPAN><SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>definitionQuery <SPAN class="operator token">=</SPAN> new_query01
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"Defintion changed for {lyr.name}: {lyr.definitionQuery}"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Cose Total: "</SPAN><SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>definitionQuery <SPAN class="operator token">=</SPAN> new_query02
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"Defintion changed for {lyr.name}: {lyr.definitionQuery}"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> <SPAN class="string token">"Complaint Total: "</SPAN><SPAN class="punctuation token">:</SPAN>
lyr<SPAN class="punctuation token">.</SPAN>definitionQuery <SPAN class="operator token">=</SPAN> new_query03
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>f<SPAN class="string token">"Defintion changed for {lyr.name}: {lyr.definitionQuery}"</SPAN><SPAN class="punctuation token">)</SPAN>
aprx<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Which at first blush, also did nothing.
I ran this several times with no resulting changes in the map.
I restarted the program and still nothing.
Then, I left the program closed for more than 10 minutes, and when I get back, I have these results:

So now I have three questions:
- If I'm changing the value on lyr.definitionQuery, then I'd think it would be an over-write and not an append? and if lyr.definitionQuery is a read/write; how does one access multiple queries to appropriately delete/add what they need?
- Why are these values not showing up immediately within the project? and why do I need a long cold restart to view changes made with ArcPy?
- this is an issue that I've seen before of needing to close a project for a ArcPy-derived result to appear in previous Pro versions
- What's with the GUI not accepting changes to complex queries, or refusing to write them in the first place?
In ArcGIS Pro 2.6
arcgispro2.6 arcpy #definition expressions #set definition #sql clause #sql query builder