I'm using the following code in a Blank Query in Power BI Desktop:
(InitialDate as date, FinalDate as date ) as number =>
let
DaysBetweenDates = Duration.Days(FinalDate-InitialDate),
DaysList = List.Dates(List.Min({InitialDate,FinalDate}),Number.Abs(DaysBetweenDates), Duration.From(1)),
WeekDaysList = List.Select(DaysList, each (Date.DayOfWeek(_, Day.Monday) < 5) ),
WorkingDays = (if DaysBetweenDates < 0 then -1 else 1) * List.Count(WeekDaysList)
in
WorkingDays
It's code to calculate the working days (that is, excluding Saturday and Sunday) between 2 given dates. I use it using the Invoke Custom Function when adding a column to the query, as shown below:
The problem is that, when one or the two dates are empty (blank) the Invoked Function returns Error. See below:
How can I change the code in order to ignore the cases where one or the two dates are empty/blank, and just return blank?
If not possible, is there a way to make the same function in DAX?
Thank you!