Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Discover how organizations are unlocking new revenue streams: Watch here
cancel
Showing results for 
Search instead for 
Did you mean: 
titouanroussel
Partner - Contributor
Partner - Contributor

Count number of hour in an hour range between 2 timestamps

Hello everyone !

I'm trying to calculate the total number of hours that fall within a specific time window ( between 00:00 and 06:00 ) across a time range defined by two timestamps in Qlik Sense.

For example, suppose I have the following timestamps :

StartTime: 2025-04-20 22:00:00

EndTime: 2025-04-21 08:00:00

I want to calculate how many hours between these two timestamps fall specifically within the 00:00 to 06:00 window. In this case, the answer should be 6 hours, since the entire window from 00:00 to 06:00 on 2025-04-21 is covered by the interval.

Another example:

StartTime: 2025-04-20 05:00:00

EndTime: 2025-04-20 07:00:00

Here, the overlap with the 00:00–06:00 window is only from 05:00 to 06:00, so the expected result would be 1 hour.

Does anyone know how this kind of calculation can be done in Qlik Sense (script or chart expression) ?

Thanks in advance for your help!

Labels (1)
3 Replies
gq
Contributor III
Contributor III

Try this
Interval(Timestamp#('2025-04-21 08:00:00', 'YYYY-MM-DD hh:mm:ss') - Timestamp#('2025-04-20 22:00:00', 'YYYY-MM-DD hh:mm:ss'), 'hh')

titouanroussel
Partner - Contributor
Partner - Contributor
Author

I already tried this method , it work great for counting the number of hours between two timestamps but finding the number of hours between the 2 timestamp is not the issue , the real issue is to count the hours only between 00:00 and 06:00 

Vegar
MVP
MVP

Can you assume that the period will not run over more than a single night? If so then you can do something like this.

Interval(

rangemin(dayname(timestamp_end) + maketime(6) , timestamp_end)

- rangemax(dayname(timestamp_end), timestamp_start)

, 'hh')