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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
karkar
Helper III
Helper III

Calculating hours

Hello,

 

I am trying to do the difference of two dates and come up with hours in the hospital.

In the process, for some people who have been in the hospital before a particular date (say 1st January) , i would want to ignore those hours, Is it possible to acheive this in Power Bi?

For simplicity , in the examples below i am assuming that a patient came in at 12:00am (admission)  on the day specified and left at 12:00am on the dicharge date

 

pat    Admission         Discharge         hours

001    01JAN2017       02JAN2017       24

002    28DEC2016       05JAN2017       96   (considered only the hours from 01JAN2017)

003    31JAN2016       02JAN2017       24       

 

Thank you

 

1 ACCEPTED SOLUTION
v-ljerr-msft
Employee
Employee

Hi @karkar,

 

According to your description, the formula using DATEDIFF Function (DAX) provided by PavelR above should work as expected with a little modification like below.Smiley Happy

 

Assume your table is called "Table1", then you should be able to use the formula below to create a calculate column to calculate the difference between the two dates in hours.

Hours = 
IF (
    Table1[Admission] < DATE ( 2017, 1, 1 ),
    DATEDIFF ( DATE ( 2017, 1, 1 ), Table1[Discharge], HOUR ),
    DATEDIFF ( Table1[Admission], Table1[Discharge], HOUR )
)

c1.PNG

 

Regards

View solution in original post

2 REPLIES 2
v-ljerr-msft
Employee
Employee

Hi @karkar,

 

According to your description, the formula using DATEDIFF Function (DAX) provided by PavelR above should work as expected with a little modification like below.Smiley Happy

 

Assume your table is called "Table1", then you should be able to use the formula below to create a calculate column to calculate the difference between the two dates in hours.

Hours = 
IF (
    Table1[Admission] < DATE ( 2017, 1, 1 ),
    DATEDIFF ( DATE ( 2017, 1, 1 ), Table1[Discharge], HOUR ),
    DATEDIFF ( Table1[Admission], Table1[Discharge], HOUR )
)

c1.PNG

 

Regards

PavelR
Solution Specialist
Solution Specialist

Hi @karkar

 

this works for me

Stay in hospital (hours) = IF(Data[Admission]<DATE(2017;1;1);DATEDIFF(DATE(2017;1;1);Data[Discharge];HOUR)-12;DATEDIFF(Data[Admission];Data[Discharge];HOUR))

Regards.

Pavel

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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