Search code examples
exceldaxpowerquerym

How to extract max amount of column for each group in powerquery?


I am working on some data in powerquery in Excel. I need to make a query that returns unique values of the original table. The original table has a structure like below.

Year    month    name   qty
2019    Jan        n    100
2019    Jan        n    200
2019    Jan        m    300
2019    Jan        m    400
2019    feb        n    1000
2019    feb        n    20
2019    feb        m    300
2019    feb        m    500
2020    Jan        n    100
2020    Jan        n    200
2020    Jan        m    300
2020    Jan        m    400
2020    feb        n    1000
2020    feb        n    20
2020    feb        m    300
2020    feb        m    500

I need to extract an output like below

year    month   product MaxQty
2019    feb        m    500
2019    feb        n    1000
2019    Jan        m    400
2019    Jan        n    200
2020    feb        m    500
2020    feb        n    1000
2020    Jan        m    400
2020    Jan        n    200

In Sql, I can extract this output with below line of code.

SELECT year,month,name,max(qty) as MaxQty
FROM Table1 group by year,month,name;

How can I do it in power query of excel?

I used the following command:

=List.Distinct(Table1[Year]) 

but it worked on one column and I could not expand it to multiple columns to extract the max price of each product in year and month.

I need to do it in Power query. I would appreciate if you could advice.


Solution

  • click select the year, month and name columns

    Right click group by

    take the maximum of the qty column

    enter image description here