Duration data format issue and error

I have an event data file in Excel. It has a start date, end date and start time, end time for all events. I created a column in Excel for Duration, which is End time - Start time.

When I bring this data into Power Query, the Duration column formats as DateTime. If I choose to format it as Duration, I am getting an error. I am attaching the Power BI file, the Excel data file,
and the screenshots.

My query is, how do I show the duration between the end time and start time for each event? Is there a way to do this within Power BI?

Thank you in advance.
Events.pbix (76.3 KB)




Event_Data.xlsx (19.5 KB)

1 Like

You can certainly do this in Power Query.

The Start date and End date columns must be set to the Date only data type.

I selected the Start Date column, held down the Control/Ctrl key and selected the Start time column then went to the Date option in the ribbon and selected combine date & time. I repeated this process with the End Date/End Time.

Converted these merged columns to Datetime format

I used this piece of M Code to get the duration between the two

= Table.AddColumn(#"Changed Type1", "ElapsedDuration", each Duration.ToRecord([#"Event End"] - [#"Event Start"]))

Which creates a record for each row as per below

image

I then selected the Expand Columns drop down to reveal Days, Hours, Minutes & Seconds as per below. I deselected Seconds and the Use original column name as prefix (that should always be deselected as default…anyway :wink: )

image

Clicked OK and it gave me a duration column for Days, Hours and Minutes as per below

PBIX included. Hope this suits your needs

David

Hadeeh Duration Solution.pbix (24.6 KB)

3 Likes

Whoa! This is incredible! let me check it out. Thanks a ton!

1 Like

Here is an updated one which transforms the days & hours into minutes adds them to the minutes column then creates a column which divides by 1440 (number of mins in a day) to give you an overall duration in day figure.

This is a bit scruffy but I best get back to my day job :wink:

Hadeeh Duration Solution updated.pbix (29.2 KB)

1 Like

Thank you, Davie. Much appreciated.

I was trying to replicate the steps you described. However, after I select the Startdate to Start time columns, my Data menu on the ribbon is greyed out! Not sure what I am doing wrong. Sorry for the bother! Please let me know when time permits.

Is your Startdate field set as a Date data type? And your Starttime field set as a Time data type?

1 Like

Yes, it is. I just rechecked. Attaching a scree shot. Not sure why I can’t get this to work.

1 Like

Ah, it’s my fault. I said to hold down shift…this selects the End date column also. Shift will select all the columns between 1st selected and last selected.

Control/Ctrl will only select the clicked on columns while held down.

So please use the Control/Ctrl button instead of the shift key

I will update the original response so no one else falls foul of my dodgy key memory :rofl:

1 Like

:sweat_smile:

Checking now

1 Like

I am sorry, still no luck :frowning:

1 Like

Your Start date column is still set to a Datetime format. It must be Date only

You will also need to set your End date column to Date only

1 Like

Yes, that worked. I played around with the format when it first didn’t work and that’s why it got the DateTime format.

I am new to M. Just know some basics. I copied and pasted this into MQuery and now getting an error. I am really sorry to be such a bother. My apologies!


1 Like

You’re not causing any bother so don’t worry.

Can you do me a favour?

Can you create a blank PBIX file and paste your example data into it please? Do you know how to do this?

If so, post the PBIX into this thread.

1 Like

New_File_Hadeeh.pbix (51.3 KB)

Thank you! Please find attached the PBIX file where I have copied and pasted the data

Kind Regards

1 Like

Hi @hadeeh

Please find enclosed PBIX.

I have created two queries, one where it stops after I created the Day, Hours & Min columns and another fuller solution where I take steps to give you a final Duration in Days column with un-needed columns removed.

M Code can be confusing, it’s taken me a while to get my head around it and I still get confused so don’t worry if you feel you don’t understand it yet. There is a lot to it. Start off with simple things first.

If you have access to the learning portal I highly recommend the Power Query/M Code courses by @Melissa. They’ve been a massive help to me (and many others).

Hopefully, how I’ve created these queries, make it clearer.

Regards

David

New_File_Hadeeh.pbix (55.1 KB)

3 Likes

Thank you, so much David. Much appreciate you taking the time to do this.

Yes, I have access to the learning portal. I will check out the courses by Melissa.

Thank you, for your words of encouragement. Hope to get better at my skills and help others like you do.

Will check the file and get back with any questions I have.

Kind Regards,
Hazra

2 Likes

My pleasure Hazra :grinning:

1 Like

Hello David,

I just wanted to update you that I worked out creating the custom column and understood how to get the elapsed time using M. A big thank you, to you for helping me learn this.

Kind Regards,
Hazra

1 Like

No problem Hazra, glad to have helped.