Power Query: Month Mayhem – Part 2
10 July 2024
Welcome to our Power Query blog. Today, I continue looking at how to index my data to keep everything in order.
My salespeople are back from their break and I have more reports to construct. I have a report with a list of the clients they have been working with each month:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image1-1719599361.png/dfe6055a736ec11fb0b0a44b08f09a93.jpg)
I would like to display the amount details in the salesperson sections but aligned to the relevant month at the top of the page:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image2-1719599385.png/184d40422ae6c7aecb7d7b7b2c11abaa.jpg)
Last time, I extracted the data to a new workbook and began transforming the data, viz.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image3-1719599460.png/36cdebf17e57886833898fde94e43510.jpg)
I created two indices, Source Full Index indicates the position of the row in the report, and Section Index will be used to keep the sub-sections together. This time, I will recombine the data, and show how this will help me to identify the data in each section.
I choose to merge my query with itself, using the ‘Merge Queries’ option on the Home tab:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image4-1719599491.png/9c31e63f0859bf418fbf820abed22d96.jpg)
In the dialog, I select ‘Sheet1(Current)’ to merge with the same query.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image5-1719599534.png/ff5e3015b57889495788344080169f75.jpg)
I select the column Source Full Index on both instances of the query and choose a ‘Left Outer’ join:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image6-1719599574.png/cfedbd490fa979d7c8e0d13fad3122f6.jpg)
Having created the step, I have the following M code:
= Table.NestedJoin(#"Renamed Columns1", {"Source Full Index"}, #"Renamed Columns1", {"Source Full Index"}, "Renamed Columns1", JoinKind.LeftOuter)
I want to change the first table to be the step ‘Renamed Columns’ (i.e. when I renamed the first index) instead of ‘Renamed Columns1’:
= Table.NestedJoin(#"Renamed Columns", {"Source Full Index"}, #"Renamed Columns1", {"Source Full Index"}, "Renamed Columns1", JoinKind.LeftOuter)
This gives me the following result:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image7-1719599778.png/7ecb0450f2ae9d81c6474e62f97278df.jpg)
I click on the icon next to the heading in the column Renamed Columns1, and choose which data to expand:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image8-1719599798.png/80ff6eae26e3c2c0e579c129f30b9fc8.jpg)
I only need the column Section Index. I also notice that my data is no longer in ascending Source Full Index order:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image9-1719599868.png/03fc08804c0bd0ab15e37eb69ebae072.jpg)
I sort ascending on Source Full Index:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image10-1719599895.png/703f17d9d33b549526572c4ade14f9f1.jpg)
Now I can see that the report sub-heading sections may be grouped together by filling down Section Index. I can achieve this by right-clicking on the heading of Section Index and choosing Fill and then Down:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image11-1719599921.png/cd4f2df9b03ae49f007a64993131d2b9.jpg)
I may now remove the column Source Full Index:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image12-1719599952.png/9f8a0196bf10d010b17e2640305f051d.jpg)
From this point, I need to treat the rows with amounts differently from the rest of the data. I will be using this query as a building block. I rename it to Source Data to make its purpose clearer:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image13-1719599979.png/3e6b3ce57a5de932217ef12106412c9c.jpg)
Next time, I will take a reference copy of Source Data and continue transforming the data.
Come back next time for more ways to use Power Query!