Power Query: Top Two
13 May 2020
Welcome to our Power Query blog. This week, I look at splitting my data into the top two (for example) and the rest.
I have some data from my imaginary salespeople that I used in Power Query: Group Functions.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image1.png/e774d10cbbb9450fc45efbe51abdf434.jpg)
This time, I want to give the commission total for 2019 to my two top salespeople, and then show an average for everyone else.
I start by extracting my data into Power Query, by using ‘From Table’ in the ‘Get & Transform’ section of the Data tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image2.png/f32e5a15e2cf9c3e4d2d058458ce054d.jpg)
I need to find the total commission for each salesperson, so I use the grouping functionality on the ‘Transform’ tab. I group by Salesperson and sum the Commission.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image3.png/f1140ff857fc3b6f5f97a6a24f4a6fc7.jpg)
I can now apply a descending sort on Commission to get my top two salespeople.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image4.png/72aa864d2854c6fefb1083fba0ab5792.jpg)
To get my top two salespeople, I can just take the top two rows.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image5.png/36776d1da4d05b45bb5a5d09375f407c.jpg)
I choose to use the ‘Keep Rows’ functionality on the Home tab. This is more flexible than removing the bottom rows, as I don’t have to specify how many rows to remove.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image6.png/23912d3b1671861e02bebcd5183f1607.jpg)
I now have my top two, and I am going to convert my salespeople column to a list by using the ‘Convert to List’ functionality on the Transform tab. I will use this later to reassemble my data.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image7.png/6f49c288a0d88a66b427eaf4ece923d6.jpg)
I create my list:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image8.png/b9ee28d90e6b5bc92ea4aeafdad51628.jpg)
I rename the step to create my list TopTwo. Now, I need to deal with the others. I create a new step which refers to the data before I removed the bottom columns.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image9.png/0485ccbc83bdeec1d741bad442a1ea5f.jpg)
The M code I have used is:
= #"Sorted Rows"
which accesses the data before I removed the bottom rows. This time, I need to remove the top rows using the remove rows functionality on the Home tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image10.png/daf8c4f0259ce428269c0d3d4badd32b.jpg)
I remove the top two rows. To get the average to appear in each column, I will use grouping, but I need to group using a constant so that I can sum the commissions. To do this, I add a custom column from the ‘Add Column’ tab, which always has the same value. The value I am going to use is “Others”, viz.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image11.png/22c6daeb82d7d69ac88f878227e04b28.jpg)
I can now group my data from the Transform tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image12.png/a1537847463e660a31158c8032525438.jpg)
I group by Custom and create a new Commission 2019 column (yes, I know we are in 2020!) which will contain the average.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image13.png/917da985be13220165c8d2823e95344f.jpg)
I have my data for the others, so I rename the step to Others and rename Custom to Salesperson. I am now ready to reassemble my data.
As I did earlier, I create a step to get back to my full data before I removed any rows. This will allow me to keep any columns that do not directly pertain to the calculation, and makes the method more flexible.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image14.png/8c3be7af9f73d031acae69ed85a2e148.jpg)
I need to select those rows which are associated with my top two.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image15.png/e63c0a4c21afc9afb438aacc09a59317.jpg)
The M code I have used is:
= Table.SelectRows(Custom2, each (List.Contains(TopTwo,[Salesperson])=true ))
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image16.png/d082e3477129350b8a2a589156028e63.jpg)
This gives me the data for my top two. Now I need to add my ‘others’. I do this by merging my table to itself, and then changing the M code generated.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image17.png/a468c9c28195e8f18fdedfa59a0cf042.jpg)
I check the M code generated.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image18.png/61935f470d8b9f21b7b7683ffca40895.jpg)
I have the M code
= Table.Combine({Custom3, Custom3})
I change this to
= Table.Combine({Custom3, Others})
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/180/image19.png/06909e3fbfc8faf87de7555c0b99e9d3.jpg)
I now have my data in the form I wanted, with my top two salespeople and the others combined into one average commission.
Come back next time for more ways to use Power Query!