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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
dbadmin
Helper IV
Helper IV

Calculate "On time" versus "Off time" by splitting time column - without filtering entire data set

I may not have labeled this question correctly. I know what I'm trying to accomplish but I haven't quite figured out how to explain it. So I'll try to show you instead. 🙂 

 

I have an "Event" View- I've done the base filtering on the database side - Here's some copy and paste Sample Data:

RowID       ID                DateTime               Event       Rotation

85777222015-12-04 11:12:2303683536
85777312015-12-04 11:12:250438875664
85777422015-12-04 11:12:3604683537
85777512015-12-04 11:12:370338875770
85777612015-12-04 11:12:560438875771
85777812015-12-04 11:13:360338876354
85777912015-12-04 11:13:360438876355
85778122015-12-04 11:14:0803684939
85778212015-12-04 11:14:080338876800
85778312015-12-04 11:14:080438876801
85778522015-12-04 11:14:0904684940
85778612015-12-04 11:14:450338877327
85778712015-12-04 11:14:460438877328
85778912015-12-04 11:14:560338877420
85779012015-12-04 11:14:560438877421
85779212015-12-04 11:15:060338877511
85779312015-12-04 11:15:070438877512
85779512015-12-04 11:15:140338877529
85779612015-12-04 11:15:580438877530
85779712015-12-04 11:16:070338877604

 

Data Modeling Goals -

 

  • Sort by ID  - 1s and 2s
  • Split DateTime Column (for filtering purposes in the final report) I know how to do this one
  • Split Event column by 3s and 4s WITHOUT filtering the whole data set. 

Ideally I need a Start time (04s) with the related time stamp and then add the Stop column next to it (03s) with it's related timestamp. 

 

Using a subset of the data provided above:

RowID       ID                DateTime               Event       Rotation

85778712015-12-04 11:14:460438877328
85778912015-12-04 11:14:560338877420
85779012015-12-04 11:14:560438877421
85779212015-12-04 11:15:060338877511
85779312015-12-04 11:15:070438877512
85779512015-12-04 11:15:1403

38877529

 

ideally it would look like this:  

RowID    ID   Date                 StartTime    StartEvent     StopTime     Stop Event     StartRotation      StopRotation

857787   1     2015-12-04     11:14:46        04                  11:14:56     03                    38877328            38877420

 

 

So on and so forth - I really don't even need the Start and Stop Event Columns.

 

Thinking on it - because of our second shift - I may need a StartDate and a Stop Date in case the start time is on day and the stop time is after midnight. 

 

The reason for this particular structure is for filterable reporting and calcuating how much "off time" there was versus "on time". 

 

I have tried to do a Merge Queries - and ended up with this -

 

Merge QueriesMerge Queries

 

But it's not working quite the way I want it to when I try to create a visual. 


When I try to place in a simple grid visual - I get this - 

Grid visual with Merged QueriesGrid visual with Merged Queries

It's almost like there's some funky filtering going on that I'm not seeing - you can't have a stop without a start event. 

 

Using an index column - the times still don't match up properly -

 

Times don't match up properlyTimes don't match up properly

 

Any ideas on how to do this a more effective / efficient way would be extremely helpful!! Thanks in advance!

 

~H

 

 

 

 

5 ACCEPTED SOLUTIONS

@dbadmin - This is a DAX way of doing it, but not sure it is doing what you want.

 

Using the sample data provided, Two queries:

 

StartTimes

let
    Source = Csv.Document(File.Contents("C:\temp\powerbi\downtime\downtime.txt"),[Delimiter="	", Columns=6, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"RowID", Int64.Type}, {"ID", Int64.Type}, {"DateTime", type datetime}, {"Event", Int64.Type}, {"Rotation", Int64.Type}, {"TagID", Int64.Type}}),
    #"Removed Bottom Rows" = Table.RemoveLastN(#"Changed Type",1),
    #"Filtered Rows" = Table.SelectRows(#"Removed Bottom Rows", each ([Event] = 3)),
    #"Added Index" = Table.AddIndexColumn(#"Filtered Rows", "Index", 0, 1)
in
    #"Added Index"

StopTimes

let
    Source = Csv.Document(File.Contents("C:\temp\powerbi\downtime\downtime.txt"),[Delimiter="	", Columns=6, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"RowID", Int64.Type}, {"ID", Int64.Type}, {"DateTime", type datetime}, {"Event", Int64.Type}, {"Rotation", Int64.Type}, {"TagID", Int64.Type}}),
    #"Removed Bottom Rows" = Table.RemoveLastN(#"Changed Type",1),
    #"Filtered Rows" = Table.SelectRows(#"Removed Bottom Rows", each ([Event] = 4)),
    #"Added Index" = Table.AddIndexColumn(#"Filtered Rows", "Index", 0, 1)
