<?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 Calculating number of weeks between two YYYY_WW dates, in while statement generating lines in App Development</title>
    <link>https://community.qlik.com/t5/App-Development/Calculating-number-of-weeks-between-two-YYYY-WW-dates-in-while/m-p/1843898#M69834</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using a powerful code in my load statement to duplicate lines based on the number of weeks between two weeks number.&lt;BR /&gt;Using my script, the following inline table will show&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Load * Inline [
  Id,To_be_delivered_on_week, Delivered_on_week
  123456_10,202049,202052
  ];&lt;/LI-CODE&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="patricesalem_0-1633647586190.png" style="width: 752px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/63703i9EBCEF401CD37558/image-dimensions/752x77?v=v2" width="752" height="77" role="button" title="patricesalem_0-1633647586190.png" alt="patricesalem_0-1633647586190.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The result is exactly as expected...&lt;/P&gt;&lt;P&gt;Unfortunatelly the code only works for weeks in the same year.&lt;BR /&gt;The script will duplicate too many lines if we cover two years..., ie:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Load * Inline [
  Id,To_be_delivered_on_week, Delivered_on_week
  123456_10,202052,202102
  ];&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This portion of the code will duplicate 50 lines from 202052 to 202102 instead of 3 :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Load
    $(vMinDate)+ IterNo() as DueDate
Autogenerate 1
While 
    $(vMinDate) + IterNo() &amp;lt;= $(vMaxDate)
;&lt;/LI-CODE&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="patricesalem_1-1633648009010.png" style="width: 740px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/63704i79B5CA1CF03C53B8/image-dimensions/740x185?v=v2" width="740" height="185" role="button" title="patricesalem_1-1633648009010.png" alt="patricesalem_1-1633648009010.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I spent hours trying to find a solution with no luck...maybe some of you will have a good idea !&lt;BR /&gt;I will also have to handle the year made of 52 or 53 weeks in order to know when to restart the counter in the loop.&lt;BR /&gt;I have attached the QVF and here is code created originally by &lt;A title="Original code" href="https://stackoverflow.com/questions/68233814/loops-through-months-in-qlik-sense/69473213#69473213" target="_self"&gt;Stephen Stoichev&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;RawData:
Load * Inline [
  Id,To_be_delivered_on_week, Delivered_on_week
  123456_10,202052,202102
  ];


// Get min and max dates from [To_be_delivered_on_week] and [Delivered_on_week] fields
TempTable1:
Load 
    
    
    min([To_be_delivered_on_week]) as MinDate,
    max([To_be_delivered_on_week]) as MaxDate
Resident
    RawData

;

concatenate

Load
	
    min([Delivered_on_week]) as MinDate,
    max([Delivered_on_week]) as MaxDate
Resident
    RawData
   
;

// Get the overall min and max dates
NoConcatenate

TempTable2:
Load
  
  min(MinDate) as MinDate,
  max(MaxDate) as MaxDate
Resident
    TempTable1
  
;

Drop Table TempTable1;


let vMinDate = peek('MinDate');
let vMaxDate = peek('MaxDate');

join (RawData)

Calendar:
Load
    $(vMinDate)+ IterNo() as DueDate
Autogenerate 1
While 
    $(vMinDate) + IterNo() &amp;lt;= $(vMaxDate)
;

Drop Table TempTable2;

// Load resident modified RawData table and while loading we'll create new field
// This field will be used a flag and we'll filter on it at the end
// The logic in the field is:
//   if DueDate &amp;gt;= [To_be_delivered_on_week] and DueDate &amp;lt;= [Delivered_on_week] then set it to 1 else 0
// The final step is to keep only records with TempFlag == 1
NoConcatenate

RawData_Final:
Load 
  Id,
  [To_be_delivered_on_week],
  [Delivered_on_week],
  Current_week_of_analysis,
  nb_of_weeks_late
  
Where 
  TempFlag = 1  
;
Load 
  Id, 
  [To_be_delivered_on_week],
  [Delivered_on_week],
  DueDate as Current_week_of_analysis,
  DueDate - To_be_delivered_on_week as nb_of_weeks_late,
  if(DueDate &amp;gt;= [To_be_delivered_on_week] and DueDate &amp;lt;= [Delivered_on_week], 1, 0) as TempFlag
Resident
    RawData
;

Drop Table RawData;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot for your reading&lt;/P&gt;&lt;P&gt;pat&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 07 Oct 2021 23:18:46 GMT</pubDate>
    <dc:creator>patricesalem</dc:creator>
    <dc:date>2021-10-07T23:18:46Z</dc:date>
    <item>
      <title>Calculating number of weeks between two YYYY_WW dates, in while statement generating lines</title>
      <link>https://community.qlik.com/t5/App-Development/Calculating-number-of-weeks-between-two-YYYY-WW-dates-in-while/m-p/1843898#M69834</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using a powerful code in my load statement to duplicate lines based on the number of weeks between two weeks number.&lt;BR /&gt;Using my script, the following inline table will show&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Load * Inline [
  Id,To_be_delivered_on_week, Delivered_on_week
  123456_10,202049,202052
  ];&lt;/LI-CODE&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="patricesalem_0-1633647586190.png" style="width: 752px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/63703i9EBCEF401CD37558/image-dimensions/752x77?v=v2" width="752" height="77" role="button" title="patricesalem_0-1633647586190.png" alt="patricesalem_0-1633647586190.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The result is exactly as expected...&lt;/P&gt;&lt;P&gt;Unfortunatelly the code only works for weeks in the same year.&lt;BR /&gt;The script will duplicate too many lines if we cover two years..., ie:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Load * Inline [
  Id,To_be_delivered_on_week, Delivered_on_week
  123456_10,202052,202102
  ];&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This portion of the code will duplicate 50 lines from 202052 to 202102 instead of 3 :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Load
    $(vMinDate)+ IterNo() as DueDate
Autogenerate 1
While 
    $(vMinDate) + IterNo() &amp;lt;= $(vMaxDate)
;&lt;/LI-CODE&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="patricesalem_1-1633648009010.png" style="width: 740px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/63704i79B5CA1CF03C53B8/image-dimensions/740x185?v=v2" width="740" height="185" role="button" title="patricesalem_1-1633648009010.png" alt="patricesalem_1-1633648009010.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I spent hours trying to find a solution with no luck...maybe some of you will have a good idea !&lt;BR /&gt;I will also have to handle the year made of 52 or 53 weeks in order to know when to restart the counter in the loop.&lt;BR /&gt;I have attached the QVF and here is code created originally by &lt;A title="Original code" href="https://stackoverflow.com/questions/68233814/loops-through-months-in-qlik-sense/69473213#69473213" target="_self"&gt;Stephen Stoichev&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;RawData:
Load * Inline [
  Id,To_be_delivered_on_week, Delivered_on_week
  123456_10,202052,202102
  ];


// Get min and max dates from [To_be_delivered_on_week] and [Delivered_on_week] fields
TempTable1:
Load 
    
    
    min([To_be_delivered_on_week]) as MinDate,
    max([To_be_delivered_on_week]) as MaxDate
Resident
    RawData

;

concatenate

Load
	
    min([Delivered_on_week]) as MinDate,
    max([Delivered_on_week]) as MaxDate
Resident
    RawData
   
;

// Get the overall min and max dates
NoConcatenate

TempTable2:
Load
  
  min(MinDate) as MinDate,
  max(MaxDate) as MaxDate
Resident
    TempTable1
  
;

Drop Table TempTable1;


let vMinDate = peek('MinDate');
let vMaxDate = peek('MaxDate');

join (RawData)

Calendar:
Load
    $(vMinDate)+ IterNo() as DueDate
Autogenerate 1
While 
    $(vMinDate) + IterNo() &amp;lt;= $(vMaxDate)
;

Drop Table TempTable2;

// Load resident modified RawData table and while loading we'll create new field
// This field will be used a flag and we'll filter on it at the end
// The logic in the field is:
//   if DueDate &amp;gt;= [To_be_delivered_on_week] and DueDate &amp;lt;= [Delivered_on_week] then set it to 1 else 0
// The final step is to keep only records with TempFlag == 1
NoConcatenate

RawData_Final:
Load 
  Id,
  [To_be_delivered_on_week],
  [Delivered_on_week],
  Current_week_of_analysis,
  nb_of_weeks_late
  
Where 
  TempFlag = 1  
;
Load 
  Id, 
  [To_be_delivered_on_week],
  [Delivered_on_week],
  DueDate as Current_week_of_analysis,
  DueDate - To_be_delivered_on_week as nb_of_weeks_late,
  if(DueDate &amp;gt;= [To_be_delivered_on_week] and DueDate &amp;lt;= [Delivered_on_week], 1, 0) as TempFlag
Resident
    RawData
;

