<?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: Looking for the first, second... purchase price in App Development</title>
    <link>https://community.qlik.com/t5/App-Development/Looking-for-the-first-second-purchase-price/m-p/2121452#M91313</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/198219"&gt;@Ernest97&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;First of all, join your two tables and create an unique table. After that, create a new calculated dimension (a flag):&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Purchasing_table:
LOAD *
Inline [
Date_purchase, Article_No, Quantity_purchase, Price_purchase
03.08.2023, 334443, 320, 221.5
07.07.2023, 334443, 960, 664.5
16.05.2023, 334443, 220, 156.38
09.05.2023, 334443, 100, 71.08
15.03.2023, 334443, 320, 227.47
08.03.2023, 334443, 320, 227.47
23.11.2022, 334443, 1600, 1107.5
16.08.2023, 124455, 1000, 150
13.07.2023, 124455, 500, 50
16.06.2023, 124455, 1000, 150
13.05.2023, 124455, 500, 50
];

Sales_table:
JOIN (Purchasing_table)
LOAD
*
Inline [
Date_sale, Article_No, Quantity_sale, Price_purchase_sale
19.06.2023, 334443, 600, 438.4
15.05.2023, 334443, 450, 190
16.06.2023, 124455, 200, 200
13.04.2023, 124455, 500, 100
];

NoConcatenate
MASTER_TAB:
LOAD 
IF([Date_sale] &amp;lt;= [Date_purchase],1,0) as Flag,
* 
RESIDENT Purchasing_table;

DROP TABLE Purchasing_table;&lt;/LI-CODE&gt;
&lt;P&gt;Normally, you'll be able to use this calculated like a flag in your Firstsortedvalue() expression (with a set analysis). Test it and comeback to us.&lt;/P&gt;
&lt;P&gt;Regards.&lt;/P&gt;</description>
    <pubDate>Fri, 22 Sep 2023 14:56:10 GMT</pubDate>
    <dc:creator>sbaro_bd</dc:creator>
    <dc:date>2023-09-22T14:56:10Z</dc:date>
    <item>
      <title>Looking for the first, second... purchase price</title>
      <link>https://community.qlik.com/t5/App-Development/Looking-for-the-first-second-purchase-price/m-p/2120848#M91278</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;This is my first post on the forum so I would like to thank you for your help.&lt;/P&gt;
&lt;P&gt;I created a table to test to find a solution, here is my data:&lt;BR /&gt;Purchasing_table:&lt;/P&gt;
&lt;P&gt;LOAD *&lt;BR /&gt;Inline [&lt;BR /&gt;Date_purchase, Article_No, Quantity_purchase, Price_purchase&lt;BR /&gt;03.08.2023, 334443, 320, 221.5&lt;BR /&gt;07.07.2023, 334443, 960, 664.5&lt;BR /&gt;16.05.2023, 334443, 220, 156.38&lt;BR /&gt;09.05.2023, 334443, 100, 71.08&lt;BR /&gt;15.03.2023, 334443, 320, 227.47&lt;BR /&gt;08.03.2023, 334443, 320, 227.47&lt;BR /&gt;23.11.2022, 334443, 1600, 1107.5&lt;BR /&gt;16.08.2023, 124455, 1000, 150&lt;BR /&gt;13.07.2023, 124455, 500, 50&lt;BR /&gt;16.06.2023, 124455, 1000, 150&lt;BR /&gt;13.05.2023, 124455, 500, 50&lt;BR /&gt;];&lt;/P&gt;
&lt;P&gt;Sales_table:&lt;BR /&gt;LOAD *&lt;BR /&gt;Inline [&lt;BR /&gt;Date_sale, Article_No, Quantity_sale, Price_purchase_sale&lt;BR /&gt;19.06.2023, 334443, 600, 438.4&lt;BR /&gt;15.05.2023, 334443, 450, 190&lt;BR /&gt;16.06.2023, 124455, 200, 200&lt;BR /&gt;13.04.2023, 124455, 500, 100&lt;BR /&gt;];&lt;/P&gt;
&lt;P&gt;I would like to assign the first 3 found values from the purchase table to the sales table, the article number must match ([Article_No] = [Article_No]).&lt;BR /&gt;I know that this can be done using FirstSortedValue(),but there is another condition the date of sale must be less than or equal to the purchase date ([Date_sale] &amp;lt;= [Date_purchase]).&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;I would like to create a table like the one below, that is, for each sale, I would like to assign the last purchase using the conditions "[Article_No] = [Article_No] and [Date_sale] &amp;lt;= [Date_purchase]".&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ernest97_1-1695284970404.png" style="width: 888px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/116733i4030092E53A75619/image-dimensions/888x76?v=v2" width="888" height="76" role="button" title="Ernest97_1-1695284970404.png" alt="Ernest97_1-1695284970404.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I am in the process of looking for a solution to this problem, if you have any suggestions I would be very grateful, if I find a solution I will share it.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Sep 2023 09:51:15 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Looking-for-the-first-second-purchase-price/m-p/2120848#M91278</guid>
      <dc:creator>Ernest97</dc:creator>
      <dc:date>2023-09-21T09:51:15Z</dc:date>
    </item>
    <item>
      <title>Re: Looking for the first, second... purchase price</title>
      <link>https://community.qlik.com/t5/App-Development/Looking-for-the-first-second-purchase-price/m-p/2121452#M91313</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/198219"&gt;@Ernest97&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;First of all, join your two tables and create an unique table. After that, create a new calculated dimension (a flag):&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Purchasing_table:
LOAD *
Inline [
Date_purchase, Article_No, Quantity_purchase, Price_purchase
03.08.2023, 334443, 320, 221.5
07.07.2023, 334443, 960, 664.5
16.05.2023, 334443, 220, 156.38
09.05.2023, 334443, 100, 71.08
15.03.2023, 334443, 320, 227.47
08.03.2023, 334443, 320, 227.47
23.11.2022, 334443, 1600, 1107.5
16.08.2023, 124455, 1000, 150
13.07.2023, 124455, 500, 50
16.06.2023, 124455, 1000, 150
13.05.2023, 124455, 500, 50
];

Sales_table:
JOIN (Purchasing_table)
LOAD
*
Inline [
Date_sale, Article_No, Quantity_sale, Price_purchase_sale
19.06.2023, 334443, 600, 438.4
15.05.2023, 334443, 450, 190
16.06.2023, 124455, 200, 200
13.04.2023, 124455, 500, 100
];

NoConcatenate
MASTER_TAB:
LOAD 
IF([Date_sale] &amp;lt;= [Date_purchase],1,0) as Flag,
* 
RESIDENT Purchasing_table;

DROP TABLE Purchasing_table;&lt;/LI-CODE&gt;
&lt;P&gt;Normally, you'll be able to use this calculated like a flag in your Firstsortedvalue() expression (with a set analysis). Test it and comeback to us.&lt;/P&gt;
&lt;P&gt;Regards.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2023 14:56:10 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Looking-for-the-first-second-purchase-price/m-p/2121452#M91313</guid>
      <dc:creator>sbaro_bd</dc:creator>
      <dc:date>2023-09-22T14:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: Looking for the first, second... purchase price</title>
      <link>https://community.qlik.com/t5/App-Development/Looking-for-the-first-second-purchase-price/m-p/2121510#M91315</link>
      <description>&lt;P&gt;Completely resolved in script:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;SET DateFormat = 'DD.MM.YYYY';

Purchases:
NOCONCATENATE
LOAD *
INLINE [
        Date_purchase, Article_No, Quantity_purchase, Price_purchase
        03.08.2023,    334443,     320,               221.5
        07.07.2023,    334443,     960,               664.5
        16.05.2023,    334443,     220,               156.38
        09.05.2023,    334443,     100,               71.08
        15.03.2023,    334443,     320,               227.47
        08.03.2023,    334443,     320,               227.47
        23.11.2022,    334443,     1600,              1107.5
        16.08.2023,    124455,     1000,              150
        13.07.2023,    124455,     500,               50
        16.06.2023,    124455,     1000,              150
        13.05.2023,    124455,     500,               50
];

Sales:
NOCONCATENATE
LOAD *
INLINE [
        Date_sale,  Article_No, Quantity_sale, Price_purchase_sale
        19.06.2023, 334443,     600,           438.4
        15.05.2023, 334443,     450,           190
        16.06.2023, 124455,     200,           200
        13.04.2023, 124455,     500,           100
];

LastestPurchasesByItemSalesTemp1:
NOCONCATENATE
LOAD DISTINCT
	Article_No,
    Date_sale
RESIDENT
	Sales
;

INNER JOIN (LastestPurchasesByItemSalesTemp1)
LOAD *
RESIDENT Purchases
;

LastestPurchasesByItemSalesTemp2:
NOCONCATENATE
LOAD *,
	If(Article_No &amp;lt;&amp;gt; Peek(Article_No) OR Date_sale &amp;lt;&amp;gt; Peek(Date_sale), 1,
    	Peek(Purchase_No) + 1
    ) AS Purchase_No
RESIDENT
	LastestPurchasesByItemSalesTemp1
WHERE
	Date_purchase &amp;lt;= Date_sale
ORDER BY
	Article_No,
    Date_sale,
    Date_purchase DESC
;

DROP TABLE LastestPurchasesByItemSalesTemp1;

FOR i = 1 TO 3

	LEFT JOIN (Sales)
    LOAD
    	Article_No,
        Date_sale,
        Date_purchase     AS Date_purchase$(i),
        Quantity_purchase AS Quantity_purchase$(i),
        Price_purchase    AS Price_purchase$(i)
    RESIDENT
    	LastestPurchasesByItemSalesTemp2
    WHERE
    	Purchase_No = $(i)
    ;

NEXT

DROP TABLE LastestPurchasesByItemSalesTemp2;

LET i          =;
LET DateFormat =;&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 22 Sep 2023 18:09:11 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Looking-for-the-first-second-purchase-price/m-p/2121510#M91315</guid>
      <dc:creator>JGMDataAnalysis</dc:creator>
      <dc:date>2023-09-22T18:09:11Z</dc:date>
    </item>
    <item>
      <title>Re: Looking for the first, second... purchase price</title>
      <link>https://community.qlik.com/t5/App-Development/Looking-for-the-first-second-purchase-price/m-p/2121905#M91349</link>
      <description>&lt;P&gt;Thanks a lot&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I have run the script against a larger amount of data and the calculations have run successfully.&lt;/P&gt;
&lt;P&gt;I've worked quite a bit with Excel before and it does things a bit differently for such searches.&lt;/P&gt;
&lt;P&gt;Thanks again for your help &lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;, you've directed me to the logic of how such information can be combined.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Sep 2023 09:58:23 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Looking-for-the-first-second-purchase-price/m-p/2121905#M91349</guid>
      <dc:creator>Ernest97</dc:creator>
      <dc:date>2023-09-25T09:58:23Z</dc:date>
    </item>
  </channel>
</rss>

