Forum Discussion
Problem with duration
- 10 years ago
Brute force:
Measures:
MinutesPerHour = 60 MinutesPerDay = 1440 MinutesPerWeek = 10080
Custom Columns:
Weeks = ROUNDDOWN([Minutes]/[MinutesPerWeek],0) Days = ROUNDDOWN(MOD([Minutes],[MinutesPerWeek])/[MinutesPerDay],0) Hours = ROUNDDOWN(MOD(MOD([Minutes],[MinutesPerWeek]),[MinutesPerDay])/[MinutesPerHour],0) MinutesLeft = ROUNDDOWN(MOD(MOD(MOD([Minutes],[MinutesPerWeek]),[MinutesPerDay]),[MinutesPerHour]),0) Duration = IF(LEN([Weeks])=2,"",0) & [Weeks] & ":" & IF(LEN([Days])=2,"",0) & [Days] & ":" & IF(LEN([Hours])=2,"",0) & [Hours] & ":" & IF(LEN([MinutesLeft])=2,"",0) & ":" & [MinutesLeft]
Brute force:
Measures:
MinutesPerHour = 60 MinutesPerDay = 1440 MinutesPerWeek = 10080
Custom Columns:
Weeks = ROUNDDOWN([Minutes]/[MinutesPerWeek],0) Days = ROUNDDOWN(MOD([Minutes],[MinutesPerWeek])/[MinutesPerDay],0) Hours = ROUNDDOWN(MOD(MOD([Minutes],[MinutesPerWeek]),[MinutesPerDay])/[MinutesPerHour],0) MinutesLeft = ROUNDDOWN(MOD(MOD(MOD([Minutes],[MinutesPerWeek]),[MinutesPerDay]),[MinutesPerHour]),0) Duration = IF(LEN([Weeks])=2,"",0) & [Weeks] & ":" & IF(LEN([Days])=2,"",0) & [Days] & ":" & IF(LEN([Hours])=2,"",0) & [Hours] & ":" & IF(LEN([MinutesLeft])=2,"",0) & ":" & [MinutesLeft]
Sorry, I'm a bit new in these forums, questions are always answered in DAX. Would it be pointless for me to help people with M code? Because M has nice ways of handling durations
- Greg_Deckler10 years ago
Community Champion
Not at all, greggyb often answers in M. As an end-user focused tool, DAX tends to be the go to for many of the users of Power BI but M code solutions are just as viable.
- arify10 years ago
Microsoft Employee
Thanks @smoupre, in that case I'll also put an M response

In M, if you have the number of minutes, you can create the duration value like this (parameters are days, hours, minutes, seconds):
#duration(0, 0, [Minutes], 0)
And then, when you have the duration value, you can get the text representation you wanted like this:
#"Changed Type" = Table.TransformColumnTypes(Source,{ {"DurationInMinutes", Int64.Type} }),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Duration", each #duration(0, 0, [DurationInMinutes], 0)),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "DurationWithWeeks",
each Number.ToText(Number.IntegerDivide(Duration.Days([Duration]),7)) & ":"
& Number.ToText(Number.Mod(Duration.Days([Duration]),7)) & ":"
& Number.ToText(Duration.Hours([Duration])) & ":"
& Number.ToText(Duration.Minutes([Duration])))
in
#"Added Custom1"Which looks like this:
However, if you're ok with the representation of "Duration" column, it's even simpler
:#"Changed Type" = Table.TransformColumnTypes(Source,{ {"DurationInMinutes", Int64.Type} }), #"Added Custom" = Table.AddColumn(#"Changed Type", "Duration", each #duration(0, 0, [DurationInMinutes], 0)) in #"Added Custom" - greggyb10 years ago
Resident Rockstar
Power Query can certainly handle durations, and nicely, but when the problem is reporting presentation, then we can't leverage most of the niceness of M. If there's no need to combine durations, only present values that will be static in reports (think labels rather than measures), then we can cast the duration to a string and be done with it.
The requirements in this thread are loose, but if I hear that there's a field containing a duration in minutes, I'd expect some of the requirements will be to find total and average duration. This wouldn't be possible if we passed a Power Query duration into our model. Either we'd cast to string where the only aggregates available would be count and (lexicographical) max/min, or we'd leave it as a PQ duration, and get a float in the model, which can be aggregated easily, but not presented without something like Greg_Deckler's mod arithmetic with rounding.
- Greg_Deckler10 years ago
Community Champion
The problem I see with using M and duration from the original problem statement is the desired inclusion of weeks. That may not really be what was intended but things are constrained to how M handles durations. If the exact format of weeks:days:hours:minutes is desired, I don't see how you get there except a brute force approach (that could admittedly most likely also be handled by M)
Edit: Oh, nevermind, I do see the solution for the original format for weeks above. Oops!