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
Mega79
Helper I
Helper I

DAX formula to create a New Measure (number of hours between starting date time and end date time)

Hi all!

I need to create a New Measure DAX formula to sum up the number of hours between "starting date time" and "end date time" of two columns. The formula needs to exclude the end date time cells that do not have any value.

 

What I really want is to get the average number of total hours between dates for a given month.

e.g.  15 Hours for the month of february.

 

Can someone help me with this?

Thanks in advance.

 

Dates.PNG

1 ACCEPTED SOLUTION
AlexChen
Employee
Employee

Hi,

 

I updated the data in your table to try to give an example. The data of February is from yours.

 

1.png

 

1. Firstly you have to create 2 new columns from “end date time” column. One is “Month”, the other is “Date”.

See screenshot below. You can create them from “query editor” -> “add column”.

 

2.png

 

2. create measure “sumHours”:

 

sumHours = sumx(timeTable, DATEDIFF(timeTable[starting date time], timeTable[end date time], SECOND))/3600

 

3. create measure “countDaysByMonth”:

 

countDaysByMonth = CALCULATE(DISTINCTCOUNT(timeTable[date]), ALLEXCEPT(timeTable,timeTable[Month]))

 

4. create measure “monthlySumHours”

 

monthlySumHours = CALCULATE([sumHours], ALLEXCEPT(timeTable, timeTable[Month]))

 

5. create measure “monthly average hour”:

 

monthly average hours = [monthlySumHours]/[countDaysByMonth]

 

6. Now create a table visual and move measures above to it. You will see that measure “monly average hours” is what you want.

 

3.png

 

 

 

 

 

 

 

View solution in original post

8 REPLIES 8
wylee1
New Member

Hello,

 

I am new to Power BI and facing an similiar issue but could not fix it. Can someone advice me?

DateTimeStart     DateTimeEnd        ToolName              Operation      User          sumhours
31-Oct-16 9:00      31-Oct-16 17:00    HiSeq 2000 Engg     TrainServ         Bernard     error

 

I try to add new column call "sumhours"  > sumHours = sumx(timeTable, DATEDIFF(timeTable[DateTimeStart], timeTable[DateTimeEnd], SECOND))/3600  but have error..  is "timeTable" refering to the table name?,?

 

 

Appreciate your time.. advance thanks

AlexChen
Employee
Employee

Hi,

 

I updated the data in your table to try to give an example. The data of February is from yours.

 

1.png

 

1. Firstly you have to create 2 new columns from “end date time” column. One is “Month”, the other is “Date”.

See screenshot below. You can create them from “query editor” -> “add column”.

 

2.png

 

2. create measure “sumHours”:

 

sumHours = sumx(timeTable, DATEDIFF(timeTable[starting date time], timeTable[end date time], SECOND))/3600

 

3. create measure “countDaysByMonth”:

 

countDaysByMonth = CALCULATE(DISTINCTCOUNT(timeTable[date]), ALLEXCEPT(timeTable,timeTable[Month]))

 

4. create measure “monthlySumHours”

 

monthlySumHours = CALCULATE([sumHours], ALLEXCEPT(timeTable, timeTable[Month]))

 

5. create measure “monthly average hour”:

 

monthly average hours = [monthlySumHours]/[countDaysByMonth]

 

6. Now create a table visual and move measures above to it. You will see that measure “monly average hours” is what you want.

 

3.png

 

 

 

 

 

 

 

Thanks a lot for your response. It helped!

 

RutchaNgam
Frequent Visitor

I need top create average each. rows (I s'nt sum average), I don't konw. How to use function in Power BI? 

samdthompson
Memorable Member
Memorable Member

1. Make a date table

2. Create average, min, max, sum etc measures based on start and end eg:

 

=sumx(table,table[end_time]-table[start_time])

 

OR

 

create calculated column [end_time] - [start_time]

create measure:

=
IF (
    HASONEVALUE ( table[difference] ),
    CALCULATE ( AVERAGE ( table[difference] )BLANK () )
)

 

 

3. make chart with month from date table on axis, measure from table as values.

// if this is a solution please mark as such. Kudos always appreciated.
RutchaNgam
Frequent Visitor

I want to know, How I can  create a New Measure DAX formula to sum up the number of Days between "starting date time" and "end date time" of two columns. Woman Very Happy

@RutchaNgam days elapsed=max(table[date])-min(table[date])

// if this is a solution please mark as such. Kudos always appreciated.

Thank you So much!Smiley Very Happy

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.