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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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!:
Power BI Cookbook Third Edition (Color)

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

@Sean - @Greg_Deckler @ImkeF

 

download.png ...

 

GOT IT.JPG

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

Great! Can't wait to see the blog!!

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Sean
Community Champion
Community Champion

@dbadmin DA-BAAAAAD-Administrator!!! Smiley LOL

 

That user name is awesome - I can never read it as db-admin though.

 

Anyway you are on a roll! There's no stopping you! Waiting to see the blog...

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

 

@Greg_Deckler I've got a meeting at 9am this morning - but will be tackling this the rest of the day - Do you have a screenshot by chance of the results produced from this solution? That would be a quick gauge for me if it's doing what it needs to - 

 

This is done in the Advanced Query Editor correct? I'm not very familiar with using that yet. But I'll give it a go. 🙂 

 

 

 

 

Just a side note: @Greg_Deckler @ImkeF @Sean - I can't thank you guys enough for helping me tackle these different problems. I'm learning a lot from you all - and I get a little bit further in the problem solving each time before I have to start asking for help. But I am tremendously grateful! 🙂 No one else in my company is familiar with this stuff (at all); not even the Power Query and such on the Excel side - so having you all and the community to work with has been invaluable. 🙂

THANK YOU! 

If @Greg_Deckler s suggestion doesn't work, check out this:

M is sometimes behaving nasty with sorted results: Simply doesn't keep them.

You need to put a "Table.Buffer" around the step that applies the sorting (and sometimes also the following steps until you reach the step where the sorted results are used.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hello @ImkeF! 🙂 If there is a link in the message, I think it's broken (@ Check out this:)

 

I'm going to tackle @Greg_Deckler solution after my meeting this morning. So I'll be able to give feedback probably before lunch - if I can get it working properly. I'm not very familiar with using the Advanced Query Editor yet. But I've got some DAX and Power BI books so I'll be breaking those in some more. :). I'm seriously thinking about getting a book on M. 

 

THANK YOU! 🙂 

 

Will get back with you all asap!

Sorry - that didn't paste very clean - here's an image... 

 

dataset5.JPG

Nhallquist
Helper V
Helper V

What is your formual for parsing out the Identifiers, like the Start and Stop events?  Can you just post your formula for them?

In my original attempt I duplicated the data set, did a simple filter for '03' stop events in one query and '04' start event in the other query. Then Merged the queries from there. 

 

I didn't create a formula to parse those two. 

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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

Top Kudoed Authors