Forum Discussion

P_Crane31's avatar
P_Crane31
Frequent Visitor
3 years ago
Solved

DAX elapsed time calculation misbehaving?

Hello all!

 I have two seperate, but related questions on some strange results when I use DAX to calcualte elapsed business days from order palcement to order shipment. Both issues occur on both Desktop and Service. I am running the June Desktop update.

Please see the business case for further...

 

Business case: We are tracking how long a vendor takes to ship our products. One of our DBAs wrote a nice SQL/ M (I’m not sure which) query that gave us a raw calculation that shows time elapsed from order placement to the order being ready for shipment. However, the raw calc doesn’t take into account non-business days. I googled up the below DAX expression to at least filter out weekends. I couldn’t make the same thing for holidays. (Holidays aren’t the reason I’m writing, but if anyone has tips, I’d be happy to experiment…)

 

Column definitions:

order_initially_placed_at == I sent the order to the vendor for processing (date/time column)

order_received_in_hw == The vendor's system has acknowledged receiving the order (date/time column)

order_last_updated_at == Order has been assigned a tracking # and is awaiting pickup by the shipper (date/time column)

time_elapsed_since_order_placement == the raw elapsed time calculation (last_updated (-) initially placed) (text column)

business_days_elapsed == time elapsed rounded down to the day (whole number column)

 

Given this DAX calculation: 

 

 

business_days_elapsed = RoundDown (DateDiff (elapsed_time[order_received_in_hw], elapsed_time[order_last_updated_at], DAY) / 7, 0) * 5 + Mod (5 + Weekday (elapsed_time[order_last_updated_at]) - Weekday (elapsed_time[order_received_in_hw]), 5)

 

 

 

On the table “5 day question” (inside spoiler) why do some of the 5 day lags come out as “0” business days when the rest are correct? (This view is filtered to 5 day lag rows only) Aside from the other question below, the rest of the results are as accurate as they can be considering we’re rounding down to the full day.

Spoiler
Five Day Question table:    
order_initially_placed_atorder_received_in_hworder_last_updated_attime_elapsed_since_order_placementbusiness_days_elapsed
06/12/2023 13:3906/12/2023 14:0906/17/2023 23:185 days 09:09:00.2883060
06/12/2023 13:3906/12/2023 14:1006/17/2023 23:185 days 09:08:43.0125530
06/12/2023 13:4006/12/2023 14:1006/17/2023 23:185 days 09:08:31.7557380
06/12/2023 13:4006/12/2023 14:1006/17/2023 23:185 days 09:08:26.4842770
06/12/2023 13:4306/12/2023 14:1306/17/2023 23:185 days 09:05:33.2780150
06/12/2023 13:4306/12/2023 14:1306/17/2023 23:185 days 09:05:22.5023630
06/12/2023 14:1006/12/2023 14:4006/17/2023 23:185 days 08:38:38.7311420
06/12/2023 17:5006/12/2023 18:2006/17/2023 23:185 days 04:58:19.2045030
06/12/2023 17:5006/12/2023 18:2006/17/2023 23:195 days 04:58:20.5011870
06/02/2023 15:0106/02/2023 15:3106/07/2023 17:455 days 02:13:38.8013613
06/09/2023 16:0506/09/2023 16:3506/14/2023 22:305 days 05:54:35.9175973
06/09/2023 16:1206/09/2023 16:4206/14/2023 22:305 days 05:47:50.0589663
06/09/2023 16:1206/09/2023 16:4206/14/2023 22:305 days 05:47:31.6066083
06/09/2023 16:1206/09/2023 16:4206/14/2023 22:305 days 05:47:31.5930113
06/14/2023 14:0706/14/2023 14:3706/19/2023 22:015 days 07:23:57.215623
06/14/2023 14:0706/14/2023 14:3706/19/2023 22:015 days 07:23:58.5718583
06/14/2023 14:0806/14/2023 14:3806/19/2023 22:015 days 07:23:01.9506913
06/14/2023 14:0806/14/2023 14:3806/19/2023 22:015 days 07:23:03.3758063
06/14/2023 17:2506/14/2023 17:5506/19/2023 22:015 days 04:05:40.7525093
06/14/2023 17:2506/14/2023 17:5506/19/2023 22:015 days 04:05:42.2359723
06/14/2023 17:2506/14/2023 17:5506/19/2023 22:015 days 04:05:43.655813
06/09/2023 17:4406/09/2023 18:1406/15/2023 15:305 days 21:15:19.5486664
06/09/2023 17:5006/09/2023 18:2006/15/2023 15:305 days 21:10:11.3528124
06/14/2023 14:0606/14/2023 14:3606/20/2023 13:175 days 22:40:44.8079814
06/14/2023 14:0606/14/2023 14:3606/20/2023 13:175 days 22:40:46.1289324
    

 

 

