Calendar starting on Monday

The weekday function in Power Apps gives us a numbers between 1 and 7 while the Calendar function can give you an array Sunday – Saturday.

Calendar functions

I will start this post by looking at the calendar functions.

The Calendar functions is a collection of functions that help you generate data used by calendars such as an array of months.

The Calendar.MonthsShort() function will generate [ “Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec” ].

In a similar way the Calendar.WeekdaysShort() will give you an array of [ “Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat” ]

And as we look at the standard Calendar screen template in Power Apps we can see that this function is being used to display the names of the days.

The Weekday and Calendar functions in Power Apps Microsoft Office 365 image 28

Where the array of months can be really helpful, the weekly days array is just a pain. As it starts on a Sunday. Many people use Monday as the first day of the week.

Weekday function

Next to the array of weekdays the Weekday function is there to give a number representing the day of the week. 1 is a Sunday, 2 is a Monday etc.

In the above mentioned Calendar screen template, there is a gallery that shows us all the numbers of the month and a few numbers before from the previous month and after from the next month. This makes the calendar days look good with 7 days shown with for each week.

This gallery uses a number array ( this looks like this [1,2,3,4,5,….41,42] ) and then for each of the items in the number array the gallery will check if it needs to be displayed or not. So for example day 42 is only displayed if the month starts on the last day of the week.

Set the visibility of the days

To decide the visibility of the days the following expression is used in the screen template.

!(DateAdd(_firstDayInView,ThisItem.Value,Days) - Weekday(DateAdd(_firstDayInView,ThisItem.Value,Days)) + 1 > _lastDayOfMonth)

This is great when you want to display the calendar starting on a Sunday, but what if you want to start on a Monday. Just like your Windows 10 does!

The Weekday and Calendar functions in Power Apps Microsoft Office 365 image 29

Consistency is great!

Fixing the calendar

Changing the day headings above the Calendar is easy. Just replace Calendar.WeekdaysShort() with the following:

[  "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" ]

The other change that we will need to make is to set the visibility of the number label to the following code:

 !(DateAdd(_firstDayInView,ThisItem.Value,Days) - If(Weekday(DateAdd(_firstDayInView,ThisItem.Value,Days))=1,7,Weekday(DateAdd(_firstDayInView,ThisItem.Value,Days)))  + 1 > _lastDayOfMonth)

The above change in code will make sure that we get day numbers in the right order, starting with Monday, as 1 and Sunday as 7

Within the Calendar screen template there are a few variables that are being set. To make this solution work we will need to make one more change.

In general I copy this code into the app start up, however in the screen template these variables are set in the OnSelect for the calendar dropdown.

The Weekday and Calendar functions in Power Apps Microsoft Office 365 image 30

There is a small change required to the calculation of the first day in view variable

Set(_dateSelected, Today());         
Set(_firstDayOfMonth, DateAdd(Today(), 1 - Day(Today()), Days));         
Set(_firstDayInView, DateAdd(_firstDayOfMonth, -(Weekday(_firstDayOfMonth) - 2 ), Days));         
Set(_lastDayOfMonth, DateAdd(DateAdd(_firstDayOfMonth, 1, Months), -1, Days));

And that’s all we have to do to fix the weekday issue within the Calendar in Power Apps.

By Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: