<?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 Re: Use a data expression to create a new field based on values in another field for use in a pie chart in ArcGIS Dashboards Questions</title>
    <link>https://community.esri.com/t5/arcgis-dashboards-questions/use-a-data-expression-to-create-a-new-field-based/m-p/1666313#M11642</link>
    <description>&lt;P&gt;Also, very good input from you as well Ken!&amp;nbsp; Thank you for sharing your knowledge and experience.&lt;/P&gt;</description>
    <pubDate>Mon, 17 Nov 2025 16:11:55 GMT</pubDate>
    <dc:creator>Bec</dc:creator>
    <dc:date>2025-11-17T16:11:55Z</dc:date>
    <item>
      <title>Use a data expression to create a new field based on values in another field for use in a pie chart</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/use-a-data-expression-to-create-a-new-field-based/m-p/1666240#M11632</link>
      <description>&lt;P&gt;Hi!&amp;nbsp; I'm trying to build a data expression in an ArcGIS Online dashboard that will take a field called, HOUR_OF_CRASH, which is an integer field and not a time field, and populate a new field called, TOD, which will be a text field for the time of day the hour falls within so that the new field can be used to build a pie chart within the dashboard.&amp;nbsp; The current data expression is shown below.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;var portal = Portal("https://www.arcgis.com");

var fs = FeatureSetByPortalItem('d651aa4112e5408ea9a1cfabecd6485c', 23)
// Create schema for the returned FeatureSet
var fields = [
    { name: "HOUR_OF_CRASH", type: "esriFieldTypeInteger" },
    { name: "TOD", type: "esriFieldTypeString" }
];

var features = [];

for (var f in fs) {
    var hour = f.HOUR_OF_CRASH;
    var tod;

    if (hour &amp;gt;= 0 &amp;amp;&amp;amp; hour &amp;lt;= 5){
        tod = "Night";
    } else if (hour &amp;gt;= 6 &amp;amp;&amp;amp; hour &amp;lt;= 8){
        tod = "Morning";
    } else if (hour &amp;gt;= 9 &amp;amp;&amp;amp; hour &amp;lt;= 15){
        tod = "Day";
    } else if (hour &amp;gt;= 16 &amp;amp;&amp;amp; hour &amp;lt;= 23){
        tod = "Evening";
    } else {
        tod = null;
    }

    Push(features, {
        attributes: {
            HOUR_OF_CRASH: hour,
            TOD: tod
        }
    });
}&lt;/LI-CODE&gt;&lt;P&gt;Unfortunately, this data expression won't function within the dashboard and doesn't give me quite the output I am looking for in the end.&amp;nbsp; See the below image of the output.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TOD Data Expression Output.png" style="width: 765px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/143868i257C64107A621D4D/image-size/large?v=v2&amp;amp;px=999" role="button" title="TOD Data Expression Output.png" alt="TOD Data Expression Output.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;My experience with data expression is relatively limited.&amp;nbsp; Any advice would be greatly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Bec&lt;/P&gt;</description>
      <pubDate>Mon, 17 Nov 2025 13:32:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/use-a-data-expression-to-create-a-new-field-based/m-p/1666240#M11632</guid>
      <dc:creator>Bec</dc:creator>
      <dc:date>2025-11-17T13:32:46Z</dc:date>
    </item>
    <item>
      <title>Re: Use a data expression to create a new field based on values in another field for use in a pie chart</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/use-a-data-expression-to-create-a-new-field-based/m-p/1666245#M11633</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/546831"&gt;@Bec&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;So the issue is that you are not returning a feature. When creating a custom data expression you should return a value similar to the one below.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var portal = Portal("https://www.arcgis.com");

var fs = FeatureSetByPortalItem('d651aa4112e5408ea9a1cfabecd6485c', 23)
// Create schema for the returned FeatureSet
var fields = [
    { name: "HOUR_OF_CRASH", type: "esriFieldTypeInteger" },
    { name: "TOD", type: "esriFieldTypeString" }
];

