Power Query

How to create a flexible calendar table with Power Query

Here is an example of how to generate calendar based on two input values – date of the first day and date of the last day.
It is a flexible approach you can generate a calendar for the year 2019 or only next 30 days.
M code that you can use in Blank Query is at the and of this post.

The first task is to create two parameters for the beginning and end of the calendar table. Create a Blank Query, go to the Home -> Manage Parameters -> New Parameter and make start_date and end_date like in the picture below.

Of course, you may have a different date format.

In Blank Query (by default Query1), you can generate a list of dates by using previously created parameters and duration calculation.
Just put this in the Formula Bar.
= List.Dates(start_date, Duration.Days(end_date-start_date)+1, #duration(1, 0, 0, 0))

You can also use a more flexible approach if you need to generate a list of fates using today’s date as a starting point.
= List.Dates(DateTime.Date(DateTime.LocalNow()), Duration.Days(end_date-DateTime.Date(DateTime.LocalNow()))+1, #duration(1, 0, 0, 0))

For new column calculations it is worth to mention:
– ISO weeknum from the previous post
– Text concatenation to add extra strings to the calculated value
="Q " & Number.ToText([Quarter])
– A combination of year and month
=Date.Year([Date])*100+Date.Month([Date])

You can also use Date.ToText function for flexibility because you can achieve many different results by using format code.
Month name
=Date.ToText([Date],"MMMM")
Week day name
Date.ToText([Date],"dddd")

For most of the calculations, I used Power Query date functions that you can see here.

You can also add some column with holiday marks by merging information from another table. If you don’t like to use an external source, then you can build that in another Blank Query.

After all your Workbook Queries pane should look like this.



0 comments on “How to create a flexible calendar table with Power Query

Leave a Reply

Your email address will not be published. Required fields are marked *