<?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>How to merge lines and calculate average data on a CDT</title><link>https://community.appian.com/discussions/f/data/21744/how-to-merge-lines-and-calculate-average-data-on-a-cdt</link><description>Hi, 
 I have a CDT that contains many lines. How would you construct a new CDT (new local variable) with merged lines (with average calculations) depending of a field values ? (like the picture below) 
 Naively, I would use a foreach and create some lists</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: How to merge lines and calculate average data on a CDT</title><link>https://community.appian.com/thread/85366?ContentTypeID=1</link><pubDate>Wed, 01 Sep 2021 21:45:11 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:a143ce15-b75b-4f07-a31d-6f3f028e3b22</guid><dc:creator>cedric02</dc:creator><description>&lt;p&gt;Thanks a lot&amp;nbsp;&lt;a class="internal-link view-user-profile" href="/members/siddharthm0003"&gt;Siddharth&lt;/a&gt;, I&amp;#39;ve missed this detail...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to merge lines and calculate average data on a CDT</title><link>https://community.appian.com/thread/85364?ContentTypeID=1</link><pubDate>Wed, 01 Sep 2021 19:18:07 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:5bf77561-b542-4292-9129-20e3bfcdfc45</guid><dc:creator>Siddharth</dc:creator><description>&lt;p&gt;This is because you are not filtering out the data properly. The index operation in line 22 should be used on &lt;em&gt;local!subsetData&amp;nbsp;&lt;/em&gt;and not &lt;em&gt;local!vehicles.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;pre class="ui-code" data-mode="java"&gt;index(
  local!subsetData,
  wherecontains(
    tointeger(local!parent1),
    tointeger(local!subsetData.subtypeid)
  ),
  {}
)&lt;/pre&gt;&lt;/em&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to merge lines and calculate average data on a CDT</title><link>https://community.appian.com/thread/85354?ContentTypeID=1</link><pubDate>Wed, 01 Sep 2021 15:07:30 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:cd5255c4-f58d-49b3-b9d4-8c77c6d2b5e3</guid><dc:creator>cedric02</dc:creator><description>&lt;p&gt;no idea for my last example ?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to merge lines and calculate average data on a CDT</title><link>https://community.appian.com/thread/85322?ContentTypeID=1</link><pubDate>Wed, 01 Sep 2021 08:14:26 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:67a922c3-bd39-4441-bd51-c9a232691dbf</guid><dc:creator>cedric02</dc:creator><description>&lt;p&gt;Thank you Siddharth,&lt;/p&gt;
&lt;p&gt;Sorry, the index is not a problem, and I finally need more to get the id (or list of ids) than the index.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve created another example with an addional level (I have now : Type, SubType ans SubType2).&lt;/p&gt;
&lt;p&gt;All seems to work well but I have met a special case.&lt;/p&gt;
&lt;p&gt;Here is the example and code below.&lt;/p&gt;
&lt;p&gt;Could you tell me why I am obtaining this 5th line please ? (here is my code below)&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="/resized-image/__size/320x240/__key/communityserver-discussions-components-files/16/5125.Average-on-CDT3.JPG" /&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;  local!aggregateValues: a!flatten(
    a!forEach(
      items: local!uniqueTypesParent,
      expression: a!localVariables(
        local!parent: fv!item,
        local!subsetData: index(
          local!vehicles,
          wherecontains(
            tointeger(local!parent),
            tointeger(local!vehicles.typeid)
          ),
          {}
        ),
        local!uniqueSubTypesChild: union(
          local!subsetData.subtypeid,
          local!subsetData.subtypeid
        ),
        a!forEach(
          items: local!uniqueSubTypesChild,
          expression: a!localVariables(
            local!parent1: fv!item,
            local!subsetData1: index(
              local!vehicles,
              wherecontains(
                tointeger(local!parent1),
                tointeger(local!vehicles.subtypeid)
              ),
              {}
            ),
            local!uniqueSubTypesChild2: union(
              local!subsetData1.subtypeid2,
              local!subsetData1.subtypeid2
            ),
            a!forEach(
              items: local!uniqueSubTypesChild2,
              expression: a!map(
                id: a!localVariables(
                  local!listeTypes: index(
                    local!vehicles,
                    wherecontains(local!parent, local!vehicles.typeid)
                  ),
                  local!listeSubTypes: index(
                    local!listeTypes,
                    wherecontains(local!parent1, local!listeTypes.subtypeid)
                  ),
                  local!listeSubTypes2: index(
                    local!listeSubTypes,
                    wherecontains(fv!item, local!listeSubTypes.subtypeid2)
                  ),
                  local!listeSubTypes2.id
                ),
                typeid: local!parent,
                subtypeid: local!parent1,
                subtypeid2: fv!item,
                value: sum(
                  index(
                    local!subsetData.value,
                    wherecontains(
                      tointeger(fv!item),
                      tointeger(local!subsetData.subtypeid2)
                    ),
                    {}
                  )
                )
              )
            )
          )
        )
      )
    )
  ), &lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="/resized-image/__size/320x240/__key/communityserver-discussions-components-files/16/8738.Average-on-CDT3-_2D00_-local-value.JPG" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to merge lines and calculate average data on a CDT</title><link>https://community.appian.com/thread/85256?ContentTypeID=1</link><pubDate>Tue, 31 Aug 2021 16:21:28 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:68cefdd8-9609-41c9-a93e-53cd3b37d100</guid><dc:creator>Siddharth</dc:creator><description>&lt;p&gt;It does generate the&amp;nbsp;correct index values (the a!update at line 51 has been used for generating the correct &amp;#39;id&amp;#39; values).&amp;nbsp;Can you please double check.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="/resized-image/__size/320x240/__key/communityserver-discussions-components-files/16/pastedimage1630426781143v1.png" alt=" " /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to merge lines and calculate average data on a CDT</title><link>https://community.appian.com/thread/85232?ContentTypeID=1</link><pubDate>Tue, 31 Aug 2021 10:10:36 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:174766bc-af21-4f4b-8905-5a3c67319a58</guid><dc:creator>cedric02</dc:creator><description>&lt;p&gt;Thanks a lot&amp;nbsp;&lt;a class="internal-link view-user-profile" href="/members/siddharthm0003"&gt;Siddharth&lt;/a&gt;, but it seems the index is incorrect.&lt;/p&gt;
&lt;p&gt;Your code generates : 1,2,1,2,1,2&amp;nbsp; (but 1,2,3,4,5,6 expected)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to merge lines and calculate average data on a CDT</title><link>https://community.appian.com/thread/85185?ContentTypeID=1</link><pubDate>Tue, 31 Aug 2021 00:04:34 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:56a35d87-79d8-4084-b19b-62df97f569fa</guid><dc:creator>Siddharth</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="java"&gt;a!localVariables(
  local!myData: {
    a!map(id: 1, type1: 1, type2: 12, value: 10),
    a!map(id: 2, type1: 1, type2: 12, value: 15),
    a!map(id: 3, type1: 1, type2: 25, value: 20),
    a!map(id: 4, type1: 1, type2: 12, value: 25),
    a!map(id: 5, type1: 2, type2: 30, value: 5),
    a!map(id: 6, type1: 2, type2: 30, value: 15),
    a!map(id: 7, type1: 2, type2: 12, value: 15),
    a!map(id: 8, type1: 2, type2: 12, value: 22)
  },
  local!uniqueTypesParent: union(local!myData.type1, local!myData.type1),
  local!aggregateValues: a!flatten(
    a!forEach(
      items: local!uniqueTypesParent,
      expression: a!localVariables(
        local!parent: fv!item,
        local!subsetData: index(
          local!myData,
          wherecontains(
            tointeger(local!parent),
            tointeger(local!myData.type1)
          ),
          {}
        ),
        local!uniqueTypesChild: union(
          local!subsetData.type2,
          local!subsetData.type2
        ),
        a!forEach(
          items: local!uniqueTypesChild,
          expression: a!map(
            id: fv!index,
            type1: local!parent,
            type2: fv!item,
            value: sum(
              index(
                local!subsetData.value,
                wherecontains(
                  tointeger(fv!item),
                  tointeger(local!subsetData.type2)
                ),
                {}
              )
            )
          )
        )
      )
    )
  ),
  a!update(
    data: local!aggregateValues,
    index: &amp;quot;id&amp;quot;,
    value: 1 + enumerate(length(local!aggregateValues))
  )
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to merge lines and calculate average data on a CDT</title><link>https://community.appian.com/thread/85180?ContentTypeID=1</link><pubDate>Mon, 30 Aug 2021 21:08:55 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:a80b5543-a9a7-406a-a808-5899e2694696</guid><dc:creator>cedric02</dc:creator><description>&lt;p&gt;Danny, this time how would you do the Qty sum on 2 disctinct field values (Type1 et Type2) ?&amp;nbsp; (without any SQL of course)&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="/resized-image/__size/320x240/__key/communityserver-discussions-components-files/16/4812.Average-on-CDT2.JPG" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to merge lines and calculate average data on a CDT</title><link>https://community.appian.com/thread/85082?ContentTypeID=1</link><pubDate>Sat, 28 Aug 2021 08:58:58 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:405c0937-872f-4cdb-be91-19cdf85bd882</guid><dc:creator>cedric02</dc:creator><description>&lt;p&gt;Ok I understand, thank you for the explanation.&lt;/p&gt;
&lt;p&gt;But your previous code will be perfect for what I need.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to merge lines and calculate average data on a CDT</title><link>https://community.appian.com/thread/85077?ContentTypeID=1</link><pubDate>Fri, 27 Aug 2021 16:25:14 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:73fd4ca9-5607-4937-9d2f-c5ee3a6ee319</guid><dc:creator>Danny Verb</dc:creator><description>&lt;p&gt;If your data is already in SAIL, then no there&amp;#39;s no way to group and aggregate in the way you want. In the case I placed above, you&amp;#39;re only looping through the unique values in your list and using the native sum() or average() functions on the corresponding values.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If your data is in a database table then you can easily use a!queryEntity or a!queryRecordType to aggregate the data on query.&lt;/p&gt;
&lt;p&gt;How is your data getting into your SAIL interface? If it&amp;#39;s from a query, then simply update that query to use a queryAggregation&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to merge lines and calculate average data on a CDT</title><link>https://community.appian.com/thread/85067?ContentTypeID=1</link><pubDate>Fri, 27 Aug 2021 14:06:51 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:a61fdac3-a2c6-4cd7-b5ca-4d6489267261</guid><dc:creator>cedric02</dc:creator><description>&lt;p&gt;Thanks a lot Danny. I would probably have coded almost the same thing in 3 times more times... ;-)&lt;/p&gt;
&lt;p&gt;but is there no other&amp;nbsp;way to do it without&amp;nbsp;a foreach ? with a native Appian function for example...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to merge lines and calculate average data on a CDT</title><link>https://community.appian.com/thread/85059?ContentTypeID=1</link><pubDate>Fri, 27 Aug 2021 13:22:06 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:95c48825-2220-43df-b182-ab6ce61ccebc</guid><dc:creator>Danny Verb</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="java"&gt;a!localVariables(
  local!myData: {
    a!map(
      id: 1,
      typeId: 1,
      value: 10
    ),
    a!map(
      id: 2,
      typeId: 1,
      value: 15
    ),
    a!map(
      id: 3,
      typeId: 2,
      value: 5
    ),
    a!map(
      id: 4,
      typeId: 2,
      value: 22
    )
  },
  local!uniqueTypes: union(local!myData.typeId,local!myData.typeId),
  local!aggregateValues: a!forEach(
    local!uniqueTypes,
    a!map(
      id: fv!index,
      typeId: fv!item,
      value: sum(index(
        local!myData.value,
        wherecontains(tointeger(fv!item),tointeger(local!myData.typeId)),
        {}
      ))
    )
  ),
  local!aggregateValues
)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The above code should work&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>