Forum Discussion

DaniloMuni's avatar
DaniloMuni
New Member
2 years ago
Solved

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!

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi DaniloMuni 

     

    Please try this:

    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:

    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.

3 Replies

  • 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

    • DaniloMuni's avatar
      DaniloMuni
      New Member

      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

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi DaniloMuni 

     

    Please try this:

    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:

    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.