<?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 Creating Data Expressions in ArcGIS Dashboards Questions</title>
    <link>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1151242#M5942</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I'm new to Arcade and even newer to creating Data Expressions in Dashboards.&amp;nbsp; I have three fields that I can populate using Arcade in my AGO web map Configure Pop-Ups section.&amp;nbsp; Unfortunately, as we all know, these type of calculated fields do not show up in Dashboards and Globals are not present.&amp;nbsp; I've been reading over past postings and it seems the key for me would be to employ FeatureSetByPortalItem() and create a data expression inside Dashboards so this data can be used.&amp;nbsp; The three fields are Temporal Block, Hour, and Weekday.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code I'm using in AGO for &lt;STRONG&gt;Temporal Block&lt;/STRONG&gt; is:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;var t = Hour($feature.PresentDate);&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;When(&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp; t &amp;gt;= 22 || t &amp;lt; 6, "Night (10pm - 6am)",&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp; t &amp;gt;= 6 &amp;amp;&amp;amp; t &amp;lt; 11, "Morning (6am - 11am)",&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp; t &amp;gt;= 11 &amp;amp;&amp;amp; t &amp;lt; 13, "Midday (11am - 1pm)",&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp; t &amp;gt;= 13 &amp;amp;&amp;amp; t &amp;lt; 17, "Afternoon (1pm - 5pm)",&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp; t &amp;gt;= 17 &amp;amp;&amp;amp; t &amp;lt; 22, "Evening (5pm - 10pm)",&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;"Invalid date" );&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The code I'm using in AGO for &lt;STRONG&gt;Hour&lt;/STRONG&gt; is:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Hour($feature.PresentDate)&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;And the code I am using in AGO for &lt;STRONG&gt;Weekday&lt;/STRONG&gt; is:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Weekday($feature.PresentDate)&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Can someone please help me or give me some hints at how to create an Arcade expression employing FeatureSetByPortalItem that will perform these calculations inside Dashboard?&lt;/P&gt;&lt;P&gt;Thank you so much in advance.&lt;/P&gt;&lt;P&gt;Jeff&lt;/P&gt;</description>
    <pubDate>Mon, 07 Mar 2022 13:57:51 GMT</pubDate>
    <dc:creator>JD1016</dc:creator>
    <dc:date>2022-03-07T13:57:51Z</dc:date>
    <item>
      <title>Creating Data Expressions</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1151242#M5942</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I'm new to Arcade and even newer to creating Data Expressions in Dashboards.&amp;nbsp; I have three fields that I can populate using Arcade in my AGO web map Configure Pop-Ups section.&amp;nbsp; Unfortunately, as we all know, these type of calculated fields do not show up in Dashboards and Globals are not present.&amp;nbsp; I've been reading over past postings and it seems the key for me would be to employ FeatureSetByPortalItem() and create a data expression inside Dashboards so this data can be used.&amp;nbsp; The three fields are Temporal Block, Hour, and Weekday.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code I'm using in AGO for &lt;STRONG&gt;Temporal Block&lt;/STRONG&gt; is:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;var t = Hour($feature.PresentDate);&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;When(&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp; t &amp;gt;= 22 || t &amp;lt; 6, "Night (10pm - 6am)",&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp; t &amp;gt;= 6 &amp;amp;&amp;amp; t &amp;lt; 11, "Morning (6am - 11am)",&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp; t &amp;gt;= 11 &amp;amp;&amp;amp; t &amp;lt; 13, "Midday (11am - 1pm)",&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp; t &amp;gt;= 13 &amp;amp;&amp;amp; t &amp;lt; 17, "Afternoon (1pm - 5pm)",&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp; t &amp;gt;= 17 &amp;amp;&amp;amp; t &amp;lt; 22, "Evening (5pm - 10pm)",&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;"Invalid date" );&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The code I'm using in AGO for &lt;STRONG&gt;Hour&lt;/STRONG&gt; is:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Hour($feature.PresentDate)&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;And the code I am using in AGO for &lt;STRONG&gt;Weekday&lt;/STRONG&gt; is:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Weekday($feature.PresentDate)&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Can someone please help me or give me some hints at how to create an Arcade expression employing FeatureSetByPortalItem that will perform these calculations inside Dashboard?&lt;/P&gt;&lt;P&gt;Thank you so much in advance.&lt;/P&gt;&lt;P&gt;Jeff&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 13:57:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1151242#M5942</guid>
      <dc:creator>JD1016</dc:creator>
      <dc:date>2022-03-07T13:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Data Expressions</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1151270#M5943</link>
      <description>&lt;P&gt;You'll want to check out the &lt;A href="https://github.com/Esri/arcade-expressions/tree/master/dashboard_data" target="_blank"&gt;Arcade GitHub repo&lt;/A&gt; for lots of examples.&lt;/P&gt;&lt;P&gt;If all you want is to get those three items into your Data Expression, that's easy enough. The basic things to know about data expressions are the &lt;STRONG&gt;FeatureSetByPortalItem&lt;/STRONG&gt; function to get your inputs, and then the basic structure of a FeatureSet's JSON definition so that you can build your own.&lt;/P&gt;&lt;P&gt;Also, just a side note: when you've got multiple conditions, they evaluate in order. So anything that fails to meet the "&amp;gt;= 22" condition will be less than 22 (or null) by definition, so you don't need to later use "&amp;lt; 22". Especially when you're using a numeric value, you can just evaluate in ascending or descending order.&lt;/P&gt;&lt;P&gt;And a side side note: since you're working with the hours field and one of your decoded strings spans the midnight mark, you can get that into a single condition by using the modulo (%) operator. Put another way, 23 % 22 = 1. So in the initial condition in that &lt;STRONG&gt;when &lt;/STRONG&gt;function, we can use that to reduce the &amp;gt;= 22 hours to a small number that will be caught in the same condition.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var portal = Portal('your portal url')

var fs = FeatureSetByPortalItem(
    portal,
    'itemID of your service',
    0, // or whatever the layer index is
    ['PresentDate', 'any', 'other', 'fields', 'you', 'want'],
    false // unless you really need geometry, but it performs much better without
)

// Create a dictionary to hold our output
var out_dict = {
    fields: [
        {name: 'PresentDate', type: 'esriFieldTypeDate'},
        {name: 'Weekday',     type: 'esriFieldTypeString'},
        {name: 'Hour',        type: 'esriFieldTypeInteger'},
        {name: 'TimeString',  type: 'esriFieldTypeString'}
        // include any other fields you want in the output here
    ],
    geometryType: '',
    features: []
}

// Iterate over features, calculate new fields, push to dictionary
for (var f in fs){
    var t = Hour(f['PresentDate'])
    
    var time_str = When(
        (t % 22) &amp;lt; 6, "Night (10pm - 6am)",
        t &amp;lt; 11, "Morning (6am - 11am)",
        t &amp;lt; 13, "Midday (11am - 1pm)",
        t &amp;lt; 17, "Afternoon (1pm - 5pm)",
        t &amp;lt; 22, "Evening (5pm - 10pm)",
        t &amp;lt; 24, "Night (10pm - 6am)",
        "Invalid date"
    )
    
    var wkday = Weekday(f['PresentDate'])
    
    Push(
        out_dict['features'],
        {
            attributes: {
                PresentDate: f['PresentDate'],
                Weekday: wkday,
                Hour: t,
                TimeString: time_str
            }
        }
    )
}

return FeatureSet(Text(out_dict))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It looks like a lot, but once you get the basic syntax down, it's not that bad.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 14:31:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1151270#M5943</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2022-03-07T14:31:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Data Expressions</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1151272#M5944</link>
      <description>&lt;P&gt;Thank you so much Josh!&amp;nbsp; I really appreciate your help.&lt;/P&gt;&lt;P&gt;Jeff&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 14:36:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1151272#M5944</guid>
      <dc:creator>JD1016</dc:creator>
      <dc:date>2022-03-07T14:36:48Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Data Expressions</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1151400#M5947</link>
      <description>&lt;P&gt;Hi Josh,&lt;/P&gt;&lt;P&gt;I think I might have spoke too soon.&amp;nbsp; I added the script and upon testing received the following (see Data Expression Test.png attached).&lt;/P&gt;&lt;P&gt;I'm assuming I should be seeing data below my field headings.&amp;nbsp; Correct?&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;Jeff&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 18:26:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1151400#M5947</guid>
      <dc:creator>JD1016</dc:creator>
      <dc:date>2022-03-07T18:26:47Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Data Expressions</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1151403#M5948</link>
      <description>&lt;P&gt;Oh! Right! I completely forgot to mention, but data expressions don't like "naked" date values, and it returns an empty FeatureSet when it encounters them. To get around this, we need to dress up the date as a number.&lt;/P&gt;&lt;P&gt;Try replacing line 44 of the above expression with this:&lt;/P&gt;&lt;PRE&gt;PresentDate: Number(f['PresentDate'])&lt;/PRE&gt;&lt;P&gt;That should get you what you need.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 18:29:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1151403#M5948</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2022-03-07T18:29:55Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Data Expressions</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1151407#M5949</link>
      <description>&lt;P&gt;That did it.&amp;nbsp; Thank you, Josh.&lt;/P&gt;&lt;P&gt;One quick additional related question...I would like to be able to add one more field, CrimeEntry, to the data available.&amp;nbsp; Am I understanding that I would need to add the CrimeEntry field after 'PresentDate' following FeatureSetByPortalItem, add it as a field in the &lt;STRONG&gt;out_dict&lt;/STRONG&gt;&amp;nbsp;&lt;STRONG&gt;fields section&lt;/STRONG&gt;, call it out as a var for the &lt;STRONG&gt;iterate over features section&lt;/STRONG&gt;, and then list it within &lt;STRONG&gt;out_dict&lt;/STRONG&gt; &lt;STRONG&gt;for attributes section&lt;/STRONG&gt; to see the corresponding crimes reported for that particular date?&lt;/P&gt;&lt;P&gt;Hope that makes sense...novice here...terminology escapes.&lt;/P&gt;&lt;P&gt;Jeff&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 18:38:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1151407#M5949</guid>
      <dc:creator>JD1016</dc:creator>
      <dc:date>2022-03-07T18:38:52Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Data Expressions</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1152099#M5953</link>
      <description>&lt;P&gt;First, I'd like to thank Josh Carlson for the big assist and the Community Forum in general for being such a great resource to GIS professionals. Truly outstanding. Secondly, I just wanted to post a follow-up to my last question.&amp;nbsp; I was able to add CrimeEntry to the original code which allowed me to associate temporal data with the particular crime reported at that given time and day.&amp;nbsp; The following is the modified data expression to achieve this result:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;//Set which data I want to use with what fields I want as part of my output
var portal = Portal('your portal')
var fs = FeatureSetByPortalItem(
    portal,
    'your itemID',
    0, ['PresentDate', 'CrimeEntry'],
    false // unless you really need geometry, but it performs much better without
);
// Create an empty dictionary to hold our output
var out_dict = {
    fields: [
        {name: 'PresentDate', type:  'esriFieldTypeDate'},
        {name: 'CrimeEntry',  type:  'esriFieldTypeString'},
        {name: 'Weekday',     type:  'esriFieldTypeInteger'},
        {name: 'Hour',        type:  'esriFieldTypeInteger'},
        {name: 'TimeString',  type:  'esriFieldTypeString'}
        // include any other fields you want in the output here
    ],
    geometryType: '',
    features: []
}

// Iterate over features, calculate new fields, push to dictionary
for (var f in fs){
    var t = Hour(f['PresentDate'])
    
    
    var time_str = When(
        (t % 22) &amp;lt; 6, "Night (10pm - 6am)",
        t &amp;lt; 11, "Morning (6am - 11am)",
        t &amp;lt; 13, "Midday (11am - 1pm)",
        t &amp;lt; 17, "Afternoon (1pm - 5pm)",
        t &amp;lt; 22, "Evening (5pm - 10pm)",
        t &amp;lt; 24, "Night (10pm - 6am)",
        "Invalid date"
    )
    
    var wkday = Weekday(f['PresentDate'])
    
    
    Push(
        out_dict['features'],
        {
            attributes: {
                PresentDate: Number(f['PresentDate']),
                CrimeEntry: (f['CrimeEntry']),
                Weekday: wkday,
                Hour: t,
                TimeString: time_str
                
            }
        }
    )
}

return FeatureSet(Text(out_dict))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2022 12:53:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/creating-data-expressions/m-p/1152099#M5953</guid>
      <dc:creator>JD1016</dc:creator>
      <dc:date>2022-03-09T12:53:21Z</dc:date>
    </item>
  </channel>
</rss>

