Power Query: Refined Rental Wrangles
4 August 2021
Welcome to our Power Query blog. This week, I look at creating a function to get data from a table.
Last week, I was working with some data on tent rentals:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image1.png/e774d10cbbb9450fc45efbe51abdf434.jpg)
I also had a Table of one day events, where I wanted to include the correct rental amount.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image2.png/f32e5a15e2cf9c3e4d2d058458ce054d.jpg)
Last time, I expanded the tent rental data so that I had an entry for each date. This time, I am going to create a function to extract the correct rental amount. I will extract my rental data to Power Query, by using ‘From Table/Range’ in the ‘Get & Transform Data’ section of the Data tab. I will call it Rental Step, as it is the table that I will be creating a function from.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image3.png/f1140ff857fc3b6f5f97a6a24f4a6fc7.jpg)
There are different ways to create a function. I am going to follow a method which involves creating as little manual M code as possible! I am going to create a parameter to filter Date and a parameter to filter Tent Type before I continue. I can do this from ‘Manage Parameters’ on the Home tab:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image4.png/72aa864d2854c6fefb1083fba0ab5792.jpg)
My date parameter is called P_Rental_Date:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image5.png/36776d1da4d05b45bb5a5d09375f407c.jpg)
I have deliberately chosen to default to a date which I know is later than my rental data. This will make it obvious that one of my later steps is required.
The next parameter is P_Tent_Type:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image6.png/23912d3b1671861e02bebcd5183f1607.jpg)
I choose a Tent Type value as the default.
Having created my parameters, I go back to Rental Step and apply a filter to Date.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image7.png/6f49c288a0d88a66b427eaf4ece923d6.jpg)
Note that this screen can be accessed by choosing any filter, and then ‘is before or equal to’ can be selected from the dropdown. I choose to filter dates before or equal to P_Rental_Date, which will currently select them all because of the late date I chose for the default.
I then apply a filter to Tent Type.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image8.png/b9ee28d90e6b5bc92ea4aeafdad51628.jpg)
As this is currently set to “Large”, I will get all rows for “Large” with any date.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image9.png/0485ccbc83bdeec1d741bad442a1ea5f.jpg)
This is why I chose to default P_Rental_Date to a date later than my data. I can see that, to get the latest rental information for “Large” at the top, I need to sort the dates in descending order.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image10.png/daf8c4f0259ce428269c0d3d4badd32b.jpg)
Now I need to calculate the Rental Rate, so I only want that column.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image11.png/22c6daeb82d7d69ac88f878227e04b28.jpg)
Finally, I drill down into the latest Rental Rate so that I only have the latest value. I can do this by right-clicking on the cell and selecting ‘Drill Down’.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image12.png/a1537847463e660a31158c8032525438.jpg)
Finally, I can convert my query to a function. As I am going for the route of least M code, I do this by right-clicking on my finished query.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image13.png/917da985be13220165c8d2823e95344f.jpg)
I am then prompted for the name of my function:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image14.png/8c3be7af9f73d031acae69ed85a2e148.jpg)
I create fx_Rental:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image15.png/e63c0a4c21afc9afb438aacc09a59317.jpg)
All the M code has been created for me, and even better, I can change my function by amending the steps in Rental Step rather than using the Advance Editor here. In fact, if I try to call the Advanced Editor, I am warned not to break the link:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image16.png/d082e3477129350b8a2a589156028e63.jpg)
Now I can go back to the events data and add a new column from the ‘Add Column’ tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image17.png/a468c9c28195e8f18fdedfa59a0cf042.jpg)
By choosing ‘Invoke Custom Function’ I can call my new function and pass Event Date and Tent Type as the parameters.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2021/power-query/244/image18.png/61935f470d8b9f21b7b7683ffca40895.jpg)
I have the same results as last time.
Come back next time for more ways to use Power Query!