var features = []; 
for (var f in fs) {
    var hour = f.HOUR_OF_CRASH;
    var tod = When( hour &amp;gt;= 0 &amp;amp;&amp;amp; hour &amp;lt;= 5, "Night", hour &amp;gt;= 6 &amp;amp;&amp;amp; hour &amp;lt;= 8, "Morning", hour &amp;gt;= 9 &amp;amp;&amp;amp; hour &amp;lt;= 15, "Day", 16 &amp;amp;&amp;amp; hour &amp;lt;= 23, "Evening", Null )
	Push( features, { attributes: { HOUR_OF_CRASH: hour, TOD: tod } })
	}
return FeatureSet( fields, Values, geometry:'' )&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 17 Nov 2025 15:37:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/use-a-data-expression-to-create-a-new-field-based/m-p/1666245#M11633</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2025-11-17T15:37:02Z</dc:date>
    </item>
    <item>
      <title>Re: Use a data expression to create a new field based on values in another field for use in a pie chart</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/use-a-data-expression-to-create-a-new-field-based/m-p/1666266#M11634</link>
      <description>&lt;P&gt;Also note that you can make the When statement more concise&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var tod = When(hour &amp;gt;= 0 &amp;amp;&amp;amp; hour &amp;lt;= 5, "Night",
               hour &amp;lt;= 8, "Morning",
               hour &amp;lt;= 15, "Day",
               hour &amp;lt;= 23, "Evening",
               Null
);&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 17 Nov 2025 14:38:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/use-a-data-expression-to-create-a-new-field-based/m-p/1666266#M11634</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2025-11-17T14:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: Use a data expression to create a new field based on values in another field for use in a pie chart</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/use-a-data-expression-to-create-a-new-field-based/m-p/1666291#M11638</link>
      <description>&lt;P&gt;You could go a more programmatic route.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var fs = FeatureSetByPortalItem('d651aa4112e5408ea9a1cfabecd6485c', 23)
// Create schema for the returned FeatureSet
var HRC = "HOUR_OF_CRASH" ; var TODn = "TOD"
var FDict = Dictionary( HRC, "esriFieldTypeInteger", TODn, "esriFieldTypeString" )
var fields = [
    { name: HRC, type: FDict[HRC] },
    { name: TODn, type: FDict[TODn] }
];

var features = [];
for (var f in fs) {
    FDict[HRC] = f.[HRC]
    FDict[TODn] = When( hour &amp;gt;= 0 &amp;amp;&amp;amp; hour &amp;lt;= 5, "Night", hour &amp;lt;= 8, "Morning", hour &amp;lt;= 15, "Day", hour &amp;lt;= 23, "Evening", Null )
 	Push( features, { attributes: FDict })
 	}
return FeatureSet( fields:fields, features:features, geometry:'' )&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 17 Nov 2025 15:45:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/use-a-data-expression-to-create-a-new-field-based/m-p/1666291#M11638</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2025-11-17T15:45:09Z</dc:date>
    </item>
    <item>
      <title>Re: Use a data expression to create a new field based on values in another field for use in a pie chart</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/use-a-data-expression-to-create-a-new-field-based/m-p/1666311#M11641</link>
      <description>&lt;P&gt;Yes,&amp;nbsp;&lt;SPAN&gt;&lt;STRONG&gt;RPGIS&lt;/STRONG&gt;,&lt;/SPAN&gt; your input about returning a FeatureSet was valuable in helping me get to my final output.&amp;nbsp; I made a few more adjustments to the final data expression and what you see below is the working version along with an image of the output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;/// Connect to portal layer
var portal = Portal("https://www.arcgis.com");
var fs = FeatureSetByPortalItem(
    portal,
    "d651aa4112e5408ea9a1cfabecd6485c",
    23
);
// Portal layer is a proxy to REST Service layer called CoC Crash Data
// If REST Service layer attributes change may affect data expression

