<?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 Time and Loops in Script in QlikView</title>
    <link>https://community.qlik.com/t5/QlikView/Time-and-Loops-in-Script/m-p/1579654#M598495</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;Looking for some scripting help.&amp;nbsp; I have the following fields in a table (100 records)&lt;/P&gt;&lt;P&gt;OriginalTable:&lt;/P&gt;&lt;P&gt;ID | Date | StartTime | EndTime&lt;/P&gt;&lt;P&gt;1&amp;nbsp; | 4/1/2019 | 14:30:00 | 16:30:00&lt;/P&gt;&lt;P&gt;2&amp;nbsp; | 4/1/2019 | 15:25:10 | 15:55:27&lt;/P&gt;&lt;P&gt;3&amp;nbsp; | 4/1/2019 | 14:12:21 | 17:20:27&lt;/P&gt;&lt;P&gt;I need to loop through this table and calculate what percentage of each hour is occupied between StartTime and EndTime.&lt;/P&gt;&lt;P&gt;So for example the ID 1 in the original table will have 3 rows in the new table&lt;/P&gt;&lt;P&gt;NewTable:&lt;/P&gt;&lt;P&gt;ID | NewID | Hour | %Occupied&lt;/P&gt;&lt;P&gt;1 | 1-1 | 14 |&amp;nbsp; 50%&lt;/P&gt;&lt;P&gt;1 | 1-2 | 15 |&amp;nbsp; 100%&lt;/P&gt;&lt;P&gt;1 | 1-3 | 16 |&amp;nbsp; 50%&lt;/P&gt;&lt;P&gt;Any help would be appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 16 Nov 2024 03:35:49 GMT</pubDate>
    <dc:creator>JT1</dc:creator>
    <dc:date>2024-11-16T03:35:49Z</dc:date>
    <item>
      <title>Time and Loops in Script</title>
      <link>https://community.qlik.com/t5/QlikView/Time-and-Loops-in-Script/m-p/1579654#M598495</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;Looking for some scripting help.&amp;nbsp; I have the following fields in a table (100 records)&lt;/P&gt;&lt;P&gt;OriginalTable:&lt;/P&gt;&lt;P&gt;ID | Date | StartTime | EndTime&lt;/P&gt;&lt;P&gt;1&amp;nbsp; | 4/1/2019 | 14:30:00 | 16:30:00&lt;/P&gt;&lt;P&gt;2&amp;nbsp; | 4/1/2019 | 15:25:10 | 15:55:27&lt;/P&gt;&lt;P&gt;3&amp;nbsp; | 4/1/2019 | 14:12:21 | 17:20:27&lt;/P&gt;&lt;P&gt;I need to loop through this table and calculate what percentage of each hour is occupied between StartTime and EndTime.&lt;/P&gt;&lt;P&gt;So for example the ID 1 in the original table will have 3 rows in the new table&lt;/P&gt;&lt;P&gt;NewTable:&lt;/P&gt;&lt;P&gt;ID | NewID | Hour | %Occupied&lt;/P&gt;&lt;P&gt;1 | 1-1 | 14 |&amp;nbsp; 50%&lt;/P&gt;&lt;P&gt;1 | 1-2 | 15 |&amp;nbsp; 100%&lt;/P&gt;&lt;P&gt;1 | 1-3 | 16 |&amp;nbsp; 50%&lt;/P&gt;&lt;P&gt;Any help would be appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Nov 2024 03:35:49 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Time-and-Loops-in-Script/m-p/1579654#M598495</guid>
      <dc:creator>JT1</dc:creator>
      <dc:date>2024-11-16T03:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: Time and Loops in Script</title>
      <link>https://community.qlik.com/t5/QlikView/Time-and-Loops-in-Script/m-p/1579809#M598496</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I do like an interval match problem … but I thought this was going to be easier than this &lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;. Try the below;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;data:
Load
	Timestamp(Date+StartTime-(1/24)) as StartDateTime,
	Timestamp(Date+EndTime) as EndDateTime,
	*
;
LOAD * INLINE [
ID , Date , StartTime , EndTime
1  , 4/1/2019, 14:30:00 , 16:30:00
2  , 4/1/2019 , 15:25:10 , 15:55:27
3 , 4/1/2019, 14:12:21 , 17:20:27
];


intervals:
Load
	IterNo() AS Interval,
     TimeStamp(DMin + ((IterNo()-2)/24)) as Hour
While DMin+((IterNo()-2)/24) &amp;lt;= DMax 
;
Load
	Min(RoundedDateStartTime) AS DMin,
	Max(RoundedDateEndTime)  AS DMax;
Load
	 Timestamp(Date+floor(StartTime, 1/24)) AS RoundedDateStartTime,
	 Timestamp(Date+floor(EndTime, 1/24)) AS RoundedDateEndTime
Resident data;


join IntervalMatch(Hour)
Load 
	StartDateTime,
	EndDateTime
resident data;

left join (data)
Load
	*
resident intervals;

drop table intervals;

data_final:
NoConcatenate
LOAD
	ID,
	ID2,
	Date,
	StartTime,
	EndTime,
	Hour,
	If(StartDateTime+(1/24)&amp;lt;Hour AND EndDateTime&amp;gt;Hour+(1/24),
		1,
		If(ID2=1,
			If(EndDateTime&amp;gt;Hour+(1/24),
				24*(Hour-StartDateTime),
				24*(EndDateTime-(StartDateTime+(1/24)))
				),				
			24*(EndDateTime-Hour)
			)
		) AS Proportion;
Load
	If(RowNo()=1,
		1,
		If(Peek('ID')=ID,
			Peek('ID2')+1,
			1
			)
		) as ID2,
	*	
Resident data
Order By ID, Hour;

drop table data;&lt;/LI-CODE&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Chris.&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2019 07:53:01 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Time-and-Loops-in-Script/m-p/1579809#M598496</guid>
      <dc:creator>chrismarlow</dc:creator>
      <dc:date>2019-05-14T07:53:01Z</dc:date>
    </item>
    <item>
      <title>Re: Time and Loops in Script</title>
      <link>https://community.qlik.com/t5/QlikView/Time-and-Loops-in-Script/m-p/1582372#M598497</link>
      <description>&lt;P&gt;Thanks this works perfectly !&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2019 17:33:48 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Time-and-Loops-in-Script/m-p/1582372#M598497</guid>
      <dc:creator>JT1</dc:creator>
      <dc:date>2019-05-20T17:33:48Z</dc:date>
    </item>
  </channel>
</rss>

