Forum Discussion

jackisc's avatar
jackisc
New Member
2 years ago
Solved

Plz help with Dax

My clock in and clock out date is in rows.

Below DAX helped me subtract the break time of 45 minutes from the employees who worked 8hrs or more perday since it's mandatory. However, I wanted to edit it to include another clause where if employee who works 6hrs or less then "Do not deduct break time" but I am unable to make it work. Any suggestions on how I can achieve this with current DAX? Also, if there is better and easier way to achieve the end results please suggest that as well. Thank you!

 

Target Wrk Hrs =
VAR _a =
    SUMX ('Table1',
        IF (
         [Index] = 2,
         DATEDIFF (
                CALCULATE (
                    MAX ('Table1'[Check-in or Check-out Time] ),
                    FILTER ('Table1', [Index] = 1
                            && [Check-in or Check-out Time] < EARLIER ( 'Table1'[Check-in or Check-out Time])
                            && [Enter Reporting Date] = EARLIER ( [Enter Reporting Date] )
                    )
                ),
                [Check-in or Check-out Time],
                MINUTE
        )
        )
    )
 VAR _b=    
SUMX ('Table1',
IF (
            [Index] = 1,
            DATEDIFF (
                CALCULATE (
                    MAX ('Table1'[Check-in or Check-out Time]),
                    FILTER ('Table1',[Enter Reporting Date] = EARLIER ('Table1'[Enter Reporting Date])
                            && [Index] = 2
                            && [Check-in or Check-out Time] < EARLIER('Table1'[Check-in or Check-out Time])
                    )
                ),
                [Check-in or Check-out Time],
                MINUTE
            )
        )
    )
VAR _c=
    IF ( _b < 45, 45, 0)
VAR _d = ( _a - _c ) / 60
RETURN
    INT ( _d )
        + 0.01
            * MOD ( _a - _c, 60 )
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi  jackisc ,

     

    Here are the steps you can follow:

    1. In Power Query -- add Column – Index Column – From 1.

    2. Create calculated column.

    Date_time =
    [Date]+[Time]
    Test01 =
    var _last=
    MAXX(
        FILTER('Table',
        'Table'[Index.1]=EARLIER('Table'[Index.1])-1&&'Table'[In/Out]="In"),[Date_time])  
    return
    DATEDIFF(
       _last, [Date_time] , HOUR)
    Test02 =
    var _result=
    IF(
        [Test01]=BLANK(),
    MAXX(
        FILTER(ALL('Table'),'Table'[Index.1]=EARLIER('Table'[Index.1])+1&&'Table'[In/Out]="Out"),[Test01]),[Test01])
    return
    IF(
        _result>=8,"8hr - subtracting 45 minutes of break time","6hr - no need to subtract break time")

    3. Result:

     

     

    Best Regards,

    Liu Yang

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

5 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    jackisc Sorry, having trouble following, can you post sample data as text and expected output?
    Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

    Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

    The most important parts are:
    1. Sample data as text, use the table tool in the editing bar
    2. Expected output from sample data
    3. Explanation in words of how to get from 1. to 2.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi  jackisc ,

     

    Here are the steps you can follow:

    1. In Power Query -- add Column – Index Column – From 1.

    2. Create calculated column.

    Date_time =
    [Date]+[Time]
    Test01 =
    var _last=
    MAXX(
        FILTER('Table',
        'Table'[Index.1]=EARLIER('Table'[Index.1])-1&&'Table'[In/Out]="In"),[Date_time])  
    return
    DATEDIFF(
       _last, [Date_time] , HOUR)
    Test02 =
    var _result=
    IF(
        [Test01]=BLANK(),
    MAXX(
        FILTER(ALL('Table'),'Table'[Index.1]=EARLIER('Table'[Index.1])+1&&'Table'[In/Out]="Out"),[Test01]),[Test01])
    return
    IF(
        _result>=8,"8hr - subtracting 45 minutes of break time","6hr - no need to subtract break time")

    3. Result:

     

     

    Best Regards,

    Liu Yang

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