Forum Discussion
NETWORKDAYS with multiple country holidays
- 8 years ago
HiAnonymous
For this use case, you need to calculate first the number of weekdays within your specified date range then deduct the number of weekdays that are holidays. Here's what I've come up with:
NETWORKDAYS W/ HOLIDAYS = VAR HOLIDAYS_ = CALCULATE ( SUM ( Holidays[Is Weekday?] ), DATESBETWEEN ( Holidays[Date], 'Processed Data'[Date of failure], 'Processed Data'[Date of repair] ), Holidays[Country] = EARLIER ( 'Processed Data'[Country] ) ) VAR REGuLAR_NETWORKKDAYS_ = CALCULATE ( SUM ( Dates[Is Weekday?] ), DATESBETWEEN ( Dates[Date], 'Processed Data'[Date of failure], 'Processed Data'[Date of repair] ) ) RETURN REGULAR_NETWORKKDAYS_ - HOLIDAYS_Please note that:
- HOLIDAYS and Dates tables are disconnected tables. They don't have any relationships with your fact table.
- DATESBETWEEN() behaves similarly with NETWORKDAYS in Excel. It aggregates the values within a specified date range and not the difference between the end and start dates. That being said, you might want to deduct 1 from the measure above and return 0 if after deducting 1 the result is negative.
Also, you can create a date calendar in DAX or M without pulling in data from an externa source. Here are sample codes:
M -
let Source = List.Dates(#date(2018, 1, 1), Number.From(DateTime.Date(DateTime.LocalNow()) - #date(2018,1,1)) +1, #duration(1,0,0,0)), #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error), #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Date", type date}}), #"Inserted Day Name" = Table.AddColumn(#"Changed Type", "Day Name", each Date.DayOfWeekName([Date]), type text), #"Added Custom" = Table.AddColumn(#"Inserted Day Name", "Is Weekday?", each if [Day Name]="Saturday" or [Day Name]="Sunday" then 0 else 1, Int64.Type) in #"Added Custom"DAX
CALENDAR Table = CALENDAR ( MIN ( 'Processed Data'[Date of failure] ), MAX ( 'Processed Data'[Date of repair] ) )
You're welcome.
Hi danex,
I have the exact same problem as jcatindoy. I tried out your method, it didn't workout. An error occured: A date column containing duplicate dates was specified in the call to function 'DATESBETWEEN'. This is not supported.
Here is my code, could you please take a look at it? Thanks!!
businessday =
var _END =Fact[completedon]
var _START =Fact[createdon]
var _holiday =
CALCULATE (
SUM (Holidays[Isweekday]),
DATESBETWEEN (Holidays[holidaydate],_START,_END),
Holidays[countrycode]=EARLIER(Fact[countrycode])
)
var _workinghours =
SUMX(
CALCULATETABLE(
Dates,
DATESBETWEEN(Dates[Date],_START,_END),
Dates[IsWorkingDay] = TRUE()),
MAX(MIN(Dates[End],_END)-MAX(Dates[Start],_START),0)
)
return IF(_workinghours-_holiday>0,_workinghours-_holiday,BLANK())
- danextian4 years agoSuper User
Hi Gabiiiii,
The first argument in DATESBWEEN requires a Date column that doesn't contain duplicates. It is possible that either your Dates[Date] or Holidays[holidaydate] has the duplicates but the culprit is most likely the latter. I would use something like this instead:
VAR __HOLIDAYS = CALCULATE ( SUM ( Holidays[Weekday Holiday Count] ), FILTER ( Holidays, Holidays[Holiday] >= EARLIER ( Data[Start] ) && Holidays[Holiday] <= EARLIER ( Data[End] ) && Holidays[Country] = EARLIER ( Data[Country] ) ) )Please refer to this sample pbix - https://drive.google.com/file/d/1pbYAD2KZf-RFq2kf3vvmI0IcxHvwmoIg/view?usp=sharing