A date-range calendar component for Power Apps
The booking calendar from our Asset Booking app, rebuilt as a reusable component. Drop it on a screen, set a few properties, and read the dates back. It marks today, picks a single day or a range, and respects booking rules such as blocked dates and maximum length.
Input properties
What your app sets. Change them and watch the calendar.
This copy of the component is called RangeCalendar1
How it works
The formulas below are the real ones from the component. The demo above runs the same rules in JavaScript so you can watch the values change as you click.
01State lives inside the component
Inside a component, Set() doesn't create app-wide variables. Each copy of the component gets its own _month, _start and _end, so two calendars on one screen never interfere, and nothing clashes with variables already in your app.
The app never touches those variables. It reads output properties instead, and clears the calendar with Reset(RangeCalendar1), which runs the component's OnReset.
// cmpRangeCalendar.OnReset Set(_start, Blank()); Set(_end, Blank()); Set(_month, Blank()) // Output properties (component → app) StartDate = _start EndDate = If(IsBlank(_start), Blank(), Coalesce(_end, _start)) DayCount = If(IsBlank(_start), 0, DateDiff(_start, Coalesce(_end, _start), TimeUnit.Days) + 1) IsComplete = !IsBlank(_end) // Month arrows (Previous uses -1) Set(_month, DateAdd(Coalesce(_month, Date(Year(Today()), Month(Today()), 1)), 1, TimeUnit.Months))
0242 days from Sequence(), rules worked out once
A month can touch six different weeks, so the grid is always 6 × 7 = 42 cells and never changes height. Weekday(first, StartOfWeek.Monday) - 1 is how far to step back from the 1st to land on a Monday.
Every "can't pick this day" rule from the input properties is folded into one IsDisabled flag per cell. The button just uses DisplayMode, so disabled days can't be clicked or tabbed to.
// rcDays.Items (WrapCount = 7) With( { first: Coalesce(_month, Date(Year(Today()), Month(Today()), 1)), minD: cmpRangeCalendar.MinDate, maxD: cmpRangeCalendar.MaxDate }, With( { offset: Weekday(first, StartOfWeek.Monday) - 1 }, ForAll( Sequence(42, 0) As n, With( { d: DateAdd(first, n.Value - offset, TimeUnit.Days) }, { CalDate: d, InMonth: Month(d) = Month(first), IsToday: d = Today(), IsDisabled: (!IsBlank(minD) && d < minD) || (!IsBlank(maxD) && d > maxD) || (cmpRangeCalendar.DisableWeekends && Weekday(d, StartOfWeek.Monday) > 5) || d in cmpRangeCalendar.BlockedDates.BlockedDate } ) ) ) )
03One button per cell, styled from state
Each cell is a single button. Fill asks: is this the start or end day? Is it in between? Coalesce(_end, _start) treats a half-finished selection as one day, so the start lights up straight away.
Colours come from one input, AccentColor. ColorFade() makes the lighter "in between" and hover shades from it, so re-theming is a one-property change.
Today gets a ring through BorderThickness, so it still shows when it's selected.
// rcDay.Fill With( { s: _start, e: Coalesce(_end, _start), d: ThisItem.CalDate }, If( !IsBlank(s) && (d = s || d = e), cmpRangeCalendar.AccentColor, !IsBlank(s) && d > s && d < e, ColorFade(cmpRangeCalendar.AccentColor, 0.88), RGBA(0, 0, 0, 0) ) ) // rcDay.BorderThickness If(ThisItem.IsToday, 2, 0) // rcDay.DisplayMode If(ThisItem.IsDisabled, DisplayMode.Disabled, DisplayMode.Edit)
04The selection rule
The whole selection logic comes down to one sentence: if this click can't finish a valid range, it starts a new one.
- Click any available day: it becomes the start.
- Click the start or a later day: it becomes the end.
- Click an earlier day, or one that would make the range too long or cross a blocked day: it becomes the new start.
- Click any day once a range is set: start again from there.
- Clear or
Reset(RangeCalendar1)empties it.
With AllowRange off, every click is a complete one-day selection.
// rcDay.OnSelect With( { d: ThisItem.CalDate }, If( // Single-day mode: every click is a complete selection !cmpRangeCalendar.AllowRange, Set(_start, d); Set(_end, d), // Start a new selection when... IsBlank(_start) // nothing picked yet || !IsBlank(_end) // a range is already complete || d < _start // clicked before the start || (cmpRangeCalendar.MaxDays > 0 // range would be too long && DateDiff(_start, d, TimeUnit.Days) + 1 > cmpRangeCalendar.MaxDays) || CountRows( // range would cross a blocked day Filter(cmpRangeCalendar.BlockedDates, BlockedDate > _start && BlockedDate < d) ) > 0, Set(_start, d); Set(_end, Blank()), // ...otherwise finish it (same day = one-day selection) Set(_end, d) ) )
Properties
These are everything your app needs to know about the component. Inputs have sensible defaults, so a plain copy works with nothing set.
| Property | Type | Default | What it does |
|---|---|---|---|
| Inputs: your app sets these | |||
| MinDate | Date | Today() | Days before this are disabled. Set to Blank() to allow past dates. |
| MaxDate | Date | Blank() | Days after this are disabled, e.g. DateAdd(Today(), 90, TimeUnit.Days). Blank means no limit. |
| BlockedDates | Table | Table({BlockedDate: Date(1900, 1, 1)}) | Days that can't be picked, in a column called BlockedDate. A range can't cross one. |
| DisableWeekends | Boolean | false | Saturdays and Sundays can't be a start or end day. A range can still span a weekend. |
| AllowRange | Boolean | true | Turn off for single-day picking. |
| MaxDays | Number | 0 | Longest range allowed, counting both ends. 0 means no limit. |
| AccentColor | Color | RGBA(0, 94, 184, 1) | Selection, today ring and buttons. Lighter shades are worked out from it. |
| Outputs: your app reads these | |||
| StartDate | Date | First selected day, or blank. | |
| EndDate | Date | Last selected day. Equals StartDate until an end is picked. | |
| DayCount | Number | Days in the selection, counting both ends. 0 when empty. | |
| IsComplete | Boolean | true once an end day has been picked. | |
| Behaviour | |||
| OnReset | Built in | Runs on Reset(RangeCalendar1) and empties the selection and month. | |
Using it in your app
Once the component is on a screen, everything happens through its properties. These examples assume the copy is called RangeCalendar1.
ASave a booking
Output properties update by themselves, so a Book button can enable itself as soon as a range is complete, and save the dates directly.
If one click should be enough for a one-day booking, use !IsBlank(RangeCalendar1.StartDate) instead of IsComplete.
// btnBook.DisplayMode If(RangeCalendar1.IsComplete, DisplayMode.Edit, DisplayMode.Disabled) // btnBook.OnSelect Patch( Bookings, Defaults(Bookings), { StartDate: RangeCalendar1.StartDate, EndDate: RangeCalendar1.EndDate } ); Reset(RangeCalendar1) // lblSummary.Text RangeCalendar1.DayCount & If(RangeCalendar1.DayCount = 1, " day", " days")
BBlock days that are already booked
Bookings are usually stored as start and end dates, but the calendar wants a list of single days. Expand each booking into its days once, when the screen opens or the asset changes, and pass the result to BlockedDates.
Use dates without times (SharePoint "Date only" columns, Today(), Date()), or the matching won't line up.
// e.g. galAssets.OnSelect or Screen.OnVisible Clear(colBlocked); ForAll( Filter(Bookings, AssetID = varAsset.ID, EndDate >= Today()) As b, Collect( colBlocked, ForAll( Sequence(DateDiff(b.StartDate, b.EndDate, TimeUnit.Days) + 1, 0) As i, { BlockedDate: DateAdd(b.StartDate, i.Value, TimeUnit.Days) } ) ) ) // RangeCalendar1.BlockedDates colBlocked
CSet the rules
Every rule is an input property on the copy you placed. They can be fixed values or formulas, for example a booking limit read from a Settings list.
RangeCalendar1.MaxDate = DateAdd(Today(), 90, TimeUnit.Days) RangeCalendar1.MaxDays = LookUp(Settings, Title = "MaxBookingDays").Value RangeCalendar1.DisableWeekends = true RangeCalendar1.AccentColor = ColorValue("#007f3b")
Two calendars, one screen
Each copy keeps its own selection, so "loan dates" and "return inspection" pickers can sit side by side.
One place to fix it
From a component library, a change to the calendar is offered to every app that uses it the next time they're edited.
Date-only maths
Everything uses Today() and Date(), never Now(), so nothing drifts a day when the clocks change.
No event property needed
Outputs recalculate on their own, so the app reacts without OnChange. Custom event properties are still experimental in Power Apps, so the component avoids them.
Download
Choose by what you want to do. The solution is the one to use in real apps.
Install the component
A solution containing the Range Calendar component library.
- Download the
.zip. Don't unzip it. - In make.powerapps.com, go to Solutions → Import solution and pick the file.
- Open your app, then Insert → Get more components and choose cmpRangeCalendar.
Open the demo app
A small canvas app with the calendar already on a screen, wired to a few labels and a Reset button.
- Download the
.msapp. - In make.powerapps.com, go to Apps → Import app → From file (.msapp).
- Play it, or open the component to read the formulas.
Read or rebuild the source
Plain-text YAML you can read on GitHub, paste into Studio, or use to build the component yourself. There's also a simpler paste-in version that uses app variables, for learning the technique.
View on GitHubImporting a solution needs an environment with Dataverse and permission to import solutions (Environment Maker or above). If you can't import solutions, use the demo app or build it from the YAML.
Show the component YAML
# Range Calendar component: the controls that go INSIDE the component
#
# This file is not the whole component. It holds the controls that live inside it.
# Build the component first (docs/BUILD.md), then:
# 1. create the component and name it cmpRangeCalendar (exactly that)
# 2. add the seven INPUT properties listed below
# 3. copy this whole file and paste it onto the empty component canvas
# 4. add the four OUTPUT properties and the OnReset formula listed below
#
# ---------------------------------------------------------------------------
# Component settings (select cmpRangeCalendar in the Tree view)
# Width 364
# Height 430
# OnReset Set(_start, Blank()); Set(_end, Blank()); Set(_month, Blank())
#
# INPUT properties Data type Default formula
# MinDate Date and time Today()
# MaxDate Date and time Blank()
# BlockedDates Table Table({BlockedDate: Date(1900, 1, 1)})
# DisableWeekends Boolean false
# AllowRange Boolean true
# MaxDays Number 0
# AccentColor Color RGBA(0, 94, 184, 1)
#
# OUTPUT properties Data type Formula
# StartDate Date and time _start
# EndDate Date and time If(IsBlank(_start), Blank(), Coalesce(_end, _start))
# DayCount Number If(IsBlank(_start), 0, DateDiff(_start, Coalesce(_end, _start), TimeUnit.Days) + 1)
# IsComplete Boolean !IsBlank(_end)
#
# Private state (Set() inside a component belongs to that one copy of it):
# _month first day of the month on show (blank = this month)
# _start first selected day
# _end last selected day (blank = start picked, end not yet)
#
# If Studio reports an unknown control version, change the @x.y.z suffix to the
# version your Studio offers.
# ---------------------------------------------------------------------------
- rcFrame:
Control: GroupContainer@1.3.0
Variant: ManualLayout
Properties:
X: =0
Y: =0
Width: =364
Height: =430
Fill: =RGBA(255, 255, 255, 1)
BorderColor: =RGBA(216, 221, 224, 1)
BorderThickness: =1
RadiusTopLeft: =8
RadiusTopRight: =8
RadiusBottomLeft: =8
RadiusBottomRight: =8
Children:
- rcPrev:
Control: Classic/Button@2.2.0
Properties:
Text: ="‹"
X: =8
Y: =8
Width: =40
Height: =40
Size: =20
Fill: =RGBA(0, 0, 0, 0)
Color: =cmpRangeCalendar.AccentColor
HoverFill: =ColorFade(cmpRangeCalendar.AccentColor, 0.88)
HoverColor: =cmpRangeCalendar.AccentColor
BorderThickness: =0
AccessibleLabel: ="Previous month"
OnSelect: |-
=Set(
_month,
DateAdd(Coalesce(_month, Date(Year(Today()), Month(Today()), 1)), -1, TimeUnit.Months)
)
- rcMonth:
Control: Label@2.5.1
Properties:
Text: =Text(Coalesce(_month, Date(Year(Today()), Month(Today()), 1)), "mmmm yyyy")
X: =56
Y: =8
Width: =252
Height: =40
Align: =Align.Center
FontWeight: =FontWeight.Semibold
Size: =15
Color: =RGBA(33, 43, 50, 1)
- rcNext:
Control: Classic/Button@2.2.0
Properties:
Text: ="›"
X: =316
Y: =8
Width: =40
Height: =40
Size: =20
Fill: =RGBA(0, 0, 0, 0)
Color: =cmpRangeCalendar.AccentColor
HoverFill: =ColorFade(cmpRangeCalendar.AccentColor, 0.88)
HoverColor: =cmpRangeCalendar.AccentColor
BorderThickness: =0
AccessibleLabel: ="Next month"
OnSelect: |-
=Set(
_month,
DateAdd(Coalesce(_month, Date(Year(Today()), Month(Today()), 1)), 1, TimeUnit.Months)
)
- rcWeekdays:
Control: Gallery@2.15.0
Variant: Vertical
Properties:
Items: =["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"]
X: =8
Y: =54
Width: =348
Height: =28
WrapCount: =7
TemplateSize: =28
TemplatePadding: =0
ShowScrollbar: =false
Children:
- rcWeekdayLabel:
Control: Label@2.5.1
Properties:
Text: =ThisItem.Value
Width: =Parent.TemplateWidth
Height: =Parent.TemplateHeight
Align: =Align.Center
Size: =10
FontWeight: =FontWeight.Semibold
Color: =RGBA(76, 99, 112, 1)
- rcDays:
Control: Gallery@2.15.0
Variant: Vertical
Properties:
Items: |-
=With(
{
first: Coalesce(_month, Date(Year(Today()), Month(Today()), 1)),
minD: cmpRangeCalendar.MinDate,
maxD: cmpRangeCalendar.MaxDate
},
With(
// How many days to step back so the grid starts on a Monday
{ offset: Weekday(first, StartOfWeek.Monday) - 1 },
// 6 weeks x 7 days = 42 cells covers every possible month
ForAll(
Sequence(42, 0) As n,
With(
{ d: DateAdd(first, n.Value - offset, TimeUnit.Days) },
{
CalDate: d,
InMonth: Month(d) = Month(first),
IsToday: d = Today(),
// Every "can't pick this day" rule lives here, once
IsDisabled:
(!IsBlank(minD) && d < minD)
|| (!IsBlank(maxD) && d > maxD)
|| (cmpRangeCalendar.DisableWeekends && Weekday(d, StartOfWeek.Monday) > 5)
|| d in cmpRangeCalendar.BlockedDates.BlockedDate
}
)
)
)
)
X: =8
Y: =84
Width: =348
Height: =288
WrapCount: =7
TemplateSize: =48
TemplatePadding: =1
ShowScrollbar: =false
Children:
- rcDay:
Control: Classic/Button@2.2.0
Properties:
Text: =Day(ThisItem.CalDate)
Width: =Parent.TemplateWidth
Height: =Parent.TemplateHeight
Size: =12
RadiusTopLeft: =6
RadiusTopRight: =6
RadiusBottomLeft: =6
RadiusBottomRight: =6
AccessibleLabel: =Text(ThisItem.CalDate, "dddd d mmmm yyyy")
DisplayMode: =If(ThisItem.IsDisabled, DisplayMode.Disabled, DisplayMode.Edit)
# Today gets a ring, whatever else is going on
BorderThickness: =If(ThisItem.IsToday, 2, 0)
BorderColor: =cmpRangeCalendar.AccentColor
Fill: |-
=With(
{ s: _start, e: Coalesce(_end, _start), d: ThisItem.CalDate },
If(
!IsBlank(s) && (d = s || d = e), cmpRangeCalendar.AccentColor, // start / end
!IsBlank(s) && d > s && d < e, ColorFade(cmpRangeCalendar.AccentColor, 0.88), // in between
RGBA(0, 0, 0, 0)
)
)
HoverFill: =ColorFade(cmpRangeCalendar.AccentColor, 0.75)
PressedFill: =ColorFade(cmpRangeCalendar.AccentColor, -0.25)
DisabledFill: =RGBA(0, 0, 0, 0)
Color: |-
=With(
{ s: _start, e: Coalesce(_end, _start), d: ThisItem.CalDate },
If(
!IsBlank(s) && (d = s || d = e), RGBA(255, 255, 255, 1),
!ThisItem.InMonth, RGBA(174, 183, 189, 1),
RGBA(33, 43, 50, 1)
)
)
HoverColor: =RGBA(33, 43, 50, 1)
DisabledColor: =RGBA(200, 206, 210, 1)
FontWeight: =If(ThisItem.IsToday, FontWeight.Bold, FontWeight.Normal)
# One rule: if this click can't finish a valid range, it starts a new one.
OnSelect: |-
=With(
{ d: ThisItem.CalDate },
If(
// Single-day mode: every click is a complete selection
!cmpRangeCalendar.AllowRange,
Set(_start, d); Set(_end, d),
// Start a new selection when...
IsBlank(_start) // nothing picked yet
|| !IsBlank(_end) // a range is already complete
|| d < _start // clicked before the start
|| (cmpRangeCalendar.MaxDays > 0 // range would be too long
&& DateDiff(_start, d, TimeUnit.Days) + 1 > cmpRangeCalendar.MaxDays)
|| CountRows( // range would cross a blocked day
Filter(cmpRangeCalendar.BlockedDates, BlockedDate > _start && BlockedDate < d)
) > 0,
Set(_start, d); Set(_end, Blank()),
// ...otherwise finish it (same day = one-day selection)
Set(_end, d)
)
)
- rcSummary:
Control: Label@2.5.1
Properties:
Text: |-
=If(
IsBlank(_start),
If(cmpRangeCalendar.AllowRange, "Select a start date", "Select a date"),
With(
{ e: Coalesce(_end, _start), n: DateDiff(_start, Coalesce(_end, _start), TimeUnit.Days) + 1 },
Text(_start, "ddd d mmm") &
If(e <> _start, " – " & Text(e, "ddd d mmm"), "") &
" · " & n & If(n = 1, " day", " days") &
If(IsBlank(_end), " (pick an end date)", "")
)
)
X: =12
Y: =380
Width: =268
Height: =40
Size: =11
Color: =RGBA(33, 43, 50, 1)
- rcClear:
Control: Classic/Button@2.2.0
Properties:
Text: ="Clear"
X: =288
Y: =384
Width: =64
Height: =32
Size: =11
Fill: =RGBA(0, 0, 0, 0)
Color: =cmpRangeCalendar.AccentColor
BorderColor: =cmpRangeCalendar.AccentColor
BorderThickness: =1
HoverFill: =ColorFade(cmpRangeCalendar.AccentColor, 0.88)
HoverColor: =cmpRangeCalendar.AccentColor
DisplayMode: =If(IsBlank(_start), DisplayMode.Disabled, DisplayMode.Edit)
OnSelect: =Set(_start, Blank()); Set(_end, Blank())