Forum Discussion
datedif Function, only for working days (mon-Fri)
- 8 years ago
Did you use a disconnected table?
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.
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
- danextian8 years agoSuper User
Did you use a disconnected table?