Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Would intervalmatch work for what I am trying to do?

Basically I have a data base that produces a time stamp every time a unit is produced in a factory. The time stamp is formatted as MM/DD/YYYY HH:MM:SS (24-hour time). What I am trying to do is create a drill down dimension as follows: Year->Month->DayNumber->Line->Shift. I have everything figured out except for how to derive which shift the timestamp would fall into. The data being loaded looks like:

     

LineTimestampOk Pieces Produced
Front right11/30/2015 06:20:021
Rear Left11/30/2015 13:03:181
Front Left11/30/2015 17:03:430
Front Left12/01/2015 06:53:021
Rear Right12/01/2015 16:47:220

I currently have the Timestamp filed mapped to derive 'Year' 'Month' 'DayNumber'. The line field already has the info I need for the 'line' dimension of the drill down so all that I am missing is for to determine the shift. Basically I want the result of the above table to look like the below table once it is loaded:

LineTimestampOk Pieces ProducedCrew
Front right11/30/2015 06:20:021C-Crew
Rear Left11/30/2015 13:03:181A-Crew
Front Left11/30/2015 17:03:430B-Crew
Front Left12/01/2015 06:53:021A-Crew
Rear Right12/01/2015 16:47:220C-Crew

I created the following table to display the weekly schedule for the shifts:

CrewStart DayStart TimeEnd Time
C-CrewSunday4:30:00 PM6:29:59 AM
A-CrewMonday6:30:00 AM4:29:59 PM
B-CrewMonday4:30:00 PM6:29:59 AM
A-CrewTuesday6:30:00 AM4:29:59 PM
C-CrewTuesday4:30:00 PM6:29:59 AM
C-CrewWednesday6:30:00 AM4:29:59 PM
B-CrewWednesday4:30:00 PM6:29:59 AM
C-CrewThursday6:30:00 AM4:29:59 PM
A-CrewThursday4:30:00 PM6:29:59 AM
B-CrewFriday6:30:00 AM4:29:59 PM

My question is, when I load the original database file, how do I add the field "Crew" in which crew is determined by comparing the timestamp to this schedule? I was able to use "if" statements to do something similar to this for another unit, but in that case the crew worked the same shift everyday so I could simply use the time interval. In this case I need to first analyze which day of the week the timestamp refers to, and then which time interval it would be contained by. I read a few documents on the interval match function, but they don't show how to add a field to the file being loaded, rather, they just show how to link two tables and I am not sure if that would work for creating a dimension. Any advice on the best method to script this would be greatly appreciated!

3 Replies
marcus_sommer

I think an intervalmatch should be possible if you extend your crew-shift-table to a date or you used only the time-part - time(frac(Timestamp)) - from yor production-table. But over a longer period of time the crew-shifts might change so that the date-extension will be necessary. Here you will find a good explanations for IntervalMatch‌.

- Marcus

jonathandienst
Partner - Champion III
Partner - Champion III

You will need to convert the time stamps in the production file to a week offset time (lets call it WeekTime), adjusted for 0 = Sunday in decimal days:

Production:

LOAD *,

  If(Offset + 1 >= 7, Offset - 6, Offset + 1) As WeekTime

;

LOAD *,

  Timestamp - WeekStart(Timestamp) As Offset

;

LOAD

  Line,

  Timestamp#(Timestamp, 'MM/dd/yyyy hh:mm:ss') As Timestamp,

  Pieces,

  ...

When you load the shifts, you will need to convert the start date to the same offsets, and compute and end date (which I have assumed to be the next day if the end time is before 12:00 noon) also in decimal days:

Shifts:

LOAD Crew,

  StartDay + Start As ShiftStart,

  EndDay + End As ShiftEnd

;

LOAD Crew,

  StartDay,

  If(End < 0.5, StartDay + 1, StartDay) As EndDay,

  Start,

  End

;

LOAD Crew,

  Dual([Start Day], Floor(Index('SunMonTueWedThuFriSat', Left([Start Day], 3))/3)) As StartDay,

  Time#([Start Time], 'h:mm:ss tt') As Start,

  Time#([End Time], 'h:mm:ss tt') As End

From Shifts....

Now you can use interval matching to link the crew to the production line:

Left Join (Production)

IntervalMatch(WeekTime)

LOAD ShiftStart, ShiftEnd

Resident Shifts;

If you want to bring the Crew into the production table (not strictly necessary - the model will work correctly without this step), and possibly remove the shifts table which will no longer be required:

Left Join (Production)

LOAD Crew, ShiftStart, ShiftEnd Resident Shifts;

DROP Fields ShiftStart, ShiftEnd From Production;

DROP Table Shifts;

I have attached a complete script which uses inline loads - these can easily be converted to file or database loads. I get this output:

The shifts table before interval matching:

Shifts.png

The results table

Results.png

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
Not applicable
Author

Thank you so much for the example script. This is exactly what I was looking for!