PowerApps Formulae - Exclude weekend formulae

While working on PowerApps I had a requirement from client to exclude the weekends and if user select the weekend throw an error and set the date selected to Friday if user select the date is Saturday or Sunday. By default we don't have option in PowerApps to achieve this functionality. But using the formulas below we can achieve this.

Update following code on "OnChange" property of the Date picker. 

Weekday(
        Self.SelectedDate,
        StartOfWeek.Monday
    ) > 5,
    Notify(
        "You are not allowed to select Weekend. Please Select Date Monday - Friday",
        NotificationType.Error
    );

The formulae above will verify the selected day is Weekday or not. if it is not weekday, it will throw error.

In the second part, we will set the Friday value if user choose Saturday or Sunday.

 Set(
        varUpdatedDate,
        DateAdd(
            Self.SelectedDate,
            -(Weekday(
                Self.SelectedDate,
                StartOfWeek.Monday
            ) - 5),
            Days
        )
    );

In the above we are setting the a variable to default value.

Set the Variable varUpdatedDate to Default property of the Date field and In the Page - OnVisible formulae, update the formulae below.

Set(varUpdatedDate, Today())

Update following formulae on OnChange of the DateTime picker picker to achive the required functionality.

If(
Weekday(
        Self.SelectedDate,
        StartOfWeek.Monday
    ) > 5,
    Notify(
        "You are not allowed to select Weekend. Please Select Date Monday - Friday",
        NotificationType.Error
    );
Set(
        varUpdatedDate,
        DateAdd(
            Self.SelectedDate,
            -(Weekday(
                Self.SelectedDate,
                StartOfWeek.Monday
            ) - 5),
            Days
        )
    );
Reset(Self);
)

Comments

Popular posts from this blog

Linksys Wireless Connection Failure

The security validation for this page is invalid. Click Back in your Web

Power Apps toPower Automate Error - The input body for trigger 'manual' of type 'Request' did not match its schema definition , the input String does not validate against the date