Power Query: Rental Wrangles
28 July 2021
Welcome to our Power Query blog. This week, I look at how to expand data to create a useful table.
I have some data on tent rentals:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image1.png/e774d10cbbb9450fc45efbe51abdf434.jpg)
It’s a fairly basic Table, which just records the date when rentals for each type of tent change.
I also have a Table of one-day events, where tent rental is required:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image2.png/f32e5a15e2cf9c3e4d2d058458ce054d.jpg)
I want to know the rental for each event, but currently there is only a clear link for the first event, as it occurs on 1 January 2021 and there is a rental rate entry for this. There are a couple of ways I can approach this. This week, I am going to expand the tent rental table so that there is an entry for every date.
I start by extracting the tent rental data to Power Query, using ‘From Table/Range’ on the ‘Get & Transform Data’ section of the Data tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image3.png/f1140ff857fc3b6f5f97a6a24f4a6fc7.jpg)
I start by ordering my data by Tent Type, using the dropdown next to the column heading.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image4.png/72aa864d2854c6fefb1083fba0ab5792.jpg)
I need to get the Tent Type into the headings so that I can cross reference Tent Type with all Dates in the appropriate period. I can do this by pivoting the Tent Type column. This option is available from the Transform tab:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image5.png/36776d1da4d05b45bb5a5d09375f407c.jpg)
I now have column headings for each Tent Type.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image6.png/23912d3b1671861e02bebcd5183f1607.jpg)
In order to create a row for each Date, I am going to create a new Blank Query from the ‘New Source’ dropdown in the ‘New Query’ section of the Home tab.
I start my new query by using functionality that will create a list. The M code I am going to use is in the form:
={fromdate..todate}
However, I can not currently use dates as the beginning and ending points; I have to use numbers. Power Query will then generate every number in the interval. Thus, I convert my dates to numbers.
= {Number.From(#date(2021,1,1))..Number.From(#date(2021,6,1))}
Here, I am creating a date by providing the year, month and day number, and then using Number.From() to convert the date to a number. This will give me a number for every day from 1 January 2021 to 1 June 2021.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image7.png/6f49c288a0d88a66b427eaf4ece923d6.jpg)
I can then convert the list to a table using the option on the ‘List Tools’ tab. I also change the datatype to DateTime (so that it matches the Date column on my original query) and rename it Date.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image8.png/b9ee28d90e6b5bc92ea4aeafdad51628.jpg)
My Calendar query is ready to use.
Back in my original query (now called Rental Calendar), I am going to merge with the Calendar query. I can do this from the Home tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image9.png/0485ccbc83bdeec1d741bad442a1ea5f.jpg)
I use a ‘Right Outer’ join because I want to include every Date from Calendar.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image10.png/daf8c4f0259ce428269c0d3d4badd32b.jpg)
I can now expand the Calendar column to just give me the Date.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image11.png/22c6daeb82d7d69ac88f878227e04b28.jpg)
I remove the original Date column. I need a rental value for every date, so I can fill down on “Large”, “Medium” and “Small”.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image12.png/a1537847463e660a31158c8032525438.jpg)
Finally, I need to get the tent types back into a Tent Type column. I can do this by selecting Date and choosing to ‘Unpivot other columns’ from the ‘Unpivot Columns’ dropdown on the Transform tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image13.png/917da985be13220165c8d2823e95344f.jpg)
I now have a value for each date, telling me what the rental is for each type of tent. I can rename the Attribute and Value columns to Tent Type and Rental Rate.
Next, I extract the data for the events:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image14.png/8c3be7af9f73d031acae69ed85a2e148.jpg)
I merge with the query Rental Calendar. This time, I use a ‘Left Outer’ join, as I only want rows for each Event Date and Tent Type.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image15.png/e63c0a4c21afc9afb438aacc09a59317.jpg)
This gives me a new column containing the data from Rental Calendar.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image16.png/d082e3477129350b8a2a589156028e63.jpg)
I expand Rental Calendar to get the Rental Rate.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/243/image17.png/a468c9c28195e8f18fdedfa59a0cf042.jpg)
Next time, I’ll look at a different way to get to this result by creating a function.
Come back next time for more ways to use Power Query!