<?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 total time duration for different tasks  with exclusion of overlapping hours in App Development</title>
    <link>https://community.qlik.com/t5/App-Development/Calculate-total-time-duration-for-different-tasks-with-exclusion/m-p/1809213#M65922</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/29453"&gt;@ahmed_qlik&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;This can be a little bit tricky, but you can get it by joining the periods table with itself by looking at which periods overlap (start time in one period between start and end times in the other). I have created an example of how to get that overlapped periods:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Times:
Load
	Task,
    Timestamp#(StartTime, 'DD-MM-YYYY hh:mm tt')	AS StartTime,
    Timestamp#(EndTime, 'DD-MM-YYYY hh:mm tt')		AS EndTime
Inline [
Task	StartTime	EndTime
Task1	18-05-2021 8:00 am	18-05-2021 12:00 pm
Task2	18-05-2021 8:00 am	18-05-2021 10:00 am
Task3	18-05-2021 12:30 pm	18-05-2021 1:00 pm
Task4	18-05-2021 2:00 pm	18-05-2021 3:00 pm
] (delimiter is '\t');


Pass1:
Load Distinct
	StartTime	AS Start1,
    EndTime		AS End1
Resident Times;
Outer Join
Load Distinct
	StartTime	AS Start2,
    EndTime		AS End2
Resident Times;

Drop Table Times;

Pass2:
Load
	Start1				AS Start,
    Max(End1, End2)		AS End
Resident Pass1
Where
	(Start2 &amp;gt;= Start1 And Start2 &amp;lt;= End1 And End2 &amp;gt; End1)		// Period 2 starts between Period 1 but ends later
	Or
    (Start1 = Start2 And End1 = End2)							// Same Period, so gets not overlapped periods
Group By Start1
;

Drop Table Pass1;

Periods:
Load Distinct
	'Period ' &amp;amp; RowNo()		AS Period,
	Timestamp(Start)		AS Start,
    Timestamp(End)			AS End,
    Timestamp(End - Start)	AS Duration
Resident Pass2
Order by Start;

Drop Table Pass2;

Tag Fields Start, End, Duration With $timestamp;&lt;/LI-CODE&gt;&lt;P&gt;The result is:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JuanGerardo_0-1621450149461.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/55321i26CE6DB9B21E986B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JuanGerardo_0-1621450149461.png" alt="JuanGerardo_0-1621450149461.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think it could be possible to add another pass for some casuistic not presented in the sample data, but it will be similar to Pass2.&lt;/P&gt;&lt;P&gt;JG&lt;/P&gt;</description>
    <pubDate>Wed, 19 May 2021 18:51:50 GMT</pubDate>
    <dc:creator>JuanGerardo</dc:creator>
    <dc:date>2021-05-19T18:51:50Z</dc:date>
    <item>
      <title>Calculate total time duration for different tasks  with exclusion of overlapping hours</title>
      <link>https://community.qlik.com/t5/App-Development/Calculate-total-time-duration-for-different-tasks-with-exclusion/m-p/1808526#M65851</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;I am looking to calculate total working hours&amp;nbsp; for a process which includes multiple tasks ( StartTime, EndTime) . Tricky part is that these tasks may go parallel also which means we will be having overlapping hours which we need to exclude.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For e.g&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Task&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Start Time(DD-MM-YYYY hh:mm:ss)&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;End Time(DD-MM-YYYY hh:mm:ss)&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Task1&lt;/TD&gt;&lt;TD&gt;18-05-2021 8:00 am&lt;/TD&gt;&lt;TD&gt;18-05-2021 12:00 pm&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Task2&lt;/TD&gt;&lt;TD&gt;18-05-2021 8:00 am&lt;/TD&gt;&lt;TD&gt;18-05-2021 10:00 am&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Task3&lt;/TD&gt;&lt;TD&gt;18-05-2021 12:30 pm&lt;/TD&gt;&lt;TD&gt;18-05-2021 1:00 pm&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Task4&lt;/TD&gt;&lt;TD&gt;18-05-2021 2:00 pm&lt;/TD&gt;&lt;TD&gt;18-05-2021 3:00 pm&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here total working hours are &lt;STRONG&gt;5 hours 30 minutes&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;( 8-12 = 4 hours;&lt;/P&gt;&lt;P&gt;12:30-1:00 = 30 minutes ;&lt;/P&gt;&lt;P&gt;2-3=1 hour)&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;EM&gt;Note: We have to avoid all those hours which are overlapping e.g Task2 --&amp;gt; (8-10 ) as these hours were already part of (8-12) in task 1&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Need a logic to calculate this &lt;STRONG&gt;total working hours&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.qlik.com/t5/user/viewprofilepage/user-id/6148" target="_blank" rel="noopener"&gt;@rwunderlich&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;A href="https://community.qlik.com/t5/user/viewprofilepage/user-id/46628" target="_blank" rel="noopener"&gt;@sunny_talwar&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.qlik.com/t5/user/viewprofilepage/user-id/28038" target="_blank" rel="noopener"&gt;@marcus_sommer&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.qlik.com/t5/user/viewprofilepage/user-id/22593" target="_blank" rel="noopener"&gt;@rubenmarin&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;@&lt;A href="https://community.qlik.com/t5/tag/wassenaar/tg-p/category-id/qlik-products" target="_blank" rel="noopener tag"&gt;wassenaar&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.qlik.com/t5/user/viewprofilepage/user-id/12989" target="_blank" rel="noopener"&gt;@Michael_Tarallo&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.qlik.com/t5/tag/christof%20schwarz/tg-p/category-id/qlik-products" target="_blank" rel="noopener tag"&gt;@christof schwarz&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 21:56:36 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Calculate-total-time-duration-for-different-tasks-with-exclusion/m-p/1808526#M65851</guid>
      <dc:creator>ahmed_qlik</dc:creator>
      <dc:date>2021-12-21T21:56:36Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate total time duration for different tasks  with exclusion of overlapping hours</title>
      <link>https://community.qlik.com/t5/App-Development/Calculate-total-time-duration-for-different-tasks-with-exclusion/m-p/1809213#M65922</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/29453"&gt;@ahmed_qlik&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;This can be a little bit tricky, but you can get it by joining the periods table with itself by looking at which periods overlap (start time in one period between start and end times in the other). I have created an example of how to get that overlapped periods:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Times:
