Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Difference between dates considering only working days

I am having a little challenge counting the right number of days between two dates. If I use DateDiff function it disregards the fact that there are weekend days in the calendar. If I use Datesbetw...
  • v-easonf-msft's avatar
    5 years ago

    Hi, Anonymous 

    You need to created  calculated column Is_WorkDay =1 or 0 to indicate if its a workday  or not in your calendar table. 

    Is_workday = IF( NOT WEEKDAY('Calendar'[Date],2)   in {6,7},1,0) 

    To avoid the situation where the start date is greater than the end date,you can create a calculated column as below in you fact table (here we set two variables "max1"  and "min1" to get the date in the table)

    _Workdays = 
    VAR max1 =
        MAX ( 'Table'[StartDate], 'Table'[EndDate] )
    VAR min1 =
        MIN ( 'Table'[StartDate], 'Table'[EndDate] )
    RETURN
        CALCULATE (
            SUM ( 'Calendar'[Is_workday] ),
            ALL ( 'Calendar' ),
            DATESBETWEEN ( 'Calendar'[Date], min1, max1 )
        )

     

    Best Regards,
    Community Support Team _ Eason