Articles

GroupBy in PowerQuery

Pinterest LinkedIn Tumblr

Introduction:  

Generally, grouping means forming several things into a cluster or being brought together, the same way in power query we have a functionality called GroupBy which allows you to group by aggregation like sum, count, min, max, etc.
At the end of this article, there is an interesting tip which will be useful to you. Let’s begin

Why GroupBy:

Let’s say you have a list of sales data with product name, region, and sale value, and from this you want to know what is a top-selling product, In Excel we use to take unique values of product and perform SUMIFS function to get the result which takes time, but in power query using GroupBy functionality you can complete in few clicks select GroupBy then group data by product and add(SUM) column sales value you will get the answer, the same way if you want to know the region-wise top-selling product you can group data by product, region and add(SUM) column sales value.

Where to find GroupBy button:

Group by button can be found in three places:

  • On the home tab  >>  Transform group  >>  GroupBy.
GroupBy in PowerQuery 1
  • Go to transform tab  >>  GroupBy.
Group by 2
  • Go to the column you want to group then right click using mouse/touchpad select group by.
GroupBy 3

List of Operations:

  1. Sum – Displays the total of the grouped rows.
  2. Average – Displays the average value of the grouped rows.
  3. Median – Displays the Median value i.e mid value of the grouped rows.
  4. Min – Displays the least value from the grouped rows.
  5. Max – Displays the highest value from the grouped rows.
  6. Count Rows – Displays the number of rows in a grouping.
  7. Count Distinct Rows Displays the distinct number of rows in a grouping.
  8. All Rows – Creates a table for each grouping.
  • Note: Not only these operations you can also perform many PowerQueryMFunctions after creating a table i.e. All Rows

Example for basic GroupBy

Here is a simple example to know how to use group by function. The following Table contains BSE data of 2 years which contains the Date, Name of the company, industry belonging to, Quantity, state, and classification.

GroupBy 4

From the above data let us find the total quantity sold by each industry, Before that let us see how to enter query editor(Power Query).
1. Convert the data to the table using CTRL+T or go to the Insert tab and select the table.

2. Go to Data tab and in Get & Transform Data group select from Table/Range.

GroupBy 5

3. Now you will be inside the query editor, before starting lets have a look at the basic GroupBy Dialogue box.

GroupBy 6

Step-1: Select the column you want to group then right-click on it and select GroupBy. Here we need to group Industry Classification so select right-click and select GroupBy.

GroupBy 7

Step-2: A GroupBy dialogue box will be displayed under that select Basic as we want to group only one column and then select the column you want to group. Here it is Industry Classification.

GroupBy 9


Step-3: Under New Column Name enter the column name you want to add.

GroupBy in PowerQuery 2


Step-4: Under Operation select the type of operation you want to perform, here we are selecting Sum to get the total quantity.

GroupBy in PowerQuery 3

Step-5: Under Column select the column for which you want to apply specified operation. and click on ok

GroupBy in PowerQuery 4

Step-6: After Clicking OK you can see the result i.e total quantity sold by each industry.

GroupBy 12

Example for Advance GroupBy

From the same example let us bring industry-wise and month wise total quantity. before starting let us see the dialogue box of advance Group By Option.

GroupBy 13

Step-1: A GroupBy dialogue box will be displayed, here we are going to group more than one column so select the advanced option.

GroupBy 14

Step-2: Next select the first column you want to group in this case it is Industry Classification

GroupBy 15

Step-3: As we are going to add another column for grouping click on add grouping and select the column in this case it is Date.

GroupBy 16

Step-4: Under New Column Name enter the column name you want to add.

GroupBy in PowerQuery 5

Step-5: Under Operation select the type of operation you want to perform, here we are selecting Sum to get the total quantity.

GroupBy in PowerQuery 6

Step-6: Under Column select the column for which you want to apply specified operation. and click on ok

GroupBy in PowerQuery 7

Step-7: After Clicking OK you can see the result i.e total quantity sold by each industry.

GroupBy 20

#Tip: If you look at the above picture grouped date is row-wise which is not good to present as a report, in the power query, there is a special functionality i.e. PIVOT COLUMN which turns it column-wise. Follow the steps to get it.
Step-1: Select the column you want to apply pivot column in this case it is Date.

GroupBy 21

Step-2: Go to Transform Tab and in any column group select pivot column.

GroupBy 22

Step-3: A Pivot Column dialogue box will be displayed under the values column select the column you want to pivot in this case it is the date.

GroupBy 23

Step-4: Click on advanced to enable more options.

GroupBy 24

Step-5: Under Aggregate value function we dont want to aggregate anything so select dont aggregate and click on ok.

GroupBy 25

Step-6: After Clicking ok you will get a proper report as shown in the below picture.

GroupBy 26

Related Posts

Write A Comment