Forum Discussion

AlienBI's avatar
AlienBI
Frequent Visitor
1 year ago
Solved

Calculating hours for each dates between 2 date columns

Hi there,  I've got a little problem which I hope you can help me with. Got a table like this one here (thousands of rows) - car rental business so you understand what this means: Confirmation...
  • pankajnamekar25's avatar
    1 year ago

    Hello AlienBI 

     

    Try this DAX Measure

    OnRent =
    VAR tmpOnRent =
    ADDCOLUMNS (
    'DemandPlotterRA+RES',
    "Pickup", 'DemandPlotterRA+RES'[Pick up date],
    "Dropoff", 'DemandPlotterRA+RES'[Drop off date]
    )
    VAR tmpTable =
    ADDCOLUMNS (
    FILTER (
    GENERATE ( tmpOnRent, 'Calendar' ),
    [Date] >= INT ( [Pickup] )
    && [Date] <= INT ( [Dropoff] )
    ),
    "RentalHours",
    VAR StartOfDay = [Date] // midnight
    VAR EndOfDay = [Date] + 1 // next midnight
    VAR StartTime = MAX ( [Pickup], StartOfDay )
    VAR EndTime = MIN ( [Dropoff], EndOfDay )
    RETURN
    DIVIDE ( DATEDIFF ( StartTime, EndTime, MINUTE ), 60, 24 )
    )
    RETURN
    SUMX ( tmpTable, [RentalHours] / 24 )