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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
DaniloMuni
New Member

DAX formula for working hour for each test from data.

Hello Everyone,

 

I need to figure out how many hours are spent on each individual test.
Each test has a different start date, and we need to indicate the hours elapsed for each test from the start of each one.

Below I show you an example of my table.

 

Test NameData TimeHour (for each test)

Test 1

16/01/2024 12:00:00 

Test 1

16/01/2024 13:00:00 

Test 1

16/01/2024 14:00:00 

test 2

17/01/2024 15:00:00 

Test2

17/01/2024 16:00:00 

Test 3

20/01/2024 09:00:00 

Test 3

20/01/2024 10:00:00 

Test 3

20/01/2024 11:00:00 

Test 3

20/01/2024 12:00:00 

 

Thank you very much to anyone who can solve this problem for me.

 

Bye!

1 ACCEPTED SOLUTION
v-zhengdxu-msft
Community Support
Community Support

Hi @DaniloMuni 

 

Please try this:

vzhengdxumsft_0-1708925649436.png

Add a calculate column:

Hour = 
VAR _PreviousTime =
    CALCULATE (
        MIN ( Data[Date Time] ),
        FILTER (
            ALLSELECTED ( 'Data' ),
            'Data'[Test Name] = EARLIER ( Data[Test Name] )
                && 'Data'[Date Time] < EARLIER ( Data[Date Time] )
        )
    )
RETURN
    IF (
        _PreviousTime = BLANK (),
        0,
        DATEDIFF ( _PreviousTime, 'Data'[Date Time], HOUR )
    )

The result is as follow:

vzhengdxumsft_1-1708925691746.png

If the above one can't help you, could you please provide more raw data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples? It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-zhengdxu-msft
Community Support
Community Support

Hi @DaniloMuni 

 

Please try this:

vzhengdxumsft_0-1708925649436.png

Add a calculate column:

Hour = 
VAR _PreviousTime =
    CALCULATE (
        MIN ( Data[Date Time] ),
        FILTER (
            ALLSELECTED ( 'Data' ),
            'Data'[Test Name] = EARLIER ( Data[Test Name] )
                && 'Data'[Date Time] < EARLIER ( Data[Date Time] )
        )
    )
RETURN
    IF (
        _PreviousTime = BLANK (),
        0,
        DATEDIFF ( _PreviousTime, 'Data'[Date Time], HOUR )
    )

The result is as follow:

vzhengdxumsft_1-1708925691746.png

If the above one can't help you, could you please provide more raw data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples? It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

jolind1996
Resolver II
Resolver II

One way is to use a calculated column in Power BI to calculate the duration of each test in hours. Here is an example of a calculated column that calculates the duration of each test in hours:

Duration (hours) = 
    DATEDIFF(
        MINX(FILTER('Table', 'Table'[Test Name] = EARLIER('Table'[Test Name])), 'Table'[Time]),
        MAXX(FILTER('Table', 'Table'[Test Name] = EARLIER('Table'[Test Name])), 'Table'[Time]),
        HOUR
    )

In this calculated column, 'Table'[Test Name] is the column containing the test names, and 'Table'[Time] is the column containing the time data. The DATEDIFF function calculates the difference between the minimum and maximum times for each test, and the result is returned in hours.

Once you have the calculated column, you can use it to create a measure that calculates the total number of hours spent on each test. Here is an example of a measure that calculates the total number of hours spent on each test:

Total Hours Spent = SUM('Table'[Duration (hours)])

In this measure, 'Table'[Duration (hours)] is the calculated column that contains the duration of each test in hours.

 

Best regards,

Johannes

Thank you very much, it was very helpfull, but I have another question.

 

Using your example I got the total hours for a single test, which is good, but I also need the individual hours for the row.

 

Example

 

est NameData TimeHour (for each test)

Test 1

16/01/2024 12:00:00                      0

Test 1

16/01/2024 13:00:00                      1

Test 1

16/01/2024 14:00:00                      2

test 2

17/01/2024 15:00:00                       0

Test2

17/01/2024 16:00:00                       1

Test 3

20/01/2024 09:00:00                       0

Test 3

20/01/2024 10:00:00                       1

Test 3

20/01/2024 11:00:00                       2

Test 3

20/01/2024 12:00:00                       3

 

Thanks!

Danilo

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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