Power Query: Time for a List
20 November 2019
There are three M functions in Power Query that will allow me to create a time dimension:
- List.Dates(start as date, count as number, step as duration) as list
This returns a list of date values of size count, starting at start. The given increment, step, is a duration value that is added to every value - List.Times(start as time, count as number, step as duration) as list
This returns a list of time values of size count, starting at start. The given increment, step, is a duration value that is added to every value - List.DateTimes(start as datetime, count as number, step as duration) as list
This returns a list of datetime values of size count, starting at start. The given increment, step, is a duration value that is added to every value.
I start with the List.Dates() function. I create a new blank query as follows:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/155/image1.png/e774d10cbbb9450fc45efbe51abdf434.jpg)
I create a new step.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/155/image2.png/f32e5a15e2cf9c3e4d2d058458ce054d.jpg)
The M code I am using is
= List.Dates(DateTime.Date(DateTime.LocalNow()), 7, #duration(-1, 0, 0, 0))
which takes the current date and subtracts a day a total of seven (7) times.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/155/image3.png/f1140ff857fc3b6f5f97a6a24f4a6fc7.jpg)
This gives me the last seven days. I can convert my list to a table using ‘List Tools’, and I rename my new column Date.
To show how List.Times() works, I add a new ‘Custom Column’ from the ‘Add Column’ tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/155/image4.png/72aa864d2854c6fefb1083fba0ab5792.jpg)
The M code I have used is:
= List.Times(#time(0, 0, 0), 96, #duration(0, 0, 15, 0))
There are 96 quarter-hours in 24 hours, so this will divide each day into 15-minute segments.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/155/image5.png/36776d1da4d05b45bb5a5d09375f407c.jpg)
I need to expand my list into rows.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/155/image6.png/23912d3b1671861e02bebcd5183f1607.jpg)
I choose to ‘Expand to New Rows’:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/155/image7.png/6f49c288a0d88a66b427eaf4ece923d6.jpg)
I could combine these columns, if I wish, by creating a new custom column.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/155/image8.png/b9ee28d90e6b5bc92ea4aeafdad51628.jpg)
This will give me a new DateTime column.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/155/image9.png/0485ccbc83bdeec1d741bad442a1ea5f.jpg)
List.DateTimes() allows me to create datetime entries at intervals starting from a particular point in time, for example, now. I can show this in a new blank query:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/155/image10.png/daf8c4f0259ce428269c0d3d4badd32b.jpg)
The M code I have used is:
= List.DateTimes(DateTime.LocalNow(),672, #duration(0, 0, 15, 0))
which gives me 96 x 7 intervals of 15 minutes (i.e. the next seven days).
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/155/image11.png/22c6daeb82d7d69ac88f878227e04b28.jpg)
I can convert and rename as before.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/155/image12.png/a1537847463e660a31158c8032525438.jpg)
Come back next time for more ways to use Power Query!