in
    #"Added Index"

In StartTimes table create new column:

 

TimeUp = DATEDIFF([DateTime],RELATED(StopTimes[DateTime]),SECOND)

You could then create a column like:

Hour = Hour([DateTime])

The issue I see here is that if you end up with TimeUp that might span hours. It starts before the hour and then ends after the hour. Is that the problem you are running into or are you having a different issue? Does this come close to what you are going for?


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

Sean
Community Champion
Community Champion

@dbadmin Here's what I did - let me know if this is what you are trying to do.

Create a Query Event3 - uncheck Enable Load - get rid of 4 and add index

Create a Query Event4 - uncheck Enable Load - get rid of 3 and add Index

Create Blank Query Result (type = Event3 in formula bar) - then Merge this with Event4 => Close and Apply

As you can see in the Resulting Table they seem to remain sorted and you can calculate the difference

Let me know if this works!

Query Editor3.png

 

 

View solution in original post

Sean
Community Champion
Community Champion

@dbadmin Smiley LOLSmiley LOLSmiley LOL

 

If its giving you an error both ways then there is definitely an issue with your data

 

Meaning if you subtract the columns manually you will get some negative durations (implying time travel)

View solution in original post

Sean
Community Champion
Community Champion

@dbadmin try using the DATEDIFF function on the Date/Time columns (instead of just the time)

 

Look at this picture... I get an error for the same times when using time only

but date/time is fine (note both are in the same order)

Query Editor5.png

 

View solution in original post

Final Answer - @Sean @ImkeF @Greg_Deckler 

 

