Power Query: Revision Time – Part 8
29 May 2024
Welcome to our Power Query blog. Today, I continue to create a refreshable revision timetable by randomising the subject slots I need for the timetable.
As my salespeople take a well-earned break, many students here in the UK are preparing for exams in the summer. To help my own offspring get organised, I volunteered to create a refreshable printable revision timetable. This is the result:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image1-1715274309.png/9fa4928e14c7422c8be9c83417b61922.jpg)
I needed a list of topics, and to begin with, I created extra entries for topics that required more timeslots:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image2-1715274356.png/2734faf8bb0c4e8f3c8cb772fe0d7ba3.jpg)
We agreed on half-hourly slots, and I created a grid so that my daughter could indicate the slots that she wouldn’t be able to revise.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image3-1715274377.png/bbef9530a5dd6e6f91072736ed0f9cc7.jpg)
When I first designed the solution, I included some Excel functions, but since this is a Power Query blog, I will ensure that I only use Power Query functions (apart from some formatting at the end!).
In Part 1, I converted my data into two [2] Tables: Subjects and Availability. I extracted Subjects to Power Query.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image4-1715274404.png/413d7ef9314ea3ca270945c0b5f5b027.jpg)
I created the Availability query by taking a copy of Subjects, and amending it:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image5-1715274421.png/c10f40de073ab43a9f508ece457af593.jpg)
In Part 2, and Part 3, I calculated the number of subjects and the number of slots on Availability.
This gave me both totals:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image6-1715274443.png/2cfbc9eb1a21686e35d2beb4789969e5.jpg)
In Part 4, I calculated how many times each subject will appear in a new query, remembering to round up to whole slots.
This told me the number of slots that each subject should have for my example is three [3]:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image7-1715274461.png/d917c7c59d5b66cf2219a2c9f1650088.jpg)
In Part 5, I created a table where each subject appeared three times (i.e. the number of times given by Subject_Slots).
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image8-1715274478.png/2a017b84b1e503112bcfaf1e5561eae2.jpg)
In Part 6, I randomised the order of the slots ready to add to the timetable:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image12-1715274671.png/a8ca9f195c1932f5ffa66e4f4ab0c151.jpg)
Last week, I took a reference copy of the query Availability which I called TimeTable and began to transform it so that it is ready to receive the slot data.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image13-1715277472.png/32061c33de06fa20fadf085e860e7280.jpg)
Before I can merge this query with Random_Subject, I need to number the slots available.
As is often the case with Power Query, there are a number of ways I can do this, but I have chosen to filter the data I need to number and then re-merge it with the rest of the query. To make it easier to keep the order of my data, I will add an index column from the ‘Add Column’ tab before I start:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image11-1715278031.png/ca6eaadacde6282c70912740032dff85.jpg)
Next, I filter on Value to remove the rows containing ‘x’:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image12-1715278056.png/95687163ba6aac5cc217d1556f04fc55.jpg)
I may now add another index starting from one [1], which I name Slot Number:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image13-1715278073.png/85269c48bbe8bb2b45a122a87e0291d1.jpg)
I could merge or append my data to include the other rows that I will need for the timetable. Before I start, I will rename the steps ‘Added Index’ and ‘Renamed Columns 1’ which I plan to join, to make it easier to follow the process:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image14-1715278091.png/b74a72dc11c22ce3538a7ba00ecbb09e.jpg)
I will look at each option: this week I append the data. In the Home tab, I choose ‘Append Queries’, and choose to append to the current query:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image15-1715278118.png/efc8fd16b01a334e3a90e9aac031aebb.jpg)
This generates the M code I need.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image16-1715278139.png/52f9231c9e6ce08bf84df47e132acd9c.jpg)
I change the code from:
= Table.Combine({#"Index Slots Only", #"Index Slots Only"})
to
= Table.Combine({#"Index Slots Only", #"Index Full Query"})
This gives me some duplicates:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image17-1715278202.png/397ea2f7e8ae1320663b671cc2ef24e3.jpg)
I will look at one way of dealing with these next week.
Come back next time for more ways to use Power Query!