Formulas - how to return all days between two dates

There are often scenarios where we need to display, or to build a collection that contains a range of sequential dates.

As an example, let's take a data entry screen in an app that manages multi-day events or conferences. On this screen, we may want to display a drop-down box with a choice of values between a start and end date.

Alternatively, let's imagine an app that bulk inserts multiple records. To implement this type of feature, we may want to base the functionality on a collection with records that correspond to each day between the start and end date values.

How to show consecutive days in a control (eg, a drop down)

The formula beneath shows the basic syntax that we would use. This formula produces an output table that contains all dates between the "1st January 2021" and "6th January 2021" inclusively.

With({startDate:DateValue("2021-01-01"),
sequenceDays: Sequence(DateDiff(DateValue("2021-01-01"),
DateValue("2021-01-06")) + 1)
},
AddColumns(sequenceDays,
"Date",
DateAdd(startDate, Value - 1, Days)
)
)

To apply this formula, we would replace the start date and end date values inside the With block, with our target start and end dates..

This formula relies on a single column table called sequenceDays. This table contains a consecutive sequence of numbers between the start and end dates. In this example, the content of the sequenceDays collection would contain the following data:

[1,2,3,4,5,6]

We use a combination of the Sequence and DateDiff functions to generate the sequenceDays collection. In this example, DateDiff returns the number of days between the start and end date which is 5. To produce an output that is inclusive of both the start and end dates, we need to add 1 to this value.

We add a column called "Date", which we calculate by adding the sequence value to the start date. Because the sequence begins with 1 and we want to add zero days for the first output record, we subtract the 'value' in the sequence by 1.

The screenshot beneath illustrates this technique, as applied to the Items property of a drop down control.

 

How to populate a collection of consecutive days with additional fields

As a logical extension of the above, here's the formula to populate a collection with the same days, but with extra columns to display the day numbers.

With({startDate:DateValue("2021-01-01"),
sequenceDays: Sequence(DateDiff(DateValue("2021-01-01"),
DateValue("2021-01-06")) + 1)
},
ClearCollect(colDates,
AddColumns(sequenceDays,
"Date",
DateAdd(startDate, Value - 1, Days),
"Day Num",
"Day " & (Value),
"Day Name",
Text(DateAdd(startDate, Value - 1, Days),
"dddd"
)
)
)
)
We implement this by defining the fields the columns to add in the call to AddColumns. Notice how we apply the a technique from an earlier post to display the day name (eg, Monday, Tuesday, etc)

The screenshot beneath illustrates the appearance of a data table, when we apply the Items property to the collection.

Conclusion

A common requirement is to build a collection of sequential days between the start and end date value. This post highlighted how to accomplish this with the help of the Sequence, DateDiff, and AddColumn functions.

Related posts

FormuIas - Is it possible to call a user-defined function recursively in Power Apps?
January 31, 2024
Formulas - A beginners guide on how to create and call user-defined functions (UDFs)
January 28, 2024
Formula - How to add a button that converts degrees Centigrade to Fahrenheit and vice versa
May 08, 2023
Formula - How to convert a single delimited string to rows and columns
April 05, 2023
Data - How to group data in a gallery and calculate sums
January 20, 2023
Formula - How to calculate compound interest
November 24, 2022
Utilities - The best way to peform OCR on images of Power Apps Formulas
October 11, 2022
Example - How to use a drop down control to convert currencies
September 27, 2022
Formula - How to parse JSON in Power Apps- 4 examples
September 15, 2022
Data - How to get a row by ordinal number
April 28, 2022
Formula - What to do when the If statement doesn't work?
December 17, 2021
Formula - Boolean And / Or operators - What is the order of precedence?
December 16, 2021
Controls - How to set the data source of a Combo Box to a comma separated string
November 16, 2021
Numbers - 10 examples of how to round numbers
August 18, 2021
Formula - Difference between round, square, and curly brackets
July 20, 2021
Top 3 highlights of upcoming enhancements to the Power Apps language (Power FX)
May 26, 2021
Email - Sending email attachments with the Office 365 Outlook connector
March 30, 2021
Formula - What to try when numbers don't format correctly
March 24, 2021
Controls - How to convert HTML to Text
March 23, 2021
Formula - How to create comma separated (CSV) list of items
February 25, 2021
Formula - How to use the IF and Switch functions - 3 common examples
February 12, 2021
Location - Finding the closest location and and sorting records by distance, based on the current location of the user
January 24, 2021
Formulas - How to cope with weekends and public holidays in date calculations
January 21, 2021