Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Match two time intervals

Hi,

I need to match two time intervals like in the below example. I found this example but looks too complicated. Any idea how to get it easier? I've tried with master calendar but didn't get me to the needed results.

Intervals1:

StartDate1EndDate1
2015-05-012015-05-31

Intervals2:

StartDate2EndDate2
2015-04-152015-05-15

Results:

StartDate1EndDate1
2015-04-152015-04-30
2015-05-012015-05-15
2015-05-162015-05-31

Regards,

Przemek

1 Solution

Accepted Solutions
Gysbert_Wassenaar

It would look quite strange since 2015-02-31 is not a valid date.

If you want to add Id's to the mix it will look like this:

Intervals:

LOAD

  Id,

  Date#(StartDate,'YYYY-MM-DD') as StartDate,

  Date#(EndDate,'YYYY-MM-DD') as EndDate 

INLINE [

Id, StartDate, EndDate

1, 2015-05-01, 2015-05-31

1, 2015-04-15, 2015-05-15

2, 2015-02-01, 2015-02-28

2, 2015-01-15, 2015-02-15

];

TempDates:

LOAD Id, StartDate as Date, 'Start' as Type Resident Intervals;

LOAD Id, EndDate as Date, 'End' as Type Resident Intervals;

NewIntervals:

LOAD * WHERE End>0;

LOAD

  Id,

  if(Type = 'Start', Date, If(Id = Previous(Id), Date(Date+1))) as Start,

  if(Id = Previous(Id),

  if(Type = 'Start' and Previous(Type) = 'Start',

  Date(Previous(Date)-1),

  Date(Previous(Date)

  )

  )

  ) as End

Resident TempDates Order By Id, Date desc;

Drop Table TempDates;


talk is cheap, supply exceeds demand

View solution in original post

12 Replies
swuehl
MVP
MVP

Which example?

And could you comment on how you retrieve the resulting records from your input records?

Digvijay_Singh

See if this sample can help, your logic of adding or subtracting 1 sometimes in start date or end date is complicated.

I tried to first combined all the dates in one field and then created interval through script.

Capture.JPG

T1:

Load Date(Date#(StartDate1,'YYYY-MM-DD'),'YYYY-MM-DD') as Date

inline [

StartDate1, EndDate1

2015-05-01,2015-05-31 ];

Load Date(Date#(EndDate1,'YYYY-MM-DD'),'YYYY-MM-DD') as Date

inline [

StartDate1, EndDate1

2015-05-01,2015-05-31 ];

Load Date(Date#(StartDate1,'YYYY-MM-DD'),'YYYY-MM-DD') as Date

inline [

StartDate1, EndDate1

2015-05-01,2015-05-31 ];

Load Date(Date#(StartDate2,'YYYY-MM-DD'),'YYYY-MM-DD') as Date

inline [

StartDate2, EndDate2

2015-04-15,2015-05-15 ];

Load Date(Date#(EndDate2,'YYYY-MM-DD'),'YYYY-MM-DD') as Date

inline [

StartDate2, EndDate2

2015-04-15,2015-05-15 ];

Let TotalRows = NoOfRows('T1');

For i = 1 to $(TotalRows)-2

  T2:

  Load $(i) as SNO,

  if($(i)=1,min(Date(Date#(Date,'YYYY-MM-DD'),'YYYY-MM-DD'),$(i)),

    min(Date(Date#(Date,'YYYY-MM-DD')+1,'YYYY-MM-DD'),$(i))) as StartDate,

  min(Date(Date#(Date,'YYYY-MM-DD'),'YYYY-MM-DD'),$(i)+1) as EndDate

  Resident T1

  Order By Date;

NEXT;

Not applicable
Author

Hi Marco,

so, yeas - my case is Henric's type "Two interval tables mapped against a common dimension ID and a common time line". The actual datamodel includes also id to be matched. Could you please give me some more hint on how to create the bridge tables and subintervals?

Intervals1:

IdStartDate1EndDate1
12015-05-012015-05-31

Intervals2:

IdStartDate2EndDate2
12015-04-152015-05-15

Results:

IdStartDate1EndDate1
12015-04-152015-04-30
12015-05-012015-05-15
12015-05-162015-05-31

Regards,

Przemek

Not applicable
Author

Forgot to paste link - this was Henric's example: https://community.qlik.com/thread/75435?q=Match%20two%20intervals

I need to retrieve the records by matching the intervals.

Gysbert_Wassenaar

This should do the trick:

Intervals:

LOAD * INLINE [

StartDate, EndDate

2015-05-01, 2015-05-31

2015-04-15, 2015-05-15

];

TempDates:

LOAD StartDate as Date, 'Start' as Type Resident Intervals;

LOAD EndDate as Date, 'End' as Type Resident Intervals;

NewIntervals:

LOAD * WHERE End>0;

LOAD

  if(Type = 'Start', Date, Date(Date+1)) as Start,

  if(Type = 'Start' and Previous(Type) = 'Start', Date(Previous(Date)-1),Date(Previous(Date))) as End

Resident TempDates Order By Date desc;

Drop Table TempDates;


talk is cheap, supply exceeds demand
Not applicable
Author

Thank you, Gysbert. Looks tempting because of its easiness. But how would it look like if we had intervals as such:

Intervals: 

LOAD * INLINE [ 

Id, StartDate, EndDate 

1, 2015-05-01, 2015-05-31 

1, 2015-04-15, 2015-05-15

2, 2015-02-01, 2015-02-31 

2, 2015-01-15, 2015-02-15

];

Not applicable
Author

Thank you, Digvijay. I've tried to integrate Id column into your code but failed. Would you be able to help with it?

Gysbert_Wassenaar

It would look quite strange since 2015-02-31 is not a valid date.

If you want to add Id's to the mix it will look like this:

Intervals:

LOAD

  Id,

  Date#(StartDate,'YYYY-MM-DD') as StartDate,

  Date#(EndDate,'YYYY-MM-DD') as EndDate 

INLINE [

Id, StartDate, EndDate

1, 2015-05-01, 2015-05-31

1, 2015-04-15, 2015-05-15

2, 2015-02-01, 2015-02-28

2, 2015-01-15, 2015-02-15

];

TempDates:

LOAD Id, StartDate as Date, 'Start' as Type Resident Intervals;

LOAD Id, EndDate as Date, 'End' as Type Resident Intervals;

NewIntervals:

LOAD * WHERE End>0;

LOAD

  Id,

  if(Type = 'Start', Date, If(Id = Previous(Id), Date(Date+1))) as Start,

  if(Id = Previous(Id),

  if(Type = 'Start' and Previous(Type) = 'Start',

  Date(Previous(Date)-1),

  Date(Previous(Date)

  )

  )

  ) as End

Resident TempDates Order By Id, Date desc;

Drop Table TempDates;


talk is cheap, supply exceeds demand