There was some corrupt data, so here's what I did - 

  • Took this month's data (most recent and correct - in development it's hard to figure out if you have corrupted data until you start trying to play with it).
  • Went back into database side and only started collecting data from May 1st of this year - on...
  • Refreshed Data
  • Redid the Uptime column (DateDiff(Start.TimeOfEvent, Stop.TimeOfEvent,Second) - WORKED!!!! 😄 
  • Created another column 'Uptime in Hours' (changing interval 'SECOND' to 'HOUR' in calculated column
  • Put into a bar graph visual 
  • Set axis to be Start.Date
  • Created a Time slicer using time table - reference here 
  • And .... WAALAAAAAA!! 😄 😄 😄 😄 

 

End Results of a LOT of Hard work and collaboration of some great peopleEnd Results of a LOT of Hard work and collaboration of some great people

 

 

download.jpg   

 

I just started blogging for Power BI (my first post comes out later this week or first of next) I think this might be a great one to put up next? Are you guys ok with that? 😄 @Sean @ImkeF @Greg_Deckler?? 

 

 

EDIT -

 

Just double checked data on Uptime in Minutes and Hours and to get a more accurate time it's better to do it this way: 

UpTime in Seconds = DATEDIFF(Results[Start.TimeOfEvent],Results[TimeOfEvent],SECOND)
UpTime in Minutes = Results[UpTime in Seconds] / 60
UpTime in Hours = Results[UpTime in Minutes] / 60

 

Also Make sure the Data type is Decimal for at least the Uptime in Hours - I showed 2 Decimal Places. Double checked the numbers and it's a lot more accurate than using the DateDiff for Minutes and Hours. 

 

Here's what the Final Raw Data looks like: 
Clean Data.JPG

View solution in original post

31 REPLIES 31
Greg_Deckler
Super User
Super User

Hi, my first question is, how do you know which start time goes with which stop time? Is there a common ID that I am missing or are things sequential such that there is a start time and then a stop time for that event is the next row?


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

In the original dataset they fall in sequential order based on the time. Here's a sample: 

 

136443512016-05-16 05:28:0003884690241
136443612016-05-16 05:28:0004884690241
136443712016-05-16 05:28:0006884690251
136443912016-05-16 05:28:4103884696121
136444012016-05-16 05:28:4104884696121
136444112016-05-16 05:28:4106884696131
136444812016-05-16 05:29:4603884705701
136445012016-05-16 05:31:2904884705711
136445112016-05-16 05:31:4403884707171
136445212016-05-16 05:31:4404884707181
136445312016-05-16 05:31:4406884707181
136446812016-05-16 05:34:2703884732591
136447712016-05-16 05:37:0504884732601
136448012016-05-16 05:38:3003884745611

So in that example, I think I see events of the following in this order

 

03

04

06

03

04

06

03

04

03

04

06

03

04

03

 

Is that correct? And I assume that you are verifying that bottom row ends up being a "04" stop event so that there is a start and stop for each one. Assuming then that you pull all of the 03 events out and assign an index to each row and do the same for 04, then your index values should match I assume that is what you are going for? 

 


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Yes! That's exactly what I was going for - I indexed both queries so I can  validate the correct stop / start times.  In the Query Mode everything seemed great - even calculating the duration - but once I came out of Query Mode - I couldn't get where I wanted to from there. 

 

Ultimately - all I'm trying to do is figure how much time ID 1 was running / "up" and how long it was down/ wasn't "running".

 

The final report that is desired is a Ratio - 

 

Example:  for the time period of 8am - 12pm, ID 1 was down 35 minutes and 45 seconds  - of course you could do the "up time" instead of the "down time".

 

In my head it seems simple enough - but trying to pull it off the way the data comes to me is proving to be more difficult than I anticipated. I thought I was on the right track at first. 

 

 

EDIT:

The 06 I will take back out - it has the same timestamp as the 04 and the 03. It's another event that triggers when ID is not running but in this case since the timestamps are the same - it won't be relevant. I was trying something different. 

 

Sean
Community Champion
Community Champion

@dbadmin Is it time to ask you know who... I bet you she can do this in less than 5 minutes? That includes reading the question

 

I'll keep trying but...

QE.png

 

LOL - I've thought about that myself. 🙂 

 

@ImkeF  - any ideas on this one?

@dbadmin - This is a DAX way of doing it, but not sure it is doing what you want.

 

Using the sample data provided, Two queries:

 

StartTimes

let
    Source = Csv.Document(File.Contents("C:\temp\powerbi\downtime\downtime.txt"),[Delimiter="	", Columns=6, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"RowID", Int64.Type}, {"ID", Int64.Type}, {"DateTime", type datetime}, {"Event", Int64.Type}, {"Rotation", Int64.Type}, {"TagID", Int64.Type}}),
    #"Removed Bottom Rows" = Table.RemoveLastN(#"Changed Type",1),
    #"Filtered Rows" = Table.SelectRows(#"Removed Bottom Rows", each ([Event] = 3)),
    #"Added Index" = Table.AddIndexColumn(#"Filtered Rows", "Index", 0, 1)
in
    #"Added Index"

StopTimes

let
    Source = Csv.Document(File.Contents("C:\temp\powerbi\downtime\downtime.txt"),[Delimiter="	", Columns=6, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"RowID", Int64.Type}, {"ID", Int64.Type}, {"DateTime", type datetime}, {"Event", Int64.Type}, {"Rotation", Int64.Type}, {"TagID", Int64.Type}}),
    #"Removed Bottom Rows" = Table.RemoveLastN(#"Changed Type",1),
    #"Filtered Rows" = Table.SelectRows(#"Removed Bottom Rows", each ([Event] = 4)),
    #"Added Index" = Table.AddIndexColumn(#"Filtered Rows", "Index", 0, 1)
in
    #"Added Index"

In StartTimes table create new column:

 

TimeUp = DATEDIFF([DateTime],RELATED(StopTimes[DateTime]),SECOND)

You could then create a column like:

Hour = Hour([DateTime])

The issue I see here is that if you end up with TimeUp that might span hours. It starts before the hour and then ends after the hour. Is that the problem you are running into or are you having a different issue? Does this come close to what you are going for?


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler @ImkeF Ok - I've tried to implement it - oddly enough - it didn't give an error but it didn't produce any results either? Not sure what happened? Trying to investigate now

 

 

Adding TimeUp ColumnAdding TimeUp Column

 

EDIT -

 

Ok - there was a relationship issue. Corrected that. The got a DateDiff function, the start date cannot be greater than the end date - error. 

 

Tried to create the same column in the Stop Table. Tried to just do the Time frame and still got the same error. There must be some erroneous data in there somewhere or it's created when the tables are filtered... Smiley Frustrated

 

 

Edit Number 2:

 

Here's the sorted data from the two filtered tables in Power BI:

 

 

TimeOfEvent        event_type                  NewColumn.TimeOfEvent              NewColumn.event_type
12/4/2015 11:04         4                                       12/4/2015 11:06                               3
12/4/2015 11:06         4                                       12/4/2015 11:08                               3
12/4/2015 11:08         4                                       12/4/2015 11:10                               3
12/4/2015 11:10         4                                       12/4/2015 11:11                                3
12/4/2015 11:11         4                                       12/4/2015 11:11                                 3
12/4/2015 11:11         4                                       12/4/2015 11:12                                3
12/4/2015 11:12         4                                       12/4/2015 11:12                                3
12/4/2015 11:12         4                                       12/4/2015 11:13                                3
12/4/2015 11:13         4                                       12/4/2015 11:14                                3
12/4/2015 11:14         4                                       12/4/2015 11:14                                3
12/4/2015 11:14         4                                       12/4/2015 11:14                                3
12/4/2015 11:14         4                                       12/4/2015 11:15                                3
12/4/2015 11:15         4                                       12/4/2015 11:15                                3
12/4/2015 11:15         4                                       12/4/2015 11:16                                3

 

Here's the original data - this is how it comes to me - I filter out the other data to simplify it. I might be able to manipulate the data a different way on the database side to make it more workable - but i"m not sure how else I would do it...

 

 Any thoughts / ideas? 

 

Row ID  ID          DateTime                       Event Code

857740 1         2015-12-04 11:04:51          04         
857741 1         2015-12-04 11:06:38          03          
857742 1         2015-12-04 11:06:38          04           
857747 1         2015-12-04 11:08:11          03 
857748 1         2015-12-04 11:08:11          04 
857760 1         2015-12-04 11:10:44          03 
857761 1         2015-12-04 11:10:44          04 
857763 1         2015-12-04 11:11:10          03 
857766 1         2015-12-04 11:11:21          04 
857767 1         2015-12-04 11:11:29          03 
857770 1         2015-12-04 11:11:56          04 
857771 1         2015-12-04 11:12:17          03 
857773 1         2015-12-04 11:12:25          04 
857775 1         2015-12-04 11:12:37          03 
857776 1         2015-12-04 11:12:56          04 
857778 1         2015-12-04 11:13:36          03 
857779 1         2015-12-04 11:13:36          04 
857782 1         2015-12-04 11:14:08          03 
857783 1         2015-12-04 11:14:08          04 
857786 1         2015-12-04 11:14:45          03 
857787 1         2015-12-04 11:14:46          04 
857789 1         2015-12-04 11:14:56          03 
857790 1         2015-12-04 11:14:56          04 

Sean
Community Champion
Community Champion

@dbadmin Here's what I did - let me know if this is what you are trying to do.

Create a Query Event3 - uncheck Enable Load - get rid of 4 and add index

Create a Query Event4 - uncheck Enable Load - get rid of 3 and add Index

Create Blank Query Result (type = Event3 in formula bar) - then Merge this with Event4 => Close and Apply

As you can see in the Resulting Table they seem to remain sorted and you can calculate the difference

Let me know if this works!

Query Editor3.png

 

 

@Sean - Hey! 🙂 I'm giving your solution a go - but I'm having trouble getting the data into the Blank query...

 

the type = Event 3 isn't working. Tried to reference it in the Advanced Query Editor also - it's just not doing anything. Any ideas?

 

EDIT - I keep getting this error: 

 

Expression.Error: There is an unknown identifier. Did you use the [field] shorthand for a _[field] outside of an 'each' expression?

 

Although I think is related to M @ImkeF - if I'm not mistaken?

 

Sean
Community Champion
Community Champion

@dbadmin rename both without a space and then try without a space between Event and 3 => = Event3

@Sean Same issue ... 😕 

Sean
Community Champion
Community Champion

@dbadmin Look.... Don't know why - maybe try the other one = Event4

Query Editor4.png

Got it - I deleted it and started over. Thanks!! Now to finish this. 🙂 

@Sean - 

 

images.png

 

 

Error.JPG

 

 

Sean
Community Champion
Community Champion

@dbadmin Smiley LOL - switch the dates/times in the formula

 

I did that....

 

images.jpg

Sean
Community Champion
Community Champion

@dbadmin Smiley LOLSmiley LOLSmiley LOL

 

If its giving you an error both ways then there is definitely an issue with your data

 

Meaning if you subtract the columns manually you will get some negative durations (implying time travel)

Si Senor... (I think I'm becoming delusional)...

 

Dataset6.JPG

 

 

EDIT - I had a thought...

 

 

Sean
Community Champion
Community Champion

Deleted COPY

 

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.