Drop Table RawData;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot for your reading&lt;/P&gt;&lt;P&gt;pat&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2021 23:18:46 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Calculating-number-of-weeks-between-two-YYYY-WW-dates-in-while/m-p/1843898#M69834</guid>
      <dc:creator>patricesalem</dc:creator>
      <dc:date>2021-10-07T23:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating number of weeks between two YYYY_WW dates, in while statement generating lines</title>
      <link>https://community.qlik.com/t5/App-Development/Calculating-number-of-weeks-between-two-YYYY-WW-dates-in-while/m-p/1844391#M69875</link>
      <description>&lt;P&gt;answer to my own question after a few hours of work. enjoy it for those looking for a solution&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;RawData:
Load * Inline [
  Id,To_be_delivered_on_week, Delivered_on_week
  123456_10,25/12/2020,15/01/2022
  ];


// Get min and max dates from [To_be_delivered_on_week] and [Delivered_on_week] fields
TempTable1:
Load 
    
    
    min([To_be_delivered_on_week]) as MinDate,
    max([To_be_delivered_on_week]) as MaxDate
Resident
    RawData

;

concatenate

Load
	
    min([Delivered_on_week]) as MinDate,
    max([Delivered_on_week]) as MaxDate
Resident
    RawData
   
;

// Get the overall min and max dates
NoConcatenate

TempTable2:
Load
  
  min(MinDate) as MinDate,
  max(MaxDate) as MaxDate
Resident
    TempTable1
  
;

Drop Table TempTable1;


let vMinDate = peek('MinDate');
let vMaxDate = peek('MaxDate');

join (RawData)

Calendar:
Load
    $(vMinDate)+ IterNo() as DueDate
Autogenerate 1
While 
    $(vMinDate) + IterNo() &amp;lt;= $(vMaxDate)
;

Drop Table TempTable2;

// Load resident modified RawData table and while loading we'll create new field
// This field will be used a flag and we'll filter on it at the end
// The logic in the field is:
//   if DueDate &amp;gt;= [To_be_delivered_on_week] and DueDate &amp;lt;= [Delivered_on_week] then set it to 1 else 0
// The final step is to keep only records with TempFlag == 1
NoConcatenate

RawData_Final:
Load 
  Id,
  To_be_delivered_on_week,
  Delivered_on_week,
  Current_week_of_analysis
  ,nb_of_weeks_late
  
Where 
  TempFlag = 1  
  
;
Load 
  min(1) as Dummy,
  Id,
  year(To_be_delivered_on_week) &amp;amp; num(week(To_be_delivered_on_week),'00') as To_be_delivered_on_week,
  year(Delivered_on_week)&amp;amp;num(week(Delivered_on_week),'00') as Delivered_on_week,
  year(DueDate)&amp;amp;num(week(DueDate),'00') as Current_week_of_analysis,
  
  (WeekEnd(DueDate)-WeekEnd(To_be_delivered_on_week))/7 as nb_of_weeks_late, 
  
  if(year(DueDate)&amp;amp;num(week(DueDate),'00') &amp;gt;= year(To_be_delivered_on_week) &amp;amp; num(week(To_be_delivered_on_week),'00')
  and  year(DueDate)&amp;amp;num(week(DueDate),'00') &amp;lt;= year(Delivered_on_week)&amp;amp;num(week(Delivered_on_week),'00'), 1, 0) as TempFlag

Resident
    RawData
    group by Id, 
    year(To_be_delivered_on_week) &amp;amp; num(week(To_be_delivered_on_week),'00'),
    year(Delivered_on_week)&amp;amp;num(week(Delivered_on_week),'00'),
    year(DueDate)&amp;amp;num(week(DueDate),'00'),
    
    (WeekEnd(DueDate)-WeekEnd(To_be_delivered_on_week))/7,
    
  
    if(year(DueDate)&amp;amp;num(week(DueDate),'00') &amp;gt;= year(To_be_delivered_on_week) &amp;amp; num(week(To_be_delivered_on_week),'00')
  and  year(DueDate)&amp;amp;num(week(DueDate),'00') &amp;lt;= year(Delivered_on_week)&amp;amp;num(week(Delivered_on_week),'00'), 1, 0)
;

Drop Table RawData;&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 08 Oct 2021 19:57:03 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Calculating-number-of-weeks-between-two-YYYY-WW-dates-in-while/m-p/1844391#M69875</guid>
      <dc:creator>patricesalem</dc:creator>
      <dc:date>2021-10-08T19:57:03Z</dc:date>
    </item>
  </channel>
</rss>