// --- Define Output Schema ---
var fields = [
    { name: "HOUR_OF_CRASH", type: "esriFieldTypeInteger" },
    { name: "TOD", type: "esriFieldTypeString" },
    { name: "SEVERITY_BY_TYPE_CD", type: "esriFieldTypeString" },
    { name: "CRASH_TYPE_CD", type: "esriFieldTypeString" },
    { name: "DAY_IN_WEEK_CD", type: "esriFieldTypeString" },
    { name: "Month", type: "esriFieldTypeDouble" },
    { name: "HIN", type: "esriFieldTypeSmallInteger" },
    { name: "ODPS_SPEED_IND", type: "esriFieldTypeString" },
    { name: "FUNCTIONAL_CLASS_CD", type: "esriFieldTypeString" },
    { name: "ODOT_LANES_NBR", type: "esriFieldTypeDouble" },
    { name: "ODOT_YOUNG_DRIVER_IND", type: "esriFieldTypeString" },
    { name: "ODPS_SENIOR_DRIVER_IND", type: "esriFieldTypeString" },
    { name: "DISTRACTED_DRIVER_IND", type: "esriFieldTypeString" },
    { name: "WEATHER_COND_CD", type: "esriFieldTypeString" },
    { name: "LIGHT_COND_PRIMARY_CD", type: "esriFieldTypeString" },
    { name: "ROAD_COND_PRIMARY_CD", type: "esriFieldTypeString" },
    { name: "ODOT_CRASH_LOCATION_CD", type: "esriFieldTypeString" }
];

var features = [];

// --- Build Output Records ---
for (var f in fs) {

    var hour = f.HOUR_OF_CRASH;

    var tod = When(
        hour &amp;gt;= 0 &amp;amp;&amp;amp; hour &amp;lt;= 5, "Night",
        hour &amp;gt;= 6 &amp;amp;&amp;amp; hour &amp;lt;= 8, "Morning",
        hour &amp;gt;= 9 &amp;amp;&amp;amp; hour &amp;lt;= 15, "Day",
        hour &amp;gt;= 16 &amp;amp;&amp;amp; hour &amp;lt;= 23, "Evening",
        null
    );

    Push(features, {
        attributes: {
            HOUR_OF_CRASH: hour,
            TOD: tod,
            SEVERITY_BY_TYPE_CD: f.SEVERITY_BY_TYPE_CD,
            CRASH_TYPE_CD: f.CRASH_TYPE_CD,
            DAY_IN_WEEK_CD: f.DAY_IN_WEEK_CD,
            Month: f.Month,
            HIN: f.HIN,
            ODPS_SPEED_IND: f.ODPS_SPEED_IND,
            FUNCTIONAL_CLASS_CD: f.FUNCTIONAL_CLASS_CD,
            ODOT_LANES_NBR: f.ODOT_LANES_NBR,
            ODOT_YOUNG_DRIVER_IND: f.ODOT_YOUNG_DRIVER_IND,
            ODPS_SENIOR_DRIVER_IND: f.ODPS_SENIOR_DRIVER_IND,
            DISTRACTED_DRIVER_IND: f.DISTRACTED_DRIVER_IND,
            WEATHER_COND_CD: f.WEATHER_COND_CD,
            LIGHT_COND_PRIMARY_CD: f.LIGHT_COND_PRIMARY_CD,
            ROAD_COND_PRIMARY_CD: f.ROAD_COND_PRIMARY_CD,
            ODOT_CRASH_LOCATION_CD: f.ODOT_CRASH_LOCATION_CD
        }
    });
}

// --- Return FeatureSet (non-spatial) ---
return FeatureSet({
    fields: fields,
    features: features
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TOD Working Data Expression Output.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/143880i2F2C6251D60E7F8B/image-size/large?v=v2&amp;amp;px=999" role="button" title="TOD Working Data Expression Output.png" alt="TOD Working Data Expression Output.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Nov 2025 16:09:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/use-a-data-expression-to-create-a-new-field-based/m-p/1666311#M11641</guid>
      <dc:creator>Bec</dc:creator>
      <dc:date>2025-11-17T16:09:45Z</dc:date>
    </item>
    <item>
      <title>Re: Use a data expression to create a new field based on values in another field for use in a pie chart</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/use-a-data-expression-to-create-a-new-field-based/m-p/1666313#M11642</link>
      <description>&lt;P&gt;Also, very good input from you as well Ken!&amp;nbsp; Thank you for sharing your knowledge and experience.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Nov 2025 16:11:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/use-a-data-expression-to-create-a-new-field-based/m-p/1666313#M11642</guid>
      <dc:creator>Bec</dc:creator>
      <dc:date>2025-11-17T16:11:55Z</dc:date>
    </item>
  </channel>
</rss>