A separate but related question: Using the same DAX calculation above, if one of the columns is “null”, why doesn’t the result always show as “0” on the report? The image below is a sample from the same table on Power Query. On the report (inside spoiler below the image), a couple do show “0”, but the rest… well, I have no idea where PBI is coming up with those results.

 

Correct null results showing in Power Query

 

Spoiler
order_initially_placed_atorder_received_in_hworder_last_updated_attime_elapsed_since_order_placementbusiness_days_elapsed
06/08/2023 14:3506/08/2023 15:05  0
06/09/2023 16:0206/09/2023 16:32  4
06/09/2023 16:0206/09/2023 16:32  4
06/09/2023 16:0206/09/2023 16:32  4
06/09/2023 16:0206/09/2023 16:32  4
06/09/2023 16:0206/09/2023 16:32  4
06/09/2023 16:0306/09/2023 16:33  4
06/09/2023 16:1306/09/2023 16:44  4
06/09/2023 16:1406/09/2023 16:44  4
06/09/2023 17:2206/09/2023 17:52  4
06/09/2023 17:2506/09/2023 17:55  4
06/09/2023 17:3206/09/2023 18:02  4
06/09/2023 17:3606/09/2023 18:06  4
06/09/2023 17:4206/09/2023 18:12  4
06/09/2023 17:4606/09/2023 18:16  4
06/09/2023 17:5106/09/2023 18:21  4
06/09/2023 17:5406/09/2023 18:24  4
06/09/2023 17:5706/09/2023 18:27  4
06/09/2023 17:5906/09/2023 18:30  4
06/09/2023 18:0606/09/2023 18:36  4
06/12/2023 16:1806/12/2023 16:48  3
06/12/2023 16:3406/12/2023 17:04  3
06/12/2023 16:3506/12/2023 17:05  3
06/12/2023 16:3906/12/2023 17:09  3
06/12/2023 16:4406/12/2023 17:14  3
06/12/2023 16:5006/12/2023 17:20  3
06/12/2023 16:5806/12/2023 17:28  3
06/13/2023 13:3906/13/2023 14:09  2
06/13/2023 13:3906/13/2023 14:09  2
06/13/2023 17:1506/13/2023 17:45  2
06/13/2023 17:2706/13/2023 17:57  2
06/14/2023 17:1706/14/2023 17:47  1
06/14/2023 17:3006/14/2023 18:00  1
06/15/2023 14:4406/15/2023 15:14  0

Right now, I use conditional formatting on the column to make the text white so it is kinda hidden when the “last_updated_at” column is null. I’d love for the cells to be blank or “0” (or even “N/A”) when one of the columns in the calculation is null, but I’d settle for just understanding why PBI is behaving like this.

 

Any thoughts on either question would be appreciated!

  • I think you can just use the NETWORKDAYS function, e.g.

    business_days_elapsed =
    IF (
        NOT ( ISBLANK ( elapsed_time[order_received_in_hw] ) )
            && NOT ( ISBLANK ( elapsed_time[order_last_updated_at] ) ),
        NETWORKDAYS (
            elapsed_time[order_received_in_hw],
            elapsed_time[order_last_updated_at]
        )
    )
    

    You can also provide to the NETWORKDAYS function a list of dates to consider as holidays.

5 Replies

  • I think you can just use the NETWORKDAYS function, e.g.

    business_days_elapsed =
    IF (
        NOT ( ISBLANK ( elapsed_time[order_received_in_hw] ) )
            && NOT ( ISBLANK ( elapsed_time[order_last_updated_at] ) ),
        NETWORKDAYS (
            elapsed_time[order_received_in_hw],
            elapsed_time[order_last_updated_at]
        )
    )
    

    You can also provide to the NETWORKDAYS function a list of dates to consider as holidays.

    • P_Crane31's avatar
      P_Crane31
      Frequent Visitor

      Thank you johnt75 That's super helpful. I'll need to do some research on how to implement it fully. 

       

      I'm trying to learn how to read/ write DAX (I've been using point 'n click so far). Am I reading that expression correctly: 

      "If elapsed_time[order_received_in_hw] is not blank AND elapsed_time[order_last_updated_at] is not blank, THEN  do the Networkdays function"

      Thanks again!

  • P_Crane31's avatar
    P_Crane31
    Frequent Visitor

    Oops... I just found the DAX formatter...

     

    business_days_elapsed =
    ROUNDDOWN (
        DATEDIFF (
            elapsed_time[order_received_in_hw],
            elapsed_time[order_last_updated_at],
            DAY
        ) / 7,
        0
    ) * 5
        + MOD (
            5 + WEEKDAY ( elapsed_time[order_last_updated_at] )
                - WEEKDAY ( elapsed_time[order_received_in_hw] ),
            5
        )