<?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: Creating average by unique countries in QlikView</title>
    <link>https://community.qlik.com/t5/QlikView/Creating-average-by-unique-countries/m-p/989073#M337129</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Try this: avg(aggr(if(rank(max(Score))&amp;lt;4,max(Score)),Country,HQ))&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 02 Nov 2015 16:31:20 GMT</pubDate>
    <dc:creator>Gysbert_Wassenaar</dc:creator>
    <dc:date>2015-11-02T16:31:20Z</dc:date>
    <item>
      <title>Creating average by unique countries</title>
      <link>https://community.qlik.com/t5/QlikView/Creating-average-by-unique-countries/m-p/989072#M337128</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;I want to create the average score of an HQ, based on its top 3 highest scoring subsidiaries. There is one condition: the top 3 has to be composed of unique countries, i.e. the average cannot contain more than 1 subsidiary based in the same country. If a such a case occurs, we will take the highest score into account. See the example below.&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;/P&gt;&lt;P&gt;Here is the source data:&lt;/P&gt;&lt;TABLE border="1" class="jiveBorder" height="201" style="border: 1px solid #000000; width: 281px; height: 202px;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;HQ&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;Subsidiary&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;Country&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;Score&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;HQ_1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;Company_A&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;FRA&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;HQ_1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;Company_B&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;ITA&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;HQ_1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;Company_C&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;FRA&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;HQ_1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;Company_D&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;GER&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;HQ_2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;Company_E&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;ESP&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;7&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;HQ_2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;Company_F&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;ITA&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;HQ_2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;Company_G&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;GER&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;HQ_2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;Company_H&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;ESP&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The resulting table would look like this:&lt;/P&gt;&lt;TABLE border="1" class="jiveBorder" height="100" style="border: 1px solid #000000; width: 265px; height: 91px;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;Rank&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;HQ&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;Average Score&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;1.&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;HQ_2&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;4.67&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;2.&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;HQ_1&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;4.33&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Notice that for HQ_1 we had to take the average of companies A, B and D, because A and C are both from France and A has the highest score. Therefore C was omitted. H was omitted because it is not in the top 3. (note: we don't have to deal with the dilemma of an HQ having subsidiaries from only 1 country)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have the following piece of code, that does the job of picking the top 3 of any HQ, but I cannot get it to exclude a duplicate country.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;avg(&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; aggr(num(rank(sum(Score),1)), HQ, Subsidiary) &amp;lt; 4,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum(Score)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; )&lt;/P&gt;&lt;P&gt;)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in advance for your help! &lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Gabor&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2015 13:17:37 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Creating-average-by-unique-countries/m-p/989072#M337128</guid>
      <dc:creator>gabormajdan</dc:creator>
      <dc:date>2015-10-29T13:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: Creating average by unique countries</title>
      <link>https://community.qlik.com/t5/QlikView/Creating-average-by-unique-countries/m-p/989073#M337129</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Try this: avg(aggr(if(rank(max(Score))&amp;lt;4,max(Score)),Country,HQ))&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Nov 2015 16:31:20 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Creating-average-by-unique-countries/m-p/989073#M337129</guid>
      <dc:creator>Gysbert_Wassenaar</dc:creator>
      <dc:date>2015-11-02T16:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: Creating average by unique countries</title>
      <link>https://community.qlik.com/t5/QlikView/Creating-average-by-unique-countries/m-p/989074#M337130</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I would use&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;=avg(aggr(if(rank(max(Score))&amp;lt;4,max(Score)),HQ, Country))&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 02 Nov 2015 16:53:49 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Creating-average-by-unique-countries/m-p/989074#M337130</guid>
      <dc:creator>swuehl</dc:creator>
      <dc:date>2015-11-02T16:53:49Z</dc:date>
    </item>
    <item>
      <title>Re: Creating average by unique countries</title>
      <link>https://community.qlik.com/t5/QlikView/Creating-average-by-unique-countries/m-p/989075#M337131</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks! I had to tweak it a bit myself, because I gave a simplified version of the problem, but overall this was the right solution (in reality the Score is a lengthy calculation so I had to embed another aggr in the max, over Country, HQ and Company fields). &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Something like this:&lt;/P&gt;&lt;P&gt;avg(aggr(if(rank(max(aggr(Score,Country,HQ,Company)),&amp;lt;4,max(aggr(Score,Country,HQ,Company))),Country,HQ))&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Nov 2015 09:36:22 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Creating-average-by-unique-countries/m-p/989075#M337131</guid>
      <dc:creator>gabormajdan</dc:creator>
      <dc:date>2015-11-04T09:36:22Z</dc:date>
    </item>
  </channel>
</rss>

