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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
rachaelwalker
Resolver II
Resolver II

Business days between two dates and to ignore null

I have two date columns. I am calculating the business days between the two dates but it is returning 0 days even for fields that are null. Is there a way to ignore those that do not have two dates and leave it blank? I am using this to calculate business hours // fnWorkHours(WHStarts, WHEnds, Ticket Start Date/Time, Ticket End Date/Time, Holidays)/WHDuration)

 

Here is the calculated column from within power query

 

 

Days = if [Date_Picked] <> null then fnWorkHours(WHStarts, WHEnds,[Date_Picked], if [Date_Req] <> null then [Date_Req] else null, Holidays)/WHDuration else null

 

 

rachaelwalker_0-1647955680550.png

I would like "Days" to return blanks for top 6 that have missing dates. And if possible to show a negative amount when the Date Req is greater than Date Picked. Currently it is showing 0 as well

 

 

 

2 REPLIES 2
v-yingjl
Community Support
Community Support

Hi @rachaelwalker ,

Is there a way to ignore those that do not have two dates and leave it blank?

You can try to define the null value first in the if statement when creating the custom column, like this:

= if [Date_Picked] = null or [Date_Req] = null then null else nWorkHours(WHStarts, WHEnds,[Date_Picked], if [Date_Req] <> null then [Date_Req] else null, Holidays)/WHDuration

 

 if possible to show a negative amount when the Date Req is greater than Date Picked

If you also wants to show a negative amount, perhaps you can consider sharing your custom function fnWorkHours like @ jennratten mentioned.

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

jennratten
Super User
Super User

Hello - this is how you can evaluate the difference between the dates, returning null values for records without two dates and a negative value if the start date is greater than the end date.  If this does not work for you pls post the script for your custom function fnWorkHours - that may need to be modified.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtc3MgAiI0MFQ0MrAwOFxFwlHSWlWJ1oJQt9Q1OojBFIpgAuY6lviCFhqA8yychIwcTKxFQhwBes0NAAYTohpYb6hliUGsKsQlaLVwLD8FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [StartDate = _t, EndDate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"StartDate", type datetime}, {"EndDate", type datetime}}),
    #"Inserted Date Subtraction" = Table.AddColumn(#"Changed Type", "Days", each Duration.Days([EndDate] - [StartDate]), type number)
in
    #"Inserted Date Subtraction"

 

jennratten_0-1647957100480.png

 

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors