Forum Discussion

DevM01's avatar
DevM01
Icon for Helper I rankHelper I
5 years ago
Solved

Group by ID and sum a column based on a third column with Power Query.

I have data coming back that look like this: 

IdYearAmount
1201015
1201020
1201220
2201010
2201130
2201140
2201115
3201210
3201320
3201320

 

I want the end result to be like this: 


 

IdYearAmount
1201035
1201220
2201010
2201185
3201210
3201340

 

So for each Id, the year should be unique, and the amount should be summed for that year. Can I do it in Power BI query editor? Do I need to write code for that? 

  • That depends on what you want to show in each other column, since you have fewer rows, you are going to have to determine what to show when those rows are merged for those other Columns. For example, you could add additional aggregates for each column using the max aggregate.

6 Replies

  • artemus's avatar
    artemus
    Icon for Microsoft Employee rankMicrosoft Employee

    What you need to do is a group by:

    1. Select both the Id and Year column

    2. Choose Group By button.

    3. In the aggregations, choose Sum, and for the column choose Amount

    4. Name the column Amount

    • DevM01's avatar
      DevM01
      Icon for Helper I rankHelper I

      Thanks artemus .. how do I do that and keep all other columns I already chose in a previous step? When I do the grouping I lose all other columns I had already. For those other columns, they are shared between all, so I'd like keep them the same. 

  • artemus's avatar
    artemus
    Icon for Microsoft Employee rankMicrosoft Employee

    That depends on what you want to show in each other column, since you have fewer rows, you are going to have to determine what to show when those rows are merged for those other Columns. For example, you could add additional aggregates for each column using the max aggregate.

    • DevM01's avatar
      DevM01
      Icon for Helper I rankHelper I

      artemus am not sure if I should post this as a new question, but I want to filter the "years" as >= current year, how do we do that? 

      • artemus's avatar
        artemus
        Icon for Microsoft Employee rankMicrosoft Employee

        You can do oit like this:

         

    • vinod473's avatar
      vinod473
      New Member

      I have the similar issue, Please share if you find any solution to it