<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.appian.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Converting Flattened Data to Nested</title><link>https://community.appian.com/discussions/f/new-to-appian/39073/converting-flattened-data-to-nested</link><description>I currently am doing an aggregate query that groups by two fields and sums a third field. The result is the following: 
 
 I am trying to pass this data into a grid but with one row per &amp;quot;week&amp;quot; value with an array of period / hour values as below: 
 
</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Converting Flattened Data to Nested</title><link>https://community.appian.com/thread/147878?ContentTypeID=1</link><pubDate>Wed, 07 May 2025 15:52:24 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:333f137e-5e60-46f7-b129-1c5f952dfb4d</guid><dc:creator>jonathanf_kr</dc:creator><description>&lt;p&gt;Replaced null on line 14 above with {} and it works as expected now.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Converting Flattened Data to Nested</title><link>https://community.appian.com/thread/147874?ContentTypeID=1</link><pubDate>Wed, 07 May 2025 15:03:00 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:143a85e1-9d78-4691-8694-91f9bd77d2ca</guid><dc:creator>jonathanf_kr</dc:creator><description>&lt;p&gt;I&amp;#39;ve gotten close with this code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!forEach(
    items: enumerate(4),
    expression: a!localVariables(
      local!week: fv!index,
      local!periods: a!forEach(
        items: local!events,
        expression: {
          if(
            fv!item.week = local!week,
            a!map(
              period: fv!item.period,
              hours: fv!item.hours
            ),
            null
          )
        }
      ),
      {
        a!map(week: fv!index, periods: local!periods)
      }
    )
  )&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;However its adding an entry for each null in the else condition:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;[week:1,periods:[period:1,hours:2]; [period:5,hours:3]; ; ; ; ]; [week:2,periods:; ; [period:2,hours:5]; [period:6,hours:8]; ; ]; [week:3,periods:; ; ; ; [period:12,hours:4]; ]; [week:4,periods:; ; ; ; ; [period:3,hours:2]]&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Converting Flattened Data to Nested</title><link>https://community.appian.com/thread/147873?ContentTypeID=1</link><pubDate>Wed, 07 May 2025 13:30:28 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:0c920e31-3736-425d-99e8-20f77b2122e7</guid><dc:creator>jonathanf_kr</dc:creator><description>&lt;p&gt;I am trying to use foreach in the following way with contains:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!forEach(
    items: local!events,
    expression: {
      if(
        or(
          a!isNullOrEmpty(local!result),
          not(contains(local!result[&amp;quot;week&amp;quot;], fv!item.week))
        ),
        append(local!result, a!map(week: fv!item.week, periods: { a!map(period: fv!item.period, hours: fv!item.total_hours)})),
        false
      )
    }
  )&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;However, it does not appear that the &amp;quot;contains&amp;quot; method is properly identifiying that a week record has already been added to my result.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>