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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
MichaelHutchens
Helper IV
Helper IV

Create a summary table from a primary table of incident timestamps

Hi folks, I'm hoping someone can assist. I need to create a summary table from a primary table of incidents.

The primary table contains a list of incident start dates, and their duration in hours and minutes. Here's an example:

incident table

Date of incident start Incident duration (HH:MM)
29/01/2021 8:10am2:02
29/01/2021 4:15pm0:30
5/2/2021 11:20am1:15
20/02/2021 2:50pm9:05
20/02/2021 2:40pm0:05
21/02/2021 2:02pm0:49
18/01/2021 8:32am1:10

 

The secondary table should be a matrix that breaks down each incident into it's starting hour (based on a 24-hour clock), how many incidents started in that hour bracket, and the average duration of all of those incidents in each hour bracket. Here's an example, based on the sample data above:

Hour Count of incidents Average incident duration (HH:MM)
0000:00
1000:00
2000:00
3000:00
4000:00
5000:00
6000:00
7000:00
8201:36
9000:00
10000:00
11101:15
12000:00
13000:00
14303:19
15000:00
16100:30
17000:00
18000:00
19000:00
20000:00
21000:00
22000:00
23000:00


I need to show zeros rather than blanks, if there aren't any incidents in a particular hour bracket or time period (I'll be using a slicer on the summary table, using the 'date of incident start' column).

Any help would really be appreciated, thanks so much 🙂

1 ACCEPTED SOLUTION
TomMartens
Super User
Super User

Hey @MichaelHutchens ,

 

first I transformed  sample data a little, as the Power BI data model necessary, does not know the data type duration.

My transformations in Power Query are

  • Converted the column Date of Incident Start to Date/Time
  • Added a column extracting the hour part from the Time of the start column
  • Converted duration column into a time column
  • Added a column of the data type duration derived from the duration column, this is necessary to do some time based maths, here I chose the transform "Total seconds"

The table will look like this:

image.png 

 

Then I created these 2 measures:

count of incidents = 
var __NoOfRows = COUNTROWS( 'Table' )
return
IF( ISBLANK( __NoOfRows ) , 0 , __NoOfRows )

And this one, this one is lengthy but simple, here some time based calculations happens and also some formatting

average incident duration = 

    var __noofrows = [count of incidents]
    var __duration = CALCULATE( SUM( 'Table'[Total Seconds] ) )
    var __averageDurationInSeconds = DIVIDE( __duration , __noofrows , 0 )
    var constSecondsPerDay = 24 * 60 * 60
    var constSecondsPerHour = 60 * 60
    var __Days = TRUNC( DIVIDE( __averageDurationInSeconds , constSecondsPerDay ) , 0)
    var __Hours = TRUNC( DIVIDE( __averageDurationInSeconds - __Days * constSecondsPerDay , constSecondsPerHour ) , 0 )
    var __Minutes = TRUNC( DIVIDE( __averageDurationInSeconds - __Days * constSecondsPerDay - __Hours * constSecondsPerHour , 60 ) , 0 )
    var __Seconds = TRUNC( __averageDurationInSeconds - __Days * constSecondsPerDay - __Hours * constSecondsPerHour - __Minutes * 60 )
    // create HH:MM Format
    var __DaysFormatted = IF( __Days = 0 , "" , CONCATENATE(__Days , "." ) ) 
    var __HoursFormatted = CONCATENATE( IF( __Hours < 10 , RIGHT( CONCATENATE( "00" , FORMAT( __Hours , "" ) ) , 2 ) , __Hours ) , ":" )
    var __MinutesFormatted = CONCATENATE( IF( __Minutes < 10 , RIGHT( CONCATENATE( "00" , FORMAT( __Minutes , "" ) ) , 2 ) , __Minutes ) , ":" )
    var __SecondsFormatted = IF( __Seconds < 10 , RIGHT( CONCATENATE( "00" , __Seconds ) , 2 ) , __Seconds )

    return
        __DaysFormatted & __HoursFormatted & __MinutesFormatted & __SecondsFormatted

In addition to that I also create a simple table that reflects the 24hours of the day 0 .. 23, I created a relationship between both tables.

All of this allows to create a table visual like this:

image.png

Hopefully, this is what you are looking for and helps to tackle your challenge.

 

Regards,

Tom

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

2 REPLIES 2
TomMartens
Super User
Super User

Hey @MichaelHutchens ,

 

first I transformed  sample data a little, as the Power BI data model necessary, does not know the data type duration.

My transformations in Power Query are

  • Converted the column Date of Incident Start to Date/Time
  • Added a column extracting the hour part from the Time of the start column
  • Converted duration column into a time column
  • Added a column of the data type duration derived from the duration column, this is necessary to do some time based maths, here I chose the transform "Total seconds"

The table will look like this:

image.png 

 

Then I created these 2 measures:

count of incidents = 
var __NoOfRows = COUNTROWS( 'Table' )
return
IF( ISBLANK( __NoOfRows ) , 0 , __NoOfRows )

And this one, this one is lengthy but simple, here some time based calculations happens and also some formatting

average incident duration = 

    var __noofrows = [count of incidents]
    var __duration = CALCULATE( SUM( 'Table'[Total Seconds] ) )
    var __averageDurationInSeconds = DIVIDE( __duration , __noofrows , 0 )
    var constSecondsPerDay = 24 * 60 * 60
    var constSecondsPerHour = 60 * 60
    var __Days = TRUNC( DIVIDE( __averageDurationInSeconds , constSecondsPerDay ) , 0)
    var __Hours = TRUNC( DIVIDE( __averageDurationInSeconds - __Days * constSecondsPerDay , constSecondsPerHour ) , 0 )
    var __Minutes = TRUNC( DIVIDE( __averageDurationInSeconds - __Days * constSecondsPerDay - __Hours * constSecondsPerHour , 60 ) , 0 )
    var __Seconds = TRUNC( __averageDurationInSeconds - __Days * constSecondsPerDay - __Hours * constSecondsPerHour - __Minutes * 60 )
    // create HH:MM Format
    var __DaysFormatted = IF( __Days = 0 , "" , CONCATENATE(__Days , "." ) ) 
    var __HoursFormatted = CONCATENATE( IF( __Hours < 10 , RIGHT( CONCATENATE( "00" , FORMAT( __Hours , "" ) ) , 2 ) , __Hours ) , ":" )
    var __MinutesFormatted = CONCATENATE( IF( __Minutes < 10 , RIGHT( CONCATENATE( "00" , FORMAT( __Minutes , "" ) ) , 2 ) , __Minutes ) , ":" )
    var __SecondsFormatted = IF( __Seconds < 10 , RIGHT( CONCATENATE( "00" , __Seconds ) , 2 ) , __Seconds )

    return
        __DaysFormatted & __HoursFormatted & __MinutesFormatted & __SecondsFormatted

In addition to that I also create a simple table that reflects the 24hours of the day 0 .. 23, I created a relationship between both tables.

All of this allows to create a table visual like this:

image.png

Hopefully, this is what you are looking for and helps to tackle your challenge.

 

Regards,

Tom

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

I'm always excited when I get a reply to my questions in this forum, and this response is an example why, @TomMartens . Thanks so much for your time, this is perfect 🙂

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.