Forum Discussion

ChrisBroome's avatar
ChrisBroome
Frequent Visitor
5 years ago
Solved

Null values where missing date

Hi,   I've got a query set to calculate the number of working days based on two dates; = (InitialDate as date, FinalDate as date ) as number => let DaysBetweenDates = Duration.Days(FinalDate-Ini...
  • CNENFRNL's avatar
    5 years ago

    Hi, ChrisBroome , a most straightforward way is to enclose the function invocation into a try ... otherwise to catch errors like this,

     

    Invoking = try Query1(#date(2020, 11, 10), null) otherwise ""

     

     

  • AlB's avatar
    5 years ago

    Hi ChrisBroome 

    Try this:

    (InitialDate as any, FinalDate as any ) as any =>
        if (InitialDate=null  or  FinalDate=null) then null else
            let
                DaysBetweenDates = Duration.Days(FinalDate-InitialDate),
                DaysList = List.Dates(List.Min({InitialDate,FinalDate}),Number.Abs(DaysBetweenDates)+1, 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

     

    Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers