Forum Discussion

BachFel's avatar
BachFel
Helper II
8 years ago
Solved

datedif Function, only for working days (mon-Fri)

Hello,

 

follwing problem.

I want to create a DAX formula which shows the difference from a specific date to today.

 

Tage_seit_erster_Objektbegung = if(KontElemente[Datum der Besichtigung]<>BLANK();TODAY()-KontElemente[Datum der Besichtigung].[Date];BLANK())

 

Is it possible to count only the working days (monday-Friday).

 

Thanks in advance

17 Replies

  • vanessafvg's avatar
    vanessafvg
    Community Champion

    BachFel i would create a flag for the working days, ie a new column calls isworkingday = 1

     

    and then use that in your calculation

  • Hi BachFel,

     

    I would follow the following approach. 

     

    First, I would create a disconnected calendar table ( a table that has no relationship with your Fact table) either in DAX or in M which contains all the dates that my fact table has. The calendar table would indicate 1 if a day is a weekday and 0 if otherwise. Here's my DAX formula:

    Calendar (Disconnected) =
    VAR START_DATE_ =
        DATE ( 2018, 3, 1 )
    VAR END_DATE_ =
        DATE ( 2018, 4, 20 )
    VAR DATES_ =
        CALENDAR ( START_DATE_, END_DATE_ )
    RETURN
        ADDCOLUMNS (
            DATES_,
            "Name of Day", FORMAT ( [Date], "ddd" ),
            "Is Weekday?", IF (
                FORMAT ( [Date], "ddd" ) = "Sat"
                    || FORMAT ( [Date], "ddd" ) = "Sun",
                0,
                1
            )
        )

    You may edit the START_DATE_ and END_DATE_ variables above as desired.

     

    In my fact table, I would create a calculated column that sums the value in Is Weekday? column fromt the disconnected calendar table filtered by a specific date till today. Here's my DAX formula: 

    Workday Difference =
        CALCULATE (
            SUM ( 'Calendar (Disconnected)'[Is Weekday?] ),
            DATESBETWEEN ( 'Calendar (Disconnected)'[Date], 'Fact'[Date], TODAY () )
        )
            - 1
    
    

    Notice that added -1 after the latest parenthesis. This is  because the sum that is being returned is the sum from start to end dates and not the difference between the two.

    • BachFel's avatar
      BachFel
      Helper II

      Hi danextian,

       

      i created a calender table where weekdays have a 1 and sat/ sun has a 0. This column in is the table called:

      Datumstabelle[IsWorkingDay]

       

      then I added your second formula. But the result is wrong.

       

      I´m not sure about: DATESBETWEEN (Datumstabelle[Daten]. This is the column in my calender with all possible dates.

      I´ve no clue where the mistake could be 

      • danextian's avatar
        danextian
        Super User

        Did you use a disconnected table?