Power Query: Sort it Out
29 January 2020
Welcome to our Power Query blog. This week, I look at sorting issues.
I have a very simple query:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/165/image1.png/e774d10cbbb9450fc45efbe51abdf434.jpg)
I also have a simple goal: to sort my data by salesperson, and then show the latest visit date for each salesperson.
I begin by sorting by salesperson, which I can do using the arrow icon at the top of the Salesperson column.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/165/image2.png/f32e5a15e2cf9c3e4d2d058458ce054d.jpg)
I choose to sort in ascending order.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/165/image3.png/f1140ff857fc3b6f5f97a6a24f4a6fc7.jpg)
Next, I sort the visit date in descending order:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/165/image4.png/72aa864d2854c6fefb1083fba0ab5792.jpg)
I now have my data in order, so I can remove duplicate names by right-clicking on the Salesperson column.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/165/image5.png/36776d1da4d05b45bb5a5d09375f407c.jpg)
I choose to ‘Remove Duplicates’.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/165/image6.png/23912d3b1671861e02bebcd5183f1607.jpg)
I have one row for each Salesperson, but I don’t have the latest date for all the salespeople. The sort on date has not been preserved. In order to keep the order, I need an extra step before I remove duplicates: I need to add an index.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/165/image7.png/6f49c288a0d88a66b427eaf4ece923d6.jpg)
On the ‘Add Column’ tab, I can add an ‘Index Column’. In this case, it doesn’t matter where I start the index; I choose to start at zero (0).
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/165/image8.png/b9ee28d90e6b5bc92ea4aeafdad51628.jpg)
My data now has an index, so I try removing duplicate names again.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/165/image9.png/0485ccbc83bdeec1d741bad442a1ea5f.jpg)
This time I do have the latest date for each salesperson. When sorting on multiple columns, I can use an index column to preserve the order on my data whilst carrying out transformations on my data.
Come back next time for more ways to use Power Query!