<?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 First Sorted value not working when calculating a measure in App Development</title>
    <link>https://community.qlik.com/t5/App-Development/First-Sorted-value-not-working-when-calculating-a-measure/m-p/2483073#M100987</link>
    <description>&lt;P&gt;Evening all,&lt;/P&gt;
&lt;P&gt;Once again I need to reach out to the community to produce a chart. I would appreciate your assistance.&lt;/P&gt;
&lt;P&gt;I have a header/Detail record structure. What is unusual is that when you add up the values in the detail it does not always equal the value stored in the header. If there are say 4 items in the detail, I want to only show the header value against the 1st item in the detail and show the header value as 0 for the rest as per the table below:&lt;/P&gt;
&lt;TABLE width="403"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="104"&gt;Header&lt;/TD&gt;
&lt;TD width="79"&gt;Detail Key&lt;/TD&gt;
&lt;TD width="114"&gt;Header Amount&lt;/TD&gt;
&lt;TD width="106"&gt;Detail Amount&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;600&lt;/TD&gt;
&lt;TD&gt;196.17&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A2&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;24.57&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A3&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;173.87&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A4&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;97.14&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To achieve this, I am using the FirstSortedValue function to return A1 and use an if statement to compare the FirstSortedVale and DetailKey. If they are equal, Sum the Header Value else set it to 0. When using the formulae I get the following table. I have included the first sorted value as a dimension and split the two conditions being compared in the measures to demonstrate what is happening.&lt;/P&gt;
&lt;TABLE width="732"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD colspan="3" width="297"&gt;&amp;lt;--------------------Dimensions-----------------------&amp;gt;&lt;/TD&gt;
&lt;TD colspan="3" width="338"&gt;&amp;lt;----------------------------------------Measures---------------------------&amp;gt;&lt;/TD&gt;
&lt;TD width="97"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Header&lt;/TD&gt;
&lt;TD&gt;Detail Key&lt;/TD&gt;
&lt;TD&gt;First Sorted Value&lt;/TD&gt;
&lt;TD&gt;First Sorted Value&lt;/TD&gt;
&lt;TD&gt;Detail key&lt;/TD&gt;
&lt;TD&gt;Header Amount&lt;/TD&gt;
&lt;TD&gt;Detail Amount&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;-&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;196.17&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A2&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;-&lt;/TD&gt;
&lt;TD&gt;A2&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;24.57&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A3&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;A3&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;173.87&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A4&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;-&lt;/TD&gt;
&lt;TD&gt;A4&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;97.14&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;The first sorted value is correct as a dimension, but only populates against the 3rd record in the measure. The If statement returns 0 as all the conditions are not met. The formulae I use is:&lt;/P&gt;
&lt;P&gt;If(Aggr(FirstSortedValue(Distinct DetailKey, LineNo), Header) = DetailKey, Sum(Header Amount),0)&lt;/P&gt;
&lt;P&gt;I need to understand why the FirstSortedValue in the measures (A1) is placed on line 3 and not on all the lines.&lt;/P&gt;
&lt;P&gt;I would appreciate some assistance on this.&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;</description>
    <pubDate>Mon, 23 Sep 2024 20:56:27 GMT</pubDate>
    <dc:creator>braham</dc:creator>
    <dc:date>2024-09-23T20:56:27Z</dc:date>
    <item>
      <title>First Sorted value not working when calculating a measure</title>
      <link>https://community.qlik.com/t5/App-Development/First-Sorted-value-not-working-when-calculating-a-measure/m-p/2483073#M100987</link>
      <description>&lt;P&gt;Evening all,&lt;/P&gt;
&lt;P&gt;Once again I need to reach out to the community to produce a chart. I would appreciate your assistance.&lt;/P&gt;
&lt;P&gt;I have a header/Detail record structure. What is unusual is that when you add up the values in the detail it does not always equal the value stored in the header. If there are say 4 items in the detail, I want to only show the header value against the 1st item in the detail and show the header value as 0 for the rest as per the table below:&lt;/P&gt;
&lt;TABLE width="403"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="104"&gt;Header&lt;/TD&gt;
&lt;TD width="79"&gt;Detail Key&lt;/TD&gt;
&lt;TD width="114"&gt;Header Amount&lt;/TD&gt;
&lt;TD width="106"&gt;Detail Amount&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;600&lt;/TD&gt;
&lt;TD&gt;196.17&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A2&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;24.57&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A3&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;173.87&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A4&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;97.14&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To achieve this, I am using the FirstSortedValue function to return A1 and use an if statement to compare the FirstSortedVale and DetailKey. If they are equal, Sum the Header Value else set it to 0. When using the formulae I get the following table. I have included the first sorted value as a dimension and split the two conditions being compared in the measures to demonstrate what is happening.&lt;/P&gt;
&lt;TABLE width="732"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD colspan="3" width="297"&gt;&amp;lt;--------------------Dimensions-----------------------&amp;gt;&lt;/TD&gt;
&lt;TD colspan="3" width="338"&gt;&amp;lt;----------------------------------------Measures---------------------------&amp;gt;&lt;/TD&gt;
&lt;TD width="97"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Header&lt;/TD&gt;
&lt;TD&gt;Detail Key&lt;/TD&gt;
&lt;TD&gt;First Sorted Value&lt;/TD&gt;
&lt;TD&gt;First Sorted Value&lt;/TD&gt;
&lt;TD&gt;Detail key&lt;/TD&gt;
&lt;TD&gt;Header Amount&lt;/TD&gt;
&lt;TD&gt;Detail Amount&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;-&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;196.17&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A2&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;-&lt;/TD&gt;
&lt;TD&gt;A2&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;24.57&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A3&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;A3&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;173.87&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;A4&lt;/TD&gt;
&lt;TD&gt;A1&lt;/TD&gt;
&lt;TD&gt;-&lt;/TD&gt;
&lt;TD&gt;A4&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;97.14&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;The first sorted value is correct as a dimension, but only populates against the 3rd record in the measure. The If statement returns 0 as all the conditions are not met. The formulae I use is:&lt;/P&gt;
&lt;P&gt;If(Aggr(FirstSortedValue(Distinct DetailKey, LineNo), Header) = DetailKey, Sum(Header Amount),0)&lt;/P&gt;
&lt;P&gt;I need to understand why the FirstSortedValue in the measures (A1) is placed on line 3 and not on all the lines.&lt;/P&gt;
&lt;P&gt;I would appreciate some assistance on this.&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2024 20:56:27 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/First-Sorted-value-not-working-when-calculating-a-measure/m-p/2483073#M100987</guid>
      <dc:creator>braham</dc:creator>
      <dc:date>2024-09-23T20:56:27Z</dc:date>
    </item>
    <item>
      <title>Re: First Sorted value not working when calculating a measure</title>
      <link>https://community.qlik.com/t5/App-Development/First-Sorted-value-not-working-when-calculating-a-measure/m-p/2483074#M100988</link>
      <description>&lt;P&gt;You have a grain mismatch between the Aggr() and the chart. See&amp;nbsp;&lt;A href="https://community.qlik.com/t5/Design/Pitfalls-of-the-Aggr-function/ba-p/1463275" target="_blank"&gt;https://community.qlik.com/t5/Design/Pitfalls-of-the-Aggr-function/ba-p/1463275&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Try using&amp;nbsp;&lt;SPAN&gt;DetailKey as dimension inside the Aggr():&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If(&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;Aggr(FirstSortedValue(Distinct Total &amp;lt;Header&amp;gt; DetailKey, LineNo), Header, DetailKey) = DetailKey, &lt;BR /&gt;&amp;nbsp; &amp;nbsp;Sum(Header Amount),&lt;BR /&gt;&amp;nbsp; &amp;nbsp;0&lt;BR /&gt;)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Or try using ... Aggr(NoDistinct ...) ...&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2024 21:08:02 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/First-Sorted-value-not-working-when-calculating-a-measure/m-p/2483074#M100988</guid>
      <dc:creator>hic</dc:creator>
      <dc:date>2024-09-23T21:08:02Z</dc:date>
    </item>
    <item>
      <title>Re: First Sorted value not working when calculating a measure</title>
      <link>https://community.qlik.com/t5/App-Development/First-Sorted-value-not-working-when-calculating-a-measure/m-p/2483174#M101000</link>
      <description>&lt;P&gt;Morning HIC, Thanks so much for the help, the 'NoDistinct' resolved the problem. I now get the FirstSortedValue on each line and the If statement now works and only shows the summed header value on the first line as I wanted.&lt;/P&gt;
&lt;P&gt;I have read a number of your articles, so appreciate your insights into the workings of Qlik.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;
&lt;P&gt;Braham&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 09:41:39 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/First-Sorted-value-not-working-when-calculating-a-measure/m-p/2483174#M101000</guid>
      <dc:creator>braham</dc:creator>
      <dc:date>2024-09-24T09:41:39Z</dc:date>
    </item>
  </channel>
</rss>

