Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
ChrisBroome
Frequent Visitor

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-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

 

however, I don't always have dates available in these columns, and so where this is the case, I get an error. Can anyone suggest a way to prevent me getting these errors flagged? I'd be fine with just Null values in these instances, but can't work out how to get these to show instead of the error.

 

Regards

2 ACCEPTED SOLUTIONS
CNENFRNL
Community Champion
Community Champion

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 ""

 

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

AlB
Community Champion
Community Champion

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 

SU18_powerbi_badge

 

View solution in original post

3 REPLIES 3
Icey
Community Support
Community Support

Hi @ChrisBroome ,

 

Please let us know if the replies above are helpful.

 

If they are, please always accept the replies making sense as solution to your question so that people who may have the same question can get the solution directly.

 

If not, please give us more details.

 

 

Best Regards,

Icey

AlB
Community Champion
Community Champion

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 

SU18_powerbi_badge

 

CNENFRNL
Community Champion
Community Champion

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 ""

 

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.