<?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: Trouble with Complex Joins and If Statements in Data Load script in App Development</title>
    <link>https://community.qlik.com/t5/App-Development/Trouble-with-Complex-Joins-and-If-Statements-in-Data-Load-script/m-p/2512365#M105201</link>
    <description>&lt;P&gt;I have been using GitHub Copilot AI assistant with this project to get the bulk of the script, it would suggest a solution, then I'd try it, and if it didn't work I'd go back.&amp;nbsp; With&amp;nbsp;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/25001"&gt;@Vegar&lt;/a&gt;&amp;nbsp;script it was able to modify it slightly to get the correct answer, so I'll Accept as Solution but also wanted to share the working script.&amp;nbsp; The main difference was the order by clause :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;// Step 1: Load the main data and create a unique identifier&lt;BR /&gt;Main_Data:&lt;BR /&gt;LOAD&lt;BR /&gt;*,&lt;BR /&gt;AutoNumberHash128(HC_EID, HC_EMAIL, HC_BAND, HC_LEVEL_5) AS HC_REC_UNIQUE_IDENTIFIER&lt;BR /&gt;FROM [$(vMainPath)QVD_files/TPD_Headcount_Main.qvd] (qvd);&lt;/P&gt;&lt;P&gt;// Step 2: Calculate IN_PREV&lt;BR /&gt;PREV_ADDED:&lt;BR /&gt;LOAD *,&lt;BR /&gt;IF(&lt;BR /&gt;Peek('HC_EID') = HC_EID,&lt;BR /&gt;IF(Peek('HC_REC_UNIQUE_IDENTIFIER') = HC_REC_UNIQUE_IDENTIFIER, 1, 2),&lt;BR /&gt;0&lt;BR /&gt;) AS IN_PREV&lt;BR /&gt;RESIDENT Main_Data&lt;BR /&gt;ORDER BY HC_EID, DATA_LOAD_DATE ASC; // Sort by HC_EID and ascending DATA_LOAD_DATE&lt;/P&gt;&lt;P&gt;DROP TABLE Main_Data; // Drop the original table&lt;/P&gt;&lt;P&gt;// Step 3: Calculate IN_NEXT&lt;BR /&gt;NEXT_ADDED:&lt;BR /&gt;LOAD *,&lt;BR /&gt;IF(&lt;BR /&gt;Peek('HC_EID') = HC_EID,&lt;BR /&gt;IF(Peek('HC_REC_UNIQUE_IDENTIFIER') = HC_REC_UNIQUE_IDENTIFIER, 1, 2),&lt;BR /&gt;0&lt;BR /&gt;) AS IN_NEXT&lt;BR /&gt;RESIDENT PREV_ADDED&lt;BR /&gt;ORDER BY HC_EID, DATA_LOAD_DATE DESC; // Sort by HC_EID and descending DATA_LOAD_DATE&lt;/P&gt;&lt;P&gt;DROP TABLE PREV_ADDED; // Drop the intermediate table&lt;/P&gt;&lt;P&gt;// Step 4: Cleanup&lt;BR /&gt;DROP FIELD HC_REC_UNIQUE_IDENTIFIER;&lt;/P&gt;</description>
    <pubDate>Mon, 31 Mar 2025 13:57:53 GMT</pubDate>
    <dc:creator>shep_work</dc:creator>
    <dc:date>2025-03-31T13:57:53Z</dc:date>
    <item>
      <title>Trouble with Complex Joins and If Statements in Data Load script</title>
      <link>https://community.qlik.com/t5/App-Development/Trouble-with-Complex-Joins-and-If-Statements-in-Data-Load-script/m-p/2512154#M105183</link>
      <description>&lt;P&gt;Hello experts,&lt;/P&gt;&lt;P&gt;I have been trying to resolve an issue for over a week now and I think I'm just too close to see the issue.&amp;nbsp; I have a table (below) that has HC_EID, HC_EMAIL, HC_BAND, HC_LEVEL_5 and DATA_LOAD_DATE in it.&amp;nbsp; Each record is for a current employee and we are tracking weekly when they are in the data set and when they are not.&amp;nbsp; So we have IN_PREV, meaning they were in the previous date's load. We also have IN_NEXT, meaning they were in the following date's data load in relation to the DATA_LOAD_DATE on that record.&amp;nbsp; My syntax works as in it runs, but everyone ends up with only 0s in the IN_NEXT and no errors in the IN_PREV when I know there should be.&lt;/P&gt;&lt;P&gt;IN_PREV Logic:&lt;BR /&gt;For a given record for a given data_load_date&lt;BR /&gt;- if the record has a matching HC_EID, HC_EMAIL, HC_BAND, and HC_LEVEL_5 as the previous data_load_date then it should be a 1 (true)&lt;BR /&gt;- if the record has only a matching HC_EID as the previous data_load_date then it should be a 2 (err)&lt;BR /&gt;- if the record does not match any of the HC_EID, HC_EMAIL, HC_BAND, and HC_LEVEL_5 as the previous data_load_date then it should be a 0 (false)&lt;/P&gt;&lt;P&gt;IN_NEXT Logic:&lt;BR /&gt;For a given record for a given data_load_date&lt;BR /&gt;- if the record has a matching HC_EID, HC_EMAIL, HC_BAND, and HC_LEVEL_5 as the next data_load_date then it should be a 1 (true)&lt;BR /&gt;- if the record has only a matching HC_EID as the next data_load_date then it should be a 2 (err)&lt;BR /&gt;- if the record does not match any of the HC_EID, HC_EMAIL, HC_BAND, and HC_LEVEL_5 as the next data_load_date then it should be a 0 (false)&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;HC_EID HC_EMAIL HC_BAND HC_LEVEL_5 DATA_LOAD_DATE&lt;BR /&gt;123456 you@you.com 5 John Smith 3/3/2025&lt;BR /&gt;123456 me@me.com 7 Jane Doe 3/10/2025&lt;BR /&gt;123456 you@you.com 5 John Smith 3/17/2025&lt;BR /&gt;123456 you@you.com 5 John Smith 3/24/2025&lt;/P&gt;&lt;P&gt;123456 results should be:&lt;/P&gt;&lt;P&gt;for 03/03/2025 - IN_NEXT: 1 and IN_PREV: 0&lt;/P&gt;&lt;P&gt;for 03/10/2025 - IN_NEXT: 2 and IN_PREV: 2&lt;/P&gt;&lt;P&gt;for 03/17/2025 - IN_NEXT: 1 and IN_PREV: 2&lt;/P&gt;&lt;P&gt;for 03/24/2025 - IN_NEXT: 0 and IN_PREV: 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;// Load the raw_data QVD&lt;BR /&gt;Main_Data:&lt;BR /&gt;LOAD&lt;BR /&gt;*&lt;BR /&gt;FROM [$(vMainPath)QVD_files/TPD_Headcount_Main.qvd] (qvd);&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;// Create a distinct list of DATA_LOAD_DATE values and sort them&lt;BR /&gt;Dates:&lt;BR /&gt;LOAD DISTINCT&lt;BR /&gt;DATA_LOAD_DATE&lt;BR /&gt;RESIDENT Main_Data&lt;BR /&gt;ORDER BY DATA_LOAD_DATE ASC;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;// Add PreviousDate and NextDate using Peek()&lt;BR /&gt;DatesWithPrevNext:&lt;BR /&gt;LOAD&lt;BR /&gt;DATA_LOAD_DATE,&lt;BR /&gt;Peek('DATA_LOAD_DATE', -1) AS PreviousDate,&lt;BR /&gt;Peek('DATA_LOAD_DATE', 1) AS NextDate&lt;BR /&gt;RESIDENT Dates;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;// Create RawDataWithDates table&lt;BR /&gt;RawDataWithDates:&lt;BR /&gt;NOCONCATENATE&lt;BR /&gt;LOAD&lt;BR /&gt;*&lt;BR /&gt;RESIDENT Main_Data;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;// Perform LEFT JOIN to add PreviousDate and NextDate to RawDataWithDates&lt;BR /&gt;LEFT JOIN (RawDataWithDates)&lt;BR /&gt;LOAD&lt;BR /&gt;DATA_LOAD_DATE,&lt;BR /&gt;PreviousDate,&lt;BR /&gt;NextDate&lt;BR /&gt;RESIDENT DatesWithPrevNext;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;// Load the previous week's data for comparison&lt;BR /&gt;PreviousWeekData:&lt;BR /&gt;LOAD&lt;BR /&gt;HC_EID AS PW_ID,&lt;BR /&gt;HC_EMAIL AS PW_EMAIL,&lt;BR /&gt;HC_BAND AS PW_BAND,&lt;BR /&gt;HC_LEVEL_5 AS PW_LEVEL_5,&lt;BR /&gt;DATA_LOAD_DATE AS PreviousDate&lt;BR /&gt;RESIDENT RawDataWithDates&lt;BR /&gt;WHERE NOT ISNULL(PreviousDate);&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;// Load the next week's data for comparison&lt;BR /&gt;NextWeekData:&lt;BR /&gt;LOAD&lt;BR /&gt;HC_EID AS NW_ID,&lt;BR /&gt;HC_EMAIL AS NW_EMAIL,&lt;BR /&gt;HC_BAND AS NW_BAND,&lt;BR /&gt;HC_LEVEL_5 AS NW_LEVEL_5,&lt;BR /&gt;DATA_LOAD_DATE AS NextDate&lt;BR /&gt;RESIDENT RawDataWithDates&lt;BR /&gt;WHERE NOT ISNULL(NextDate);&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;// Join the previous week's data with the current data to calculate IN_PREV&lt;BR /&gt;LEFT JOIN (RawDataWithDates)&lt;BR /&gt;LOAD&lt;BR /&gt;PW_ID,&lt;BR /&gt;PW_EMAIL,&lt;BR /&gt;PW_BAND,&lt;BR /&gt;PW_LEVEL_5,&lt;BR /&gt;PreviousDate,&lt;BR /&gt;IF(&lt;BR /&gt;'PreviousWeekData.PW_ID' = 'RawDataWithDates.HC_EID' AND&lt;BR /&gt;'PreviousWeekData.PW_EMAIL' = 'RawDataWithDates.HC_EMAIL' AND&lt;BR /&gt;'PreviousWeekData.PW_BAND' = 'RawDataWithDates.HC_BAND' AND&lt;BR /&gt;'PreviousWeekData.PW_LEVEL_5' = 'RawDataWithDates.HC_LEVEL_5',&lt;BR /&gt;1, 0&lt;BR /&gt;) AS FoundInPrevFull,&lt;BR /&gt;IF(&lt;BR /&gt;'PreviousWeekData.PW_ID' = 'RawDataWithDates.HC_EID',&lt;BR /&gt;1, 0&lt;BR /&gt;) AS FoundInPrevID&lt;BR /&gt;RESIDENT PreviousWeekData;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;// Join the next week's data with the current data to calculate IN_NEXT&lt;BR /&gt;LEFT JOIN (RawDataWithDates)&lt;BR /&gt;LOAD&lt;BR /&gt;NW_ID,&lt;BR /&gt;NW_EMAIL,&lt;BR /&gt;NW_BAND,&lt;BR /&gt;NW_LEVEL_5,&lt;BR /&gt;NextDate,&lt;BR /&gt;IF(&lt;BR /&gt;'NextWeekData.NW_ID' = 'RawDataWithDates.HC_EID' AND&lt;BR /&gt;'NextWeekData.NW_EMAIL' = 'RawDataWithDates.HC_EMAIL' AND&lt;BR /&gt;'NextWeekData.NW_BAND' = 'RawDataWithDates.HC_BAND' AND&lt;BR /&gt;'NextWeekData.NW_LEVEL_5' = 'RawDataWithDates.HC_LEVEL_5',&lt;BR /&gt;1, 0&lt;BR /&gt;) AS FoundInNextFull,&lt;BR /&gt;IF(&lt;BR /&gt;'NextWeekData.NW_ID' = 'RawDataWithDates.HC_EID',&lt;BR /&gt;1, 0&lt;BR /&gt;) AS FoundInNextID&lt;BR /&gt;RESIDENT NextWeekData;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;// Calculate the IN_PREV and IN_NEXT columns&lt;BR /&gt;FinalData:&lt;BR /&gt;LOAD&lt;BR /&gt;*,&lt;BR /&gt;IF(FoundInPrevFull = 0, 1,&lt;BR /&gt;IF(FoundInPrevID = 0, 2, 0)) AS IN_PREV,&lt;BR /&gt;IF(FoundInNextFull = 0, 1,&lt;BR /&gt;IF(FoundInNextID = 0, 2, 0)) AS IN_NEXT&lt;BR /&gt;RESIDENT RawDataWithDates;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;// Drop intermediate tables&lt;BR /&gt;DROP TABLE Main_Data;&lt;BR /&gt;DROP TABLE Dates;&lt;BR /&gt;DROP TABLE DatesWithPrevNext;&lt;BR /&gt;DROP TABLE RawDataWithDates;&lt;BR /&gt;DROP TABLE PreviousWeekData;&lt;BR /&gt;DROP TABLE NextWeekData;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Mar 2025 21:21:03 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Trouble-with-Complex-Joins-and-If-Statements-in-Data-Load-script/m-p/2512154#M105183</guid>
      <dc:creator>shep_work</dc:creator>
      <dc:date>2025-03-28T21:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with Complex Joins and If Statements in Data Load script</title>
      <link>https://community.qlik.com/t5/App-Development/Trouble-with-Complex-Joins-and-If-Statements-in-Data-Load-script/m-p/2512158#M105184</link>
      <description>&lt;P&gt;ps- I tried several times to add a proper table and got the 'bad html was removed' issue along with not being able to format the code; not sure why but I have no 'select html option in the header' per the instructions.&lt;/P&gt;</description>
      <pubDate>Mon, 31 Mar 2025 13:30:56 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Trouble-with-Complex-Joins-and-If-Statements-in-Data-Load-script/m-p/2512158#M105184</guid>
      <dc:creator>shep_work</dc:creator>
      <dc:date>2025-03-31T13:30:56Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with Complex Joins and If Statements in Data Load script</title>
      <link>https://community.qlik.com/t5/App-Development/Trouble-with-Complex-Joins-and-If-Statements-in-Data-Load-script/m-p/2512185#M105189</link>
      <description>&lt;P&gt;What if you just read the datset twice with different sort order and do conditional peeks . See my suggestion for your sample dataset below.&lt;/P&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;raw:&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;LOAD *,&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;AutoNumberHash256(HC_EID, HC_EMAIL, HC_BAND,&amp;nbsp; HC_LEVEL_5) as HC_REC_UNIQUE_IDENTIFIER&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;inline [&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;HC_EID,HC_EMAIL,HC_BAND,HC_LEVEL_5,DATA_LOAD_DATE&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;123456,you@you.com,5,John Smith,3/3/2025&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;123456,me@me.com,7,Jane Doe,3/10/2025&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;123456,you@you.com,5,John Smith,3/17/2025&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;123456,you@you.com,5,John Smith,3/24/2025&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;];&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;PREV_ADDED:&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;Load&amp;nbsp;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;*,&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;IF( Peek('HC_EID') =HC_EID,&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; if( Peek('HC_REC_UNIQUE_IDENTIFIER') =HC_REC_UNIQUE_IDENTIFIER, 1, 2),&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0) as IN_NEXT&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;Resident raw order by DATA_LOAD_DATE desc;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;drop table raw;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;NEXT_ADDED:&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;Load&amp;nbsp;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;*,&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;IF( Peek('HC_EID') =HC_EID,&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; if(Peek('HC_REC_UNIQUE_IDENTIFIER') =HC_REC_UNIQUE_IDENTIFIER,1,2),&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0) as IN_PREV&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;resident PREV_ADDED order by DATA_LOAD_DATE asc;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;drop table PREV_ADDED;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new,courier"&gt;drop field HC_REC_UNIQUE_IDENTIFIER;&lt;/FONT&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 29 Mar 2025 12:12:28 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Trouble-with-Complex-Joins-and-If-Statements-in-Data-Load-script/m-p/2512185#M105189</guid>
      <dc:creator>Vegar</dc:creator>
      <dc:date>2025-03-29T12:12:28Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with Complex Joins and If Statements in Data Load script</title>
      <link>https://community.qlik.com/t5/App-Development/Trouble-with-Complex-Joins-and-If-Statements-in-Data-Load-script/m-p/2512348#M105200</link>
      <description>&lt;P&gt;Thank you sir, but the result of this is all the IN_NEXT and IN_PREV come out as zeros.&lt;/P&gt;</description>
      <pubDate>Mon, 31 Mar 2025 13:30:11 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Trouble-with-Complex-Joins-and-If-Statements-in-Data-Load-script/m-p/2512348#M105200</guid>
      <dc:creator>shep_work</dc:creator>
      <dc:date>2025-03-31T13:30:11Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with Complex Joins and If Statements in Data Load script</title>
      <link>https://community.qlik.com/t5/App-Development/Trouble-with-Complex-Joins-and-If-Statements-in-Data-Load-script/m-p/2512365#M105201</link>
      <description>&lt;P&gt;I have been using GitHub Copilot AI assistant with this project to get the bulk of the script, it would suggest a solution, then I'd try it, and if it didn't work I'd go back.&amp;nbsp; With&amp;nbsp;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/25001"&gt;@Vegar&lt;/a&gt;&amp;nbsp;script it was able to modify it slightly to get the correct answer, so I'll Accept as Solution but also wanted to share the working script.&amp;nbsp; The main difference was the order by clause :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;// Step 1: Load the main data and create a unique identifier&lt;BR /&gt;Main_Data:&lt;BR /&gt;LOAD&lt;BR /&gt;*,&lt;BR /&gt;AutoNumberHash128(HC_EID, HC_EMAIL, HC_BAND, HC_LEVEL_5) AS HC_REC_UNIQUE_IDENTIFIER&lt;BR /&gt;FROM [$(vMainPath)QVD_files/TPD_Headcount_Main.qvd] (qvd);&lt;/P&gt;&lt;P&gt;// Step 2: Calculate IN_PREV&lt;BR /&gt;PREV_ADDED:&lt;BR /&gt;LOAD *,&lt;BR /&gt;IF(&lt;BR /&gt;Peek('HC_EID') = HC_EID,&lt;BR /&gt;IF(Peek('HC_REC_UNIQUE_IDENTIFIER') = HC_REC_UNIQUE_IDENTIFIER, 1, 2),&lt;BR /&gt;0&lt;BR /&gt;) AS IN_PREV&lt;BR /&gt;RESIDENT Main_Data&lt;BR /&gt;ORDER BY HC_EID, DATA_LOAD_DATE ASC; // Sort by HC_EID and ascending DATA_LOAD_DATE&lt;/P&gt;&lt;P&gt;DROP TABLE Main_Data; // Drop the original table&lt;/P&gt;&lt;P&gt;// Step 3: Calculate IN_NEXT&lt;BR /&gt;NEXT_ADDED:&lt;BR /&gt;LOAD *,&lt;BR /&gt;IF(&lt;BR /&gt;Peek('HC_EID') = HC_EID,&lt;BR /&gt;IF(Peek('HC_REC_UNIQUE_IDENTIFIER') = HC_REC_UNIQUE_IDENTIFIER, 1, 2),&lt;BR /&gt;0&lt;BR /&gt;) AS IN_NEXT&lt;BR /&gt;RESIDENT PREV_ADDED&lt;BR /&gt;ORDER BY HC_EID, DATA_LOAD_DATE DESC; // Sort by HC_EID and descending DATA_LOAD_DATE&lt;/P&gt;&lt;P&gt;DROP TABLE PREV_ADDED; // Drop the intermediate table&lt;/P&gt;&lt;P&gt;// Step 4: Cleanup&lt;BR /&gt;DROP FIELD HC_REC_UNIQUE_IDENTIFIER;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Mar 2025 13:57:53 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Trouble-with-Complex-Joins-and-If-Statements-in-Data-Load-script/m-p/2512365#M105201</guid>
      <dc:creator>shep_work</dc:creator>
      <dc:date>2025-03-31T13:57:53Z</dc:date>
    </item>
  </channel>
</rss>

