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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Lyevon1
Frequent Visitor

Adding SLA +1 day to a Delivery Date excluding Weekends & Public Holidays

Hi Sir/Mdm, 

 

I need some help with the Power BI query of adding a SLA of a day to a date while excluding Weekends and Public holidays

Have been trying to find similar answers here but has been futile.

 

Example: 30 Dec 2022 (friday) + 1 day SLA + excluding New Year Holiday; to return 4 Jan (since that 2 Jan is the New Year Observed Holiday + 1 day SLA)

In excel i could easily use workday to exclude the weekends and holidays + a day of SLA

 

Any advice and help with the code is truly appreciated. Thank youCapture11.PNG

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

I am not sure if I understood your question correctly, but please check the below formula and the attached pbix file.

It is for creating a new column.

 

Expected result CC =
VAR _calendar =
    ADDCOLUMNS (
        CALENDAR ( DATE ( 2022, 1, 1 ), DATE ( 2023, 12, 31 ) ),
        "@Dayname", FORMAT ( [Date], "dddd" )
    )
VAR _holidaycalendar =
    SUMMARIZE (
        FILTER (
            ADDCOLUMNS ( _calendar, "@holiday", [Date] IN DISTINCT ( Holiday[Holiday] ) ),
            [@holiday] = TRUE ()
        ),
        [Date]
    )
VAR _actualstartdate =
    ADDCOLUMNS (
        Data,
        "@actualstartdate",
            MINX (
                FILTER (
                    _calendar,
                    [Date] >= EARLIER ( Data[Delivery date] )
                        && NOT ( [Date] IN _holidaycalendar )
                        && [@Dayname] <> "Saturday"
                        && [@Dayname] <> "Sunday"
                ),
                [Date]
            )
    )
VAR _networkdays =
    FILTER (
        ADDCOLUMNS (
            GENERATE (
                _actualstartdate,
                CALENDAR ( [@actualstartdate], [@actualstartdate] + 30 )
            ),
            "@networkdays", NETWORKDAYS ( [@actualstartdate], [Date], 1, _holidaycalendar )
        ),
        [@networkdays] = 3
    )
RETURN
    MINX (
        FILTER ( _networkdays, Data[Delivery date] = EARLIER ( Data[Delivery date] ) ),
        [Date]
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

6 REPLIES 6
FreemanZ
Super User
Super User

hi @Lyevon1 

 

Try to

1) load the holidays as a table.

2) create a DateTable like this:

 

DateTable = 
ADDCOLUMNS(
    CALENDAR(DATE(2022,12,1), DATE(2023,2,1)),
    "IsHolidayWeekend",    
    IF(
        [Date] IN ALL(Holiday[Date])
            || WEEKDAY([Date],2) IN {6,7},
        "Yes", "No"
    )
)

 

 3) add a column in your delivery table like this:

 

ExpectedDeliveryDate2 = 
VAR _date = [DeliveyDate]
RETURN 
MINX(
    FILTER(
        DateTable,
        DateTable[IsHolidayWeekend] = "No"
            &&DateTable[Date]>_date
    ),
    DateTable[Date]
)+1

 

 

I tried and it worked like this:

FreemanZ_0-1672121007577.png

 

Jihwan_Kim
Super User
Super User

Hi,

I am not sure if I understood your question correctly, but please check the below formula and the attached pbix file.

It is for creating a new column.

 

Expected result CC =
VAR _calendar =
    ADDCOLUMNS (
        CALENDAR ( DATE ( 2022, 1, 1 ), DATE ( 2023, 12, 31 ) ),
        "@Dayname", FORMAT ( [Date], "dddd" )
    )
VAR _holidaycalendar =
    SUMMARIZE (
        FILTER (
            ADDCOLUMNS ( _calendar, "@holiday", [Date] IN DISTINCT ( Holiday[Holiday] ) ),
            [@holiday] = TRUE ()
        ),
        [Date]
    )
VAR _actualstartdate =
    ADDCOLUMNS (
        Data,
        "@actualstartdate",
            MINX (
                FILTER (
                    _calendar,
                    [Date] >= EARLIER ( Data[Delivery date] )
                        && NOT ( [Date] IN _holidaycalendar )
                        && [@Dayname] <> "Saturday"
                        && [@Dayname] <> "Sunday"
                ),
                [Date]
            )
    )
VAR _networkdays =
    FILTER (
        ADDCOLUMNS (
            GENERATE (
                _actualstartdate,
                CALENDAR ( [@actualstartdate], [@actualstartdate] + 30 )
            ),
            "@networkdays", NETWORKDAYS ( [@actualstartdate], [Date], 1, _holidaycalendar )
        ),
        [@networkdays] = 3
    )
RETURN
    MINX (
        FILTER ( _networkdays, Data[Delivery date] = EARLIER ( Data[Delivery date] ) ),
        [Date]
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Hi Mr Jihwan,
Quick question, if i have wish to have a separate table for the SLA, how should i go about doing that? 
apology for the question if this is a straight forward ans.. i'm still new to BI.

Hi,

I added a new table into the file by using the below formula.

It is for creating a new table.

Please check if it suits your requirement.

 

Expected result Table =
VAR _calendar =
    ADDCOLUMNS (
        CALENDAR ( DATE ( 2022, 1, 1 ), DATE ( 2023, 12, 31 ) ),
        "@Dayname", FORMAT ( [Date], "dddd" )
    )
VAR _holidaycalendar =
    SUMMARIZE (
        FILTER (
            ADDCOLUMNS ( _calendar, "@holiday", [Date] IN DISTINCT ( Holiday[Holiday] ) ),
            [@holiday] = TRUE ()
        ),
        [Date]
    )
VAR _actualstartdate =
    ADDCOLUMNS (
        Data,
        "@actualstartdate",
            MINX (
                FILTER (
                    _calendar,
                    [Date] >= EARLIER ( Data[Delivery date] )
                        && NOT ( [Date] IN _holidaycalendar )
                        && [@Dayname] <> "Saturday"
                        && [@Dayname] <> "Sunday"
                ),
                [Date]
            )
    )
VAR _networkdays =
    FILTER (
        ADDCOLUMNS (
            GENERATE (
                _actualstartdate,
                CALENDAR ( [@actualstartdate], [@actualstartdate] + 30 )
            ),
            "@networkdays", NETWORKDAYS ( [@actualstartdate], [Date], 1, _holidaycalendar )
        ),
        [@networkdays] = 3
    )
RETURN
    GROUPBY (
        _networkdays,
        Data[Delivery date],
        Data[Day Name],
        "@expectedresultdate", MINX ( CURRENTGROUP (), [Date] )
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Thank you so much Mr Jihwan, this works!. appreciate so much for the advice!

amitchandak
Super User
Super User

@Lyevon1 , You need to create column with flag of weekend and holiday, and then follow an approch like

https://community.powerbi.com/t5/Community-Blog/Travelling-Across-Workdays-Decoding-Date-and-Calenda...

 

Column code is given in the comments

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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

Top Kudoed Authors