Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Need Help! Calculations based on Date Filters

Hello All,   Need help in handling couple of scenarios involving dates, i have a 2 date columns Start Date and Finish Date, and need to calculate number of days based on the below scenarios (Start ...
  • v-alq-msft's avatar
    6 years ago

    Hi, Anonymous 

     

    Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

     

    Table:

     

    Calendar(a calculated table):

     

    Calendar = CALENDARAUTO()

     

     

    There is no relationship between two tables. You may create a measure as below.

     

    Result = 
    var _minslicerdate = CALCULATE(MIN('Calendar'[Date]),ALLSELECTED('Calendar'))
    var _maxslicerdate = CALCULATE(MAX('Calendar'[Date]),ALLSELECTED('Calendar'))
    var tab = 
    ADDCOLUMNS(
        'Table',
        "Result",
        var _startdate = [Start Date]
        var _finishdate = [Finish Date]
        return
        SWITCH(
             TRUE(),
            _startdate>=_minslicerdate&&_finishdate<=_maxslicerdate,
            DATEDIFF(_startdate,_finishdate,DAY),
            _startdate<_minslicerdate&&_finishdate>=_minslicerdate&&_finishdate<=_maxslicerdate,
            DATEDIFF(_minslicerdate,_finishdate,DAY),
            _startdate<_minslicerdate&&_finishdate>_maxslicerdate,
            DATEDIFF(_minslicerdate,_maxslicerdate,DAY),
            _startdate>=_minslicerdate&&_startdate<=_finishdate&&_finishdate>_maxslicerdate,
            DATEDIFF(_startdate,_maxslicerdate,DAY)
        )
    )
    return
    SUMX(
        tab,
        [Result]
    )

     

     

    Then you need to use the date column from 'Calendar' table to filter the result.

     

    Best Regards

    Allan

     

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