Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
azeem
New Member

Calculate Overlap Period

Hi, I have two columns represent the start time & end time for each activities. Each of the periods are overlapping. I would like to calculate total hours (considering the overlapping periods). The solution that I've found takes too long as I have 10k rows. Therefore, I would like to create a measure using GENERATESERIES for each row for each second and UNION them into a column to DISTINCTCOUNT them to get the total seconds then convert them to total hours (that I think will make the DAX code runs faster). How I could achieve that? Thanks

 

azeem_1-1649330631720.png

 

 

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @azeem 

You can certainly write a measure along these lines, and I would be interested to hear how it performs with your actual dataset.

You can use GENERATE with GENERATESERIES to create the required list of "second values" per row in a new column. Then select this column and count distinct values.

 

The measure would look something like this:

 

Total Hours = 
VAR SecondsPerHour = 3600
VAR SecondsPerDay = SecondsPerHour * 24
VAR SecondIndex =
    SELECTCOLUMNS (
        GENERATE (
            Data,
            GENERATESERIES (
                INT ( Data[Start Time] * SecondsPerDay ),
                INT ( Data[End Time] * SecondsPerDay )
            )
        ),
        "@SecondIndex", [Value]
    )
VAR NumSeconds =
    COUNTROWS (
        DISTINCT ( SecondIndex )
    )
VAR Hours = NumSeconds / SecondsPerHour
RETURN
    Hours

 

Alternatively:

Total Hours v2 = 
VAR SecondsPerHour = 3600
VAR SecondsPerDay = SecondsPerHour * 24
VAR SecondIndex =
    SUMMARIZE (
        GENERATE (
            Data,
            GENERATESERIES (
                INT ( Data[Start Time] * SecondsPerDay ),
                INT ( Data[End Time] * SecondsPerDay )
            )
        ),
        [Value]
    )
VAR NumSeconds =
    COUNTROWS ( SecondIndex )
VAR Hours = NumSeconds / SecondsPerHour
RETURN
    Hours

 

We multiply by SecondsPerDay in order to convert serial numbers (where an interval of a day corresponds to a value of 1) into seconds.

 

The values created by GENERATESERIES should be well within the Power BI's integer bounds for any conceivable date/time values.

 

One thing to note is that both the start and end times are included. So if Start Time and End Time are equal, the duration for that row would be one second. This could be tweaked if required.

 

Does this work for you?

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

1 REPLY 1
OwenAuger
Super User
Super User

Hi @azeem 

You can certainly write a measure along these lines, and I would be interested to hear how it performs with your actual dataset.

You can use GENERATE with GENERATESERIES to create the required list of "second values" per row in a new column. Then select this column and count distinct values.

 

The measure would look something like this:

 

Total Hours = 
VAR SecondsPerHour = 3600
VAR SecondsPerDay = SecondsPerHour * 24
VAR SecondIndex =
    SELECTCOLUMNS (
        GENERATE (
            Data,
            GENERATESERIES (
                INT ( Data[Start Time] * SecondsPerDay ),
                INT ( Data[End Time] * SecondsPerDay )
            )
        ),
        "@SecondIndex", [Value]
    )
VAR NumSeconds =
    COUNTROWS (
        DISTINCT ( SecondIndex )
    )
VAR Hours = NumSeconds / SecondsPerHour
RETURN
    Hours

 

Alternatively:

Total Hours v2 = 
VAR SecondsPerHour = 3600
VAR SecondsPerDay = SecondsPerHour * 24
VAR SecondIndex =
    SUMMARIZE (
        GENERATE (
            Data,
            GENERATESERIES (
                INT ( Data[Start Time] * SecondsPerDay ),
                INT ( Data[End Time] * SecondsPerDay )
            )
        ),
        [Value]
    )
VAR NumSeconds =
    COUNTROWS ( SecondIndex )
VAR Hours = NumSeconds / SecondsPerHour
RETURN
    Hours

 

We multiply by SecondsPerDay in order to convert serial numbers (where an interval of a day corresponds to a value of 1) into seconds.

 

The values created by GENERATESERIES should be well within the Power BI's integer bounds for any conceivable date/time values.

 

One thing to note is that both the start and end times are included. So if Start Time and End Time are equal, the duration for that row would be one second. This could be tweaked if required.

 

Does this work for you?

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors