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
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
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.