Forum Discussion

Dharani_98's avatar
Dharani_98
Frequent Visitor
6 years ago
Solved

Time And Date In Power BI Desktop

I wanted to create a calculated column that returns the total working hours based on the starting Date.The result of my calculated column should be [9,4:30,13]. i.e B worked for 9 hours on 6-4-2020...
  • Icey's avatar
    6 years ago

    Hi Dharani_98 ,

     

    I create a Measure, not a Calculated Column. Please check:

     

    1. Create [StartDate] column. 

    StartDate = DATEVALUE('Table'[StartDate and Time])

     

    2. Create a Calendar table.

    Calendar = 
    CALENDAR (
        MINX ( 'Table', DATEVALUE ( 'Table'[StartDate and Time] ) ),
        MAXX ( 'Table', DATEVALUE ( 'Table'[End Date And Time] ) )
    )

     

    3. Create relationship.

     

    4. Create [Rank_] measure.

    Rank_ =
    RANKX (
        ALLSELECTED ( 'Table' ),
        CALCULATE ( MAX ( 'Table'[StartDate] ) ),
        ,
        ASC,
        DENSE
    )
    

     

    5. Create [Salary Measure].

    Salary Measure = 
    VAR LastRank = [Rank_] - 1
    VAR LastStartDateTime =
        CALCULATE (
            MAX ( 'Table'[StartDate and Time] ),
            FILTER ( ALLSELECTED ( 'Table' ), [Rank_] = LastRank )
        )
    VAR LastEndDateTime =
        CALCULATE (
            MAX ( 'Table'[End Date And Time] ),
            FILTER ( ALLSELECTED ( 'Table' ), [Rank_] = LastRank )
        )
    VAR LastDateDiff =
        DATEDIFF ( LastStartDateTime, LastEndDateTime, DAY )
    VAR ThisDateDiff =
        DATEDIFF (
            MAX ( 'Table'[StartDate and Time] ),
            MAX ( 'Table'[End Date And Time] ),
            DAY
        )
    VAR StartDateTime =
        IF (
            LastDateDiff = 1,
            CONVERT (
                DATEVALUE ( MAX ( 'Table'[StartDate and Time] ) ) & " "
                    & TIME ( 0, 0, 0 ),
                DATETIME
            ),
            MAX ( 'Table'[StartDate and Time] )
        )
    VAR EndDateTime =
        IF (
            ThisDateDiff = 1,
            CONVERT (
                DATEVALUE ( MAX ( 'Table'[End Date And Time] ) ) & " "
                    & TIME ( 0, 0, 0 ),
                DATETIME
            ),
            MAX ( 'Table'[End Date And Time] )
        )
    VAR SpecifiedStartTime =
        CONVERT (
            DATEVALUE ( MAX ( 'Table'[StartDate and Time] ) ) & " "
                & TIME ( 8, 0, 0 ),
            DATETIME
        )
    VAR SpecifiedEndTime =
        CONVERT (
            DATEVALUE ( MAX ( 'Table'[StartDate and Time] ) ) & " "
                & TIME ( 17, 0, 0 ),
            DATETIME
        )
    VAR Result =
        IF (
            LastDateDiff = 1,
            IF (
                EndDateTime > SpecifiedStartTime
                    && EndDateTime <= SpecifiedEndTime,
                DATEDIFF ( StartDateTime, LastEndDateTime, MINUTE ) / 60 * 200
                    + DATEDIFF ( MAX ( 'Table'[StartDate and Time] ), EndDateTime, MINUTE ) / 60 * 100,
                IF (
                    EndDateTime > SpecifiedEndTime,
                    DATEDIFF ( StartDateTime, LastEndDateTime, MINUTE ) / 60 * 200
                        + DATEDIFF ( MAX ( 'Table'[StartDate and Time] ), SpecifiedEndTime, MINUTE ) / 60 * 100
                        + DATEDIFF ( SpecifiedEndTime, EndDateTime, MINUTE ) / 60 * 200
                )
            ),
            IF (
                LastDateDiff <> 1,
                IF (
                    StartDateTime >= SpecifiedStartTime
                        && EndDateTime <= SpecifiedEndTime,
                    DATEDIFF ( StartDateTime, EndDateTime, MINUTE ) / 60 * 100,
                    IF (
                        StartDateTime >= SpecifiedStartTime
                            && StartDateTime < SpecifiedEndTime
                            && EndDateTime > SpecifiedEndTime,
                        DATEDIFF ( StartDateTime, SpecifiedEndTime, MINUTE ) / 60 * 100
                            + DATEDIFF ( SpecifiedEndTime, EndDateTime, MINUTE ) / 60 * 200,
                        IF (
                            StartDateTime >= SpecifiedEndTime,
                            DATEDIFF ( StartDateTime, EndDateTime, MINUTE ) / 60 * 200
                        )
                    )
                )
            )
        )
    RETURN
        Result

     

    6. Create a table visual.

     

    For more details, please check the attached PBIX file.

     

     

     

    Best Regards,

    Icey

     

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