Power BI: time management
This is how I use and calculate the time, let´s take an example, first with only the time:
To calculate in number, I will add a new column with this formula:
HOUR('table'[argument])*3600+MINUTE('table'[argument]*60)+SECOND('table'[argument])
And to convert it into number:
HOUR('table'[argument])&":"&MINUTE('table'[argument])&":"&SECOND('table'[argument])
Now I have the date and time together:
And I want to know the duration so I will calculate the difference between them to get the number of days:
As I can see, it is not showing what I want. Moreover, the date is not correct and the difference of hours is not correct for the last one. To get what I need, I will change its data type and I will take advantage to change its name from “column” to “days”:
This is the result:
I will add 3 new columns for the duration:
- In hour with this formula: [argument]*24
- In minute with this formula: [argument]*1440
- In second with this formula: [argument]*86400
Alternatively, I can use the DATEDIFF function like that:
- For hour with this formula: DATEDIFF([argument1],[argument2],HOUR)
- For minute with this formula: DATEDIFF([argument1],[argument2],MINUTE)
- For second with this formula: DATEDIFF([argument1],[argument2],SECOND)
As I can see, comparing both formulas:
- For hours, DATEDIFF doesn´t take in account the seconds and minutes
- For minutes, DATEDIFF doesn´t take in account the seconds
Now, I want to show the duration in the time format, I will create a new column and use the same formula as I put for “days” and in the “data type” field, I will select “time”:
I can see that for the last row, since it is more than 24 hours, it is not showing correctly. To remediate it, I will change my formula by this one:
var calsec=[argument] // or put a formula var shour=INT(INT(calsec/60)/60) var smin=MOD(INT(calsec/60),60) var ssec=MOD(calsec,60) return shour&":"&smin&":"&ssec // to show 2 digits: FORMAT(shour,"00")&":"&FORMAT(smin,"00")&":"&FORMAT(ssec,"00")
This formula will work if the [argument] is in seconds. If it is in minutes, I will use this one:
var calmin=[argument] // or put a formula var shour=int(calmin/60) var smin=MOD(calmin,60) return shour&":"&smin // to show 2 digits: FORMAT(shour,"00")&":"&FORMAT(smin,"00")
Interesting Topics
-
Be successfully certified ITIL 4 Managing Professional
Study, study and study, I couldn’t be successfully certified without studying it, if you are interested...
-
Be successfully certified ITIL 4 Strategic Leader
With my ITIL 4 Managing Professional certification (ITIL MP) in the pocket, it was time to go for the...
-
Hide visual and change background color based on selection
Some small tricks to customize the background colour of a text box...
-
Stacked and clustered column chart or double stacked column chart
In excel, I use a lot the combination of clustered and stacked chart...