<?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: row-by-row filtered Difference calculation with advanced Aggregation in load script in QlikView</title>
    <link>https://community.qlik.com/t5/QlikView/row-by-row-filtered-Difference-calculation-with-advanced/m-p/1970970#M1220558</link>
    <description>&lt;P&gt;If you need to do this in script then may be something like this, sorting records in a specific order helps in this approach.&lt;/P&gt;
&lt;P&gt;source:&lt;BR /&gt;Load * inline [&lt;BR /&gt;Field1 Field2 Measure&lt;BR /&gt;A D 4&lt;BR /&gt;B E 10&lt;BR /&gt;A D 3&lt;BR /&gt;A F 1&lt;BR /&gt;C D 8&lt;BR /&gt;B E 5&lt;BR /&gt;B F 4&lt;BR /&gt;C D 3&lt;BR /&gt;C D 9&lt;BR /&gt;C F 15&lt;BR /&gt;A F 13&lt;BR /&gt;](delimiter is spaces)&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;NoConcatenate&lt;/P&gt;
&lt;P&gt;Final1:&lt;BR /&gt;Load *&lt;BR /&gt;resident source&lt;BR /&gt;Order By Field1, Field2, Measure DESC&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;NoConcatenate&lt;BR /&gt;Final2:&lt;BR /&gt;Load *,&lt;BR /&gt;if(Field1=Previous(Field1) and Field2=Previous(Field2) and peek(MaxMeasure)&amp;gt;Measure, peek(MaxMeasure)-Measure, 'NA') as Diff&lt;BR /&gt;;&lt;BR /&gt;Load *, &lt;BR /&gt;if(Field1=Previous(Field1) and Field2=Previous(Field2) and len(peek(MaxMeasure))&amp;lt;&amp;gt;0, Peek(MaxMeasure),Measure) as MaxMeasure &lt;BR /&gt;resident Final1;&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;drop table source,Final1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Digvijay_Singh_0-1661036781435.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/87121i2DA88D664881CB44/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Digvijay_Singh_0-1661036781435.png" alt="Digvijay_Singh_0-1661036781435.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 20 Aug 2022 23:06:33 GMT</pubDate>
    <dc:creator>Digvijay_Singh</dc:creator>
    <dc:date>2022-08-20T23:06:33Z</dc:date>
    <item>
      <title>row-by-row filtered Difference calculation with advanced Aggregation in load script</title>
      <link>https://community.qlik.com/t5/QlikView/row-by-row-filtered-Difference-calculation-with-advanced/m-p/1970909#M1220555</link>
      <description>&lt;P&gt;Hello, recently I've run into issue where I have a dataset with 2 info fields and 1 measure field, and for each row I need to calculate the difference between the current row's measure and the row with the same info fields but the NEXT BIGGEST measure, if no next biggest measure exists, the cell is left empty. This is an example the data I'm starting with:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TimurKhayrullin_0-1660947655436.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/87104i6862DFD715FA24F7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TimurKhayrullin_0-1660947655436.png" alt="TimurKhayrullin_0-1660947655436.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;and this is what I'm aiming for:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TimurKhayrullin_1-1660947753972.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/87105iFE3D30F069EB23C4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TimurKhayrullin_1-1660947753972.png" alt="TimurKhayrullin_1-1660947753972.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;For example, the row "A, D, 3" has Diff value 1 because the next biggest row value filtered for fields 1 and 2 is "A, D, 4", and 4-3 is 1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My Approach has been to try and generate an intermediary column "nextMeasure" that finds the next biggest measure in accordance to the filters, so then the desired column is a simple subtraction:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TimurKhayrullin_2-1660948079940.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/87106i4F811587CB014C60/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TimurKhayrullin_2-1660948079940.png" alt="TimurKhayrullin_2-1660948079940.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;EDIT: my apologies, I made a small mistake that invalidates this example. The following is an ammended version with proper difference values (shown in red):&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TimurKhayrullin_1-1661193057237.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/87209i4A91A0A6D6413F3F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TimurKhayrullin_1-1661193057237.png" alt="TimurKhayrullin_1-1661193057237.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;After digging extensively online I have come up a "pseudo-expression" that (almost) works to generate this "nextMeasure" column:&amp;nbsp;&lt;STRONG&gt; FirstSortedValue(Measure, Aggr(if(Measure &amp;gt; [current row's measure], 1, 0), Field 2, Field 1)) as nextMeasure&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;In my view there are 4 problems with my expression&lt;/P&gt;
&lt;P&gt;1) Aggr can't be used in a load script&lt;/P&gt;
&lt;P&gt;2) I don't know how to properly reference the current row's measure in an if statement&lt;/P&gt;
&lt;P&gt;3) I don't know if the IF statement is the appropriate to handle the aggregation&lt;/P&gt;
&lt;P&gt;4) I don't know how to leave the cell blank if no next biggest measure is found&lt;/P&gt;
&lt;P&gt;Could anyone help me generate the desired column? Any advice helps, it doesn't have to use my methods.&lt;/P&gt;
&lt;P&gt;I've attached a QVD file with my input data as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Aug 2022 18:30:30 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/row-by-row-filtered-Difference-calculation-with-advanced/m-p/1970909#M1220555</guid>
      <dc:creator>TimurKhayrullin</dc:creator>
      <dc:date>2022-08-22T18:30:30Z</dc:date>
    </item>
    <item>
      <title>Re: row-by-row filtered Difference calculation with advanced Aggregation in load script</title>
      <link>https://community.qlik.com/t5/QlikView/row-by-row-filtered-Difference-calculation-with-advanced/m-p/1970970#M1220558</link>
      <description>&lt;P&gt;If you need to do this in script then may be something like this, sorting records in a specific order helps in this approach.&lt;/P&gt;
&lt;P&gt;source:&lt;BR /&gt;Load * inline [&lt;BR /&gt;Field1 Field2 Measure&lt;BR /&gt;A D 4&lt;BR /&gt;B E 10&lt;BR /&gt;A D 3&lt;BR /&gt;A F 1&lt;BR /&gt;C D 8&lt;BR /&gt;B E 5&lt;BR /&gt;B F 4&lt;BR /&gt;C D 3&lt;BR /&gt;C D 9&lt;BR /&gt;C F 15&lt;BR /&gt;A F 13&lt;BR /&gt;](delimiter is spaces)&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;NoConcatenate&lt;/P&gt;
&lt;P&gt;Final1:&lt;BR /&gt;Load *&lt;BR /&gt;resident source&lt;BR /&gt;Order By Field1, Field2, Measure DESC&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;NoConcatenate&lt;BR /&gt;Final2:&lt;BR /&gt;Load *,&lt;BR /&gt;if(Field1=Previous(Field1) and Field2=Previous(Field2) and peek(MaxMeasure)&amp;gt;Measure, peek(MaxMeasure)-Measure, 'NA') as Diff&lt;BR /&gt;;&lt;BR /&gt;Load *, &lt;BR /&gt;if(Field1=Previous(Field1) and Field2=Previous(Field2) and len(peek(MaxMeasure))&amp;lt;&amp;gt;0, Peek(MaxMeasure),Measure) as MaxMeasure &lt;BR /&gt;resident Final1;&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;drop table source,Final1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Digvijay_Singh_0-1661036781435.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/87121i2DA88D664881CB44/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Digvijay_Singh_0-1661036781435.png" alt="Digvijay_Singh_0-1661036781435.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Aug 2022 23:06:33 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/row-by-row-filtered-Difference-calculation-with-advanced/m-p/1970970#M1220558</guid>
      <dc:creator>Digvijay_Singh</dc:creator>
      <dc:date>2022-08-20T23:06:33Z</dc:date>
    </item>
    <item>
      <title>Re: row-by-row filtered Difference calculation with advanced Aggregation in load script</title>
      <link>https://community.qlik.com/t5/QlikView/row-by-row-filtered-Difference-calculation-with-advanced/m-p/1971504#M1220571</link>
      <description>&lt;P&gt;Hi Digvijay, after trying your solution I realized I does not do quite what I need. However, this is completely my fault as I made a mistake in my example. Here is the ammended final table:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TimurKhayrullin_0-1661193169782.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/87210i5C6395F6E4B80D86/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TimurKhayrullin_0-1661193169782.png" alt="TimurKhayrullin_0-1661193169782.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;your solution is close, but it relates the current to the MAX of the required filters. I just need the NEXT BIGGEST. In my example, "C,D,3" should have diff value 5, not 6. Yours gives the value of 6. How could the solution be changed to give the desired outcomes?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Aug 2022 18:33:47 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/row-by-row-filtered-Difference-calculation-with-advanced/m-p/1971504#M1220571</guid>
      <dc:creator>TimurKhayrullin</dc:creator>
      <dc:date>2022-08-22T18:33:47Z</dc:date>
    </item>
    <item>
      <title>Re: row-by-row filtered Difference calculation with advanced Aggregation in load script</title>
      <link>https://community.qlik.com/t5/QlikView/row-by-row-filtered-Difference-calculation-with-advanced/m-p/1971511#M1220572</link>
      <description>&lt;P&gt;After editing the proposed solution I came up with a load script that fixes the error. It ended up being much simpler. Nonetheless I must thank&amp;nbsp;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/48860"&gt;@Digvijay_Singh&lt;/a&gt;&amp;nbsp;for the approach! Without it I'd be lost. Here's the load script in full:&lt;/P&gt;
&lt;P&gt;source:&lt;BR /&gt;Load * inline [&lt;BR /&gt;Field1 Field2 Measure&lt;BR /&gt;A D 4&lt;BR /&gt;B E 10&lt;BR /&gt;A D 3&lt;BR /&gt;A F 1&lt;BR /&gt;C D 8&lt;BR /&gt;B E 5&lt;BR /&gt;B F 4&lt;BR /&gt;C D 3&lt;BR /&gt;C D 9&lt;BR /&gt;C F 15&lt;BR /&gt;A F 13&lt;BR /&gt;](delimiter is spaces)&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;NoConcatenate&lt;/P&gt;
&lt;P&gt;Final1:&lt;BR /&gt;Load *&lt;BR /&gt;resident source&lt;BR /&gt;Order By Field1, Field2, Measure DESC&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;NoConcatenate&lt;BR /&gt;Final2:&lt;BR /&gt;Load *,&lt;BR /&gt;if(Field1=Previous(Field1) and Field2=Previous(Field2) and Previous(Measure)&amp;gt;Measure, Previous(Measure), 'NA') as nextMeasure,&lt;BR /&gt;if(Field1=Previous(Field1) and Field2=Previous(Field2) and Previous(Measure)&amp;gt;Measure, Previous(Measure)-Measure, 'NA') as Diff&lt;BR /&gt;resident Final1;&lt;/P&gt;
&lt;P&gt;drop table source,Final1;&lt;/P&gt;
&lt;P&gt;And here is the result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TimurKhayrullin_0-1661194638197.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/87211iE954C01B6911EDAE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TimurKhayrullin_0-1661194638197.png" alt="TimurKhayrullin_0-1661194638197.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I've also attached the .qvf with the results.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Aug 2022 18:58:25 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/row-by-row-filtered-Difference-calculation-with-advanced/m-p/1971511#M1220572</guid>
      <dc:creator>TimurKhayrullin</dc:creator>
      <dc:date>2022-08-22T18:58:25Z</dc:date>
    </item>
    <item>
      <title>Re: row-by-row filtered Difference calculation with advanced Aggregation in load script</title>
      <link>https://community.qlik.com/t5/QlikView/row-by-row-filtered-Difference-calculation-with-advanced/m-p/1971519#M1220573</link>
      <description>&lt;P&gt;Great! In fact my first test had this output from my script but than I updated it further to match with your output &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Aug 2022 19:08:58 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/row-by-row-filtered-Difference-calculation-with-advanced/m-p/1971519#M1220573</guid>
      <dc:creator>Digvijay_Singh</dc:creator>
      <dc:date>2022-08-22T19:08:58Z</dc:date>
    </item>
  </channel>
</rss>