Load
	Task,
    Timestamp#(StartTime, 'DD-MM-YYYY hh:mm tt')	AS StartTime,
    Timestamp#(EndTime, 'DD-MM-YYYY hh:mm tt')		AS EndTime
Inline [
Task	StartTime	EndTime
Task1	18-05-2021 8:00 am	18-05-2021 12:00 pm
Task2	18-05-2021 8:00 am	18-05-2021 10:00 am
Task3	18-05-2021 12:30 pm	18-05-2021 1:00 pm
Task4	18-05-2021 2:00 pm	18-05-2021 3:00 pm
] (delimiter is '\t');


Pass1:
Load Distinct
	StartTime	AS Start1,
    EndTime		AS End1
Resident Times;
Outer Join
Load Distinct
	StartTime	AS Start2,
    EndTime		AS End2
Resident Times;

Drop Table Times;

Pass2:
Load
	Start1				AS Start,
    Max(End1, End2)		AS End
Resident Pass1
Where
	(Start2 &amp;gt;= Start1 And Start2 &amp;lt;= End1 And End2 &amp;gt; End1)		// Period 2 starts between Period 1 but ends later
	Or
    (Start1 = Start2 And End1 = End2)							// Same Period, so gets not overlapped periods
Group By Start1
;

Drop Table Pass1;

Periods:
Load Distinct
	'Period ' &amp;amp; RowNo()		AS Period,
	Timestamp(Start)		AS Start,
    Timestamp(End)			AS End,
    Timestamp(End - Start)	AS Duration
Resident Pass2
Order by Start;

Drop Table Pass2;

