<?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 How add a field to table while looping the records because depending of previous values to reset in App Development</title>
    <link>https://community.qlik.com/t5/App-Development/How-add-a-field-to-table-while-looping-the-records-because/m-p/1567656#M41409</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I want to add a field "running saldo" to each record in a table. Depending of some conditions the running saldo must restart at zero, therefore it must be done in the load script and i cannot use functions like rangesum in the chart&lt;/P&gt;&lt;P&gt;I see how I can loop through the records and calculate the running saldo. But I don't see how i can add this field to the appropriate record in my table.&lt;/P&gt;&lt;P&gt;My script looks something like this:&lt;/P&gt;&lt;P&gt;FactTemp:&lt;/P&gt;&lt;P&gt;…&lt;/P&gt;&lt;P&gt;&lt;EM&gt;(FactTemp is the full table with all fields loaded, and where i finally want to add the "running saldo" to all records)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;AddCommandInProgressTemp:&lt;BR /&gt;&lt;BR /&gt;Load&lt;BR /&gt;article&lt;BR /&gt;, documentDate&lt;BR /&gt;, document&lt;BR /&gt;, quantity&lt;BR /&gt;&lt;SPAN&gt;Resident FactTemp&lt;BR /&gt;&lt;/SPAN&gt;Order by article, documentDate;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;//(it's important that the records are read in the correct sort order)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;Let vNoOfRows = NoOfRows('FactTemp');&lt;BR /&gt;Set vSaldo = 0;&lt;BR /&gt;Set vNewSaldo = 0;&lt;BR /&gt;Set vPrevArticle = '';&lt;/P&gt;&lt;P&gt;FOR i=0 to $(vNoOfRows)-1&lt;/P&gt;&lt;P&gt;&amp;nbsp; SET vQuantity=Peek('[quantity]',$(i),'AddCommandInProgress');&amp;nbsp; &lt;EM&gt;//(quantities can be positive or negative)&lt;BR /&gt;&lt;/EM&gt;&amp;nbsp; SET vArticle=Peek('[article]',$(i),'AddCommandInProgress');&lt;BR /&gt;&amp;nbsp; SET vDocumentDate=Peek('[documentDate]',$(i),'AddCommandInProgress');&lt;BR /&gt;&amp;nbsp; SET vDocNr=Peek('[document]',$(i),'AddCommandInProgress');&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;//(for each new article, running saldo must turn to zero)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; If $( vArticle) &amp;lt;&amp;gt;&amp;nbsp; vPrevArticle then&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SET vSaldo = 0;&lt;BR /&gt;&amp;nbsp;End If&lt;/P&gt;&lt;P&gt;&amp;nbsp; Let vNewSaldo = vSaldo + vQuantity;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;//(running saldo cannot be negative. When negative restart at zero)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; If vNewSaldo &amp;lt; 0&amp;nbsp;&amp;nbsp; Then&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Set vNewSaldo = 0&lt;BR /&gt;EndIf&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;Let vPrevArticle = $( vArticle);&lt;/P&gt;&lt;P&gt;&amp;nbsp; let vSaldo = vNewSaldo;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;(the purpuse of AddFields is to build a table where the new field NewSaldo is added and to finally add them to the original table. But like this i get a ScriptError : "Unexpected Token: NewSaldo..."&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;AddFields:&lt;BR /&gt;&lt;SPAN&gt;Left Join(FactTemp)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;LOAD&lt;BR /&gt;$( vArticle) as article&lt;BR /&gt;,$( vDocumentDate) as documentDate&lt;BR /&gt;,$(vDocNr) as document&lt;BR /&gt;,$(vNewSaldo) as [NewSaldo];&lt;/P&gt;&lt;P&gt;NEXT i;&lt;/P&gt;&lt;P&gt;Can someone explain how this can be done?&lt;/P&gt;&lt;P&gt;Tx&lt;/P&gt;</description>
    <pubDate>Sat, 16 Nov 2024 21:04:43 GMT</pubDate>
    <dc:creator>kristien</dc:creator>
    <dc:date>2024-11-16T21:04:43Z</dc:date>
    <item>
      <title>How add a field to table while looping the records because depending of previous values to reset</title>
      <link>https://community.qlik.com/t5/App-Development/How-add-a-field-to-table-while-looping-the-records-because/m-p/1567656#M41409</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I want to add a field "running saldo" to each record in a table. Depending of some conditions the running saldo must restart at zero, therefore it must be done in the load script and i cannot use functions like rangesum in the chart&lt;/P&gt;&lt;P&gt;I see how I can loop through the records and calculate the running saldo. But I don't see how i can add this field to the appropriate record in my table.&lt;/P&gt;&lt;P&gt;My script looks something like this:&lt;/P&gt;&lt;P&gt;FactTemp:&lt;/P&gt;&lt;P&gt;…&lt;/P&gt;&lt;P&gt;&lt;EM&gt;(FactTemp is the full table with all fields loaded, and where i finally want to add the "running saldo" to all records)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;AddCommandInProgressTemp:&lt;BR /&gt;&lt;BR /&gt;Load&lt;BR /&gt;article&lt;BR /&gt;, documentDate&lt;BR /&gt;, document&lt;BR /&gt;, quantity&lt;BR /&gt;&lt;SPAN&gt;Resident FactTemp&lt;BR /&gt;&lt;/SPAN&gt;Order by article, documentDate;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;//(it's important that the records are read in the correct sort order)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;Let vNoOfRows = NoOfRows('FactTemp');&lt;BR /&gt;Set vSaldo = 0;&lt;BR /&gt;Set vNewSaldo = 0;&lt;BR /&gt;Set vPrevArticle = '';&lt;/P&gt;&lt;P&gt;FOR i=0 to $(vNoOfRows)-1&lt;/P&gt;&lt;P&gt;&amp;nbsp; SET vQuantity=Peek('[quantity]',$(i),'AddCommandInProgress');&amp;nbsp; &lt;EM&gt;//(quantities can be positive or negative)&lt;BR /&gt;&lt;/EM&gt;&amp;nbsp; SET vArticle=Peek('[article]',$(i),'AddCommandInProgress');&lt;BR /&gt;&amp;nbsp; SET vDocumentDate=Peek('[documentDate]',$(i),'AddCommandInProgress');&lt;BR /&gt;&amp;nbsp; SET vDocNr=Peek('[document]',$(i),'AddCommandInProgress');&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&amp;nbsp;//(for each new article, running saldo must turn to zero)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; If $( vArticle) &amp;lt;&amp;gt;&amp;nbsp; vPrevArticle then&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SET vSaldo = 0;&lt;BR /&gt;&amp;nbsp;End If&lt;/P&gt;&lt;P&gt;&amp;nbsp; Let vNewSaldo = vSaldo + vQuantity;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;//(running saldo cannot be negative. When negative restart at zero)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; If vNewSaldo &amp;lt; 0&amp;nbsp;&amp;nbsp; Then&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Set vNewSaldo = 0&lt;BR /&gt;EndIf&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;Let vPrevArticle = $( vArticle);&lt;/P&gt;&lt;P&gt;&amp;nbsp; let vSaldo = vNewSaldo;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;(the purpuse of AddFields is to build a table where the new field NewSaldo is added and to finally add them to the original table. But like this i get a ScriptError : "Unexpected Token: NewSaldo..."&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;AddFields:&lt;BR /&gt;&lt;SPAN&gt;Left Join(FactTemp)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;LOAD&lt;BR /&gt;$( vArticle) as article&lt;BR /&gt;,$( vDocumentDate) as documentDate&lt;BR /&gt;,$(vDocNr) as document&lt;BR /&gt;,$(vNewSaldo) as [NewSaldo];&lt;/P&gt;&lt;P&gt;NEXT i;&lt;/P&gt;&lt;P&gt;Can someone explain how this can be done?&lt;/P&gt;&lt;P&gt;Tx&lt;/P&gt;</description>
      <pubDate>Sat, 16 Nov 2024 21:04:43 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/How-add-a-field-to-table-while-looping-the-records-because/m-p/1567656#M41409</guid>
      <dc:creator>kristien</dc:creator>
      <dc:date>2024-11-16T21:04:43Z</dc:date>
    </item>
    <item>
      <title>Re: How add a field to table while looping the records because depending of previous values to reset</title>
      <link>https://community.qlik.com/t5/App-Development/How-add-a-field-to-table-while-looping-the-records-because/m-p/1567694#M41413</link>
      <description>&lt;P&gt;Generally a running total is added using the Previous and Peek function with a regular LOAD. Loops are not necessary.&lt;/P&gt;&lt;PRE&gt;UnsortedFacts:
LOAD
    Article,
    Date,
    Quantity
FROM
   ...source... ;

Facts:
NOCONCATENATE LOAD
     Article,
     Date,
     Quantity,
     If(Previous(Article)=Article, RangeMax(0,RangeSum(Quantity, Peek(RunningSaldo))),Quantity) as RunningSaldo
RESIDENT
     UnsortedFacts
ORDER BY
     Article,
     Date
     ;&lt;BR /&gt;&lt;BR /&gt;DROP TABLE UnsortedFacts;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Apr 2019 14:15:49 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/How-add-a-field-to-table-while-looping-the-records-because/m-p/1567694#M41413</guid>
      <dc:creator>Gysbert_Wassenaar</dc:creator>
      <dc:date>2019-04-10T14:15:49Z</dc:date>
    </item>
    <item>
      <title>Re: How add a field to table while looping the records because depending of previous values to reset</title>
      <link>https://community.qlik.com/t5/App-Development/How-add-a-field-to-table-while-looping-the-records-because/m-p/1567757#M41418</link>
      <description>&lt;P&gt;Thanks, this helped me,&amp;nbsp; I'm back on the right track now&lt;/P&gt;</description>
      <pubDate>Wed, 10 Apr 2019 15:46:15 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/How-add-a-field-to-table-while-looping-the-records-because/m-p/1567757#M41418</guid>
      <dc:creator>kristien</dc:creator>
      <dc:date>2019-04-10T15:46:15Z</dc:date>
    </item>
  </channel>
</rss>

