Expand date ranges into a full list of dates
If you have a range of dates, with start and end dates for specific periods, you can easily expand to date ranges for a full list of dates. So instead of having a date range of 17-Sep to 16-Jan, you can get each day in the range; 17-Sep, 18-Sep, … , 15-Jan, 16-Jan.
No missing dates, so long as the date ranges are continuous.
This makes it possible to join dates in other tables to the expanded date range table.
Starting with date ranges like this that represent stages.
Start Date | End Date | Stage |
---|---|---|
17/09/2021 | 16/01/2022 | 1st Stage |
17/01/2022 | 16/06/2022 | 2nd Stage |
17/06/2022 | 16/09/2022 | 3rd Stage |
17/09/2022 | 16/01/2023 | 1st Stage |
17/01/2023 | 16/06/2023 | 2nd Stage |
17/06/2023 | 16/09/2023 | 3rd Stage |
17/09/2023 | 16/01/2024 | 1st Stage |
Select the table of dates and navigate the Data menu tab and click on From Table/Range.
The data will be imported into Power Query.
Change the format of the date columns to Date.
Navigate to the Add Column menu tab and click on the Custom Column icon. Add a formula to create a list of dates based on the Start Date and End Date of each row.
List.Dates([Start Date],Duration.Days( [End Date] - [Start Date] ) + 1, #duration( 1, 0, 0, 0 ))
Breaking down the formula, List.Dates(start as date, count as number, step as duration) as list
, the start is set as the Start Date.
The count uses Duration.Days
to calculate the number of days between the Start Date and the End Date and adds one day.
The #duration is set to 1 day, 0 hours, 0 minutes, 0 seconds.
This creates a list of dates from the Start Date to the End Date.
A list will be added to each row.
Click on the Expand icon in the column heading.
The date ranges will be expanded into a full list of dates.
Remove the columns that you no longer need.
Navigate to the Home menu tab and click on the Close & Load icon.
The data will be loaded back into Excel.
The data can be used to join tables.
For example it can be merged with other queries to bring in other data, in this case, the Stage.