Tag Fields Start, End, Duration With $timestamp;&lt;/LI-CODE&gt;&lt;P&gt;The result is:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JuanGerardo_0-1621450149461.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/55321i26CE6DB9B21E986B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JuanGerardo_0-1621450149461.png" alt="JuanGerardo_0-1621450149461.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think it could be possible to add another pass for some casuistic not presented in the sample data, but it will be similar to Pass2.&lt;/P&gt;&lt;P&gt;JG&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 18:51:50 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Calculate-total-time-duration-for-different-tasks-with-exclusion/m-p/1809213#M65922</guid>
      <dc:creator>JuanGerardo</dc:creator>
      <dc:date>2021-05-19T18:51:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate total time duration for different tasks  with exclusion of overlapping hours</title>
      <link>https://community.qlik.com/t5/App-Development/Calculate-total-time-duration-for-different-tasks-with-exclusion/m-p/1809264#M65927</link>
      <description>&lt;P&gt;Thanks&amp;nbsp; Juan , really appreciate your quick support.&lt;/P&gt;&lt;P&gt;However if there is a condition where only some part of the duration is overlapped like&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Task1, 18-05-2021 8:00 am, 18-05-2021 &lt;FONT color="#FF0000"&gt;12:00 pm&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Task7, 18-05-2021 10:00 am, 18-05-2021 &lt;FONT color="#FF0000"&gt;12:30 pm&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;if you notice, Task 7 is overlapped with Task1 but not completely ( 30 minutes ).&lt;/P&gt;&lt;P&gt;What we should do in that condition?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Wed, 19 May 2021 23:42:49 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Calculate-total-time-duration-for-different-tasks-with-exclusion/m-p/1809264#M65927</guid>
      <dc:creator>ahmed_qlik</dc:creator>
      <dc:date>2021-05-19T23:42:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate total time duration for different tasks  with exclusion of overlapping hours</title>
      <link>https://community.qlik.com/t5/App-Development/Calculate-total-time-duration-for-different-tasks-with-exclusion/m-p/1809348#M65939</link>
      <description>&lt;P&gt;I have modified it little bit ,&lt;/P&gt;&lt;P&gt;Pass1:&lt;BR /&gt;Load Distinct&lt;BR /&gt;//Port,&lt;BR /&gt;StartTime AS Start1,&lt;BR /&gt;EndTime AS End1&lt;BR /&gt;Resident Times;&lt;BR /&gt;Outer Join&lt;BR /&gt;Load Distinct&lt;BR /&gt;Port,&lt;BR /&gt;StartTime AS Start2,&lt;BR /&gt;EndTime AS End2&lt;BR /&gt;Resident Times;&lt;/P&gt;&lt;P&gt;Drop Table Times;&lt;/P&gt;&lt;P&gt;Pass2:&lt;BR /&gt;Load distinct&lt;BR /&gt;Port,&lt;BR /&gt;Start1 AS Start,&lt;BR /&gt;Max(End1, End2) AS End&lt;BR /&gt;Resident Pass1&lt;BR /&gt;Where&lt;BR /&gt;(Start2 &amp;gt;= Start1 And Start2 &amp;lt;= End1 And End2 &amp;gt; End1) // Period 2 starts between Period 1 but ends later&lt;BR /&gt;Or&lt;BR /&gt;(Start1 = Start2 And End1 = End2) // Same Period, so gets not overlapped periods&lt;BR /&gt;&lt;BR /&gt;Group By&lt;BR /&gt;Port,&lt;BR /&gt;Start1 order by&lt;BR /&gt;Port,&lt;BR /&gt;Start1 asc&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;Drop Table Pass1;&lt;BR /&gt;//exit SCRIPT;&lt;BR /&gt;Pass3:&lt;BR /&gt;load *,&lt;BR /&gt;if (Port=Previous(Port) ,if( Start &amp;gt; Previous(Start) and End &amp;gt;=Previous(End) and Start&amp;lt;Previous(End), Start-Previous(End)),0) as New_duration&lt;BR /&gt;Resident Pass2 ;&lt;BR /&gt;drop Table Pass2;&lt;/P&gt;&lt;P&gt;//exit SCRIPT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Periods:&lt;BR /&gt;Load Distinct&lt;BR /&gt;'Period ' &amp;amp; RowNo() AS Period,&lt;BR /&gt;Port,&lt;BR /&gt;Timestamp(Start) AS Start,&lt;BR /&gt;Timestamp(End) AS End,&lt;BR /&gt;Timestamp(End - Start) AS Duration,&lt;BR /&gt;New_duration&lt;BR /&gt;Resident Pass3&lt;BR /&gt;Order by Port,Start;&lt;/P&gt;&lt;P&gt;Drop Table Pass3;&lt;/P&gt;&lt;P&gt;//then adding duration and New_duration ; it seems working now but if there is a whole day difference between Start and end time, then it is not working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 09:03:32 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Calculate-total-time-duration-for-different-tasks-with-exclusion/m-p/1809348#M65939</guid>
      <dc:creator>ahmed_qlik</dc:creator>
      <dc:date>2021-05-20T09:03:32Z</dc:date>
    </item>
  </channel>
</rss>

