<?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: Calculate the remaining Balance in QlikView</title>
    <link>https://community.qlik.com/t5/QlikView/Calculate-the-remaining-Balance/m-p/1614498#M596873</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;You will need a lot more from your client I think but it is possible to script allocating matched &amp;amp; unmatched items, the following shows a simplistic approach and I suspect is very easy to break.&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Chris.&lt;/P&gt;&lt;P&gt;Ps I am on DD/MM/YYYY dates&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;invoice:
LOAD * INLINE [
    invoice_id, date, amount
    1, 01/01/2019, 100
    2, 01/02/2019,50
    3, 01/03/2019, 75
    4, 01/04/2019, 100
];

reciept:
NoConcatenate
LOAD * INLINE [
    date, amount, invoice_id
    01/01/2019, 100, 1
    01/03/2019, 75, 3
    01/04/2019, 100,
    01/04/2019, 25,
];

Left Join (invoice)
Load
	invoice_id,
	amount as match_amount
Resident reciept;

unmatched_invoice:
NoConcatenate
Load
	invoice_id, date, amount
Resident invoice
Where isnull(match_amount)
order by date asc;

unmatched_reciept:
NoConcatenate
Load
	date, amount
Resident reciept
Where isnull(invoice_id) or invoice_id='';

allocation:
NoConcatenate
Load
	-1 as invoice_id,
	0 as allocation
AUTOGENERATE 1;

j=0;
for i=0 to NoOfRows('unmatched_reciept')-1

	toallocate=Peek('amount',i,'unmatched_reciept')
	
	do until toallocate=0 or j&amp;gt;NoOfRows('unmatched_invoice')-1

		if toallocate&amp;gt;=Peek('amount',j,'unmatched_invoice') then
		
			Concatenate (allocation)
			Load
				Peek('invoice_id',$(j),'unmatched_invoice') as invoice_id,
				Peek('amount',$(j),'unmatched_invoice') as allocation
			AUTOGENERATE 1;
			
			toallocate=toallocate-Peek('amount',$(j),'unmatched_invoice');
			j=j+1;
			
		else
		
			Concatenate (allocation)
			Load
				Peek('invoice_id',$(j),'unmatched_invoice') as invoice_id,
				$(toallocate) as allocation
			AUTOGENERATE 1;

			toallocate=0;
			
		end if
		
	loop

next i;

allocation_aggregate:
NoConcatenate
Load
	invoice_id,
	sum(allocation) AS allocation
Resident allocation
Group By invoice_id;

drop tables reciept, unmatched_invoice, unmatched_reciept, allocation;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 20 Aug 2019 20:35:15 GMT</pubDate>
    <dc:creator>chrismarlow</dc:creator>
    <dc:date>2019-08-20T20:35:15Z</dc:date>
    <item>
      <title>Calculate the remaining Balance</title>
      <link>https://community.qlik.com/t5/QlikView/Calculate-the-remaining-Balance/m-p/1612364#M596871</link>
      <description>&lt;P&gt;Hi ,&amp;nbsp;&lt;/P&gt;&lt;P&gt;One of our clients has a strange request but I still have to answer.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The purpose of the exercise is:&lt;/P&gt;&lt;P&gt;-I must calculate the balance of the bills by month of billing. 70% of payments received are related to invoices through a structured communication. But 30% of the payments are not related to any invoice. So I must try to assign payments to the oldest bills and identify the remaining balance.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Should I answer by No it's not possible or can someone see a solution ?&lt;BR /&gt;&lt;BR /&gt;Thanks for your help&amp;nbsp;&lt;/P&gt;&lt;P&gt;Marwen&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2019 13:24:16 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Calculate-the-remaining-Balance/m-p/1612364#M596871</guid>
      <dc:creator>marwen_garwachi</dc:creator>
      <dc:date>2019-08-14T13:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the remaining Balance</title>
      <link>https://community.qlik.com/t5/QlikView/Calculate-the-remaining-Balance/m-p/1613504#M596872</link>
      <description>&lt;P&gt;What is the logic to identify which is "Invoice"? If you can create, Flag on that we can define?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Aug 2019 14:18:18 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Calculate-the-remaining-Balance/m-p/1613504#M596872</guid>
      <dc:creator>Anil_Babu_Samineni</dc:creator>
      <dc:date>2019-08-17T14:18:18Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the remaining Balance</title>
      <link>https://community.qlik.com/t5/QlikView/Calculate-the-remaining-Balance/m-p/1614498#M596873</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;You will need a lot more from your client I think but it is possible to script allocating matched &amp;amp; unmatched items, the following shows a simplistic approach and I suspect is very easy to break.&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Chris.&lt;/P&gt;&lt;P&gt;Ps I am on DD/MM/YYYY dates&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;invoice:
LOAD * INLINE [
    invoice_id, date, amount
    1, 01/01/2019, 100
    2, 01/02/2019,50
    3, 01/03/2019, 75
    4, 01/04/2019, 100
];

reciept:
NoConcatenate
LOAD * INLINE [
    date, amount, invoice_id
    01/01/2019, 100, 1
    01/03/2019, 75, 3
    01/04/2019, 100,
    01/04/2019, 25,
];

Left Join (invoice)
Load
	invoice_id,
	amount as match_amount
Resident reciept;

unmatched_invoice:
NoConcatenate
Load
	invoice_id, date, amount
Resident invoice
Where isnull(match_amount)
order by date asc;

unmatched_reciept:
NoConcatenate
Load
	date, amount
Resident reciept
Where isnull(invoice_id) or invoice_id='';

allocation:
NoConcatenate
Load
	-1 as invoice_id,
	0 as allocation
AUTOGENERATE 1;

j=0;
for i=0 to NoOfRows('unmatched_reciept')-1

	toallocate=Peek('amount',i,'unmatched_reciept')
	
	do until toallocate=0 or j&amp;gt;NoOfRows('unmatched_invoice')-1

		if toallocate&amp;gt;=Peek('amount',j,'unmatched_invoice') then
		
			Concatenate (allocation)
			Load
				Peek('invoice_id',$(j),'unmatched_invoice') as invoice_id,
				Peek('amount',$(j),'unmatched_invoice') as allocation
			AUTOGENERATE 1;
			
			toallocate=toallocate-Peek('amount',$(j),'unmatched_invoice');
			j=j+1;
			
		else
		
			Concatenate (allocation)
			Load
				Peek('invoice_id',$(j),'unmatched_invoice') as invoice_id,
				$(toallocate) as allocation
			AUTOGENERATE 1;

			toallocate=0;
			
		end if
		
	loop

next i;

allocation_aggregate:
NoConcatenate
Load
	invoice_id,
	sum(allocation) AS allocation
Resident allocation
Group By invoice_id;

drop tables reciept, unmatched_invoice, unmatched_reciept, allocation;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 20:35:15 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Calculate-the-remaining-Balance/m-p/1614498#M596873</guid>
      <dc:creator>chrismarlow</dc:creator>
      <dc:date>2019-08-20T20:35:15Z</dc:date>
    </item>
  </channel>
</rss>

