Group by is a type of query which is also used in power bi, there is an editor in power bi which is known as query editor window which has various queries for accessing data and such query is Group by which is used to group rows based on a given certain value.
Group By Function in Power BI
Power BI GROUPBY Dax Function will group together data by collecting the information from two different tables. This works almost similar to the summarize function in Power BI but groupby doesn’t allow us to do any kind of implicit calculations by using calculate function instead it allows us to use the new DAX function “CURRENTGROUP()” inside the aggregation functions in the additional column it adds.
Below is the SYNTAX of the Power BI GROUP BY Dax function.
- <Table>: First we need to specify the table name from where we are grouping the data.
- [<GroupBy_columnName1>]: What is the relationship first column from any of the two tables?
- [<GroupBy_columnName2>]: What is the relationship second column from any of the two tables?
- <Name>: This will be what is the name for the new column we are going to create.
- <Expression>: By using which DAX function we need to aggregate the values.
I know it is not that easy to understand by reading the syntax explanations but in the example sections, you will learn clearly.
How to Use GroupBy Dax Function in Power BI?
To use the Power BI groupby function we need at least two tables and below are the two tables I am going to use it for this DAX function demonstration.
Between the first table (Sales Table) and the second table (Buyers Table), common column is “Category”. In the “Buyers” table we have what is the category for each buyer, so using these two tables we will group the data.
While grouping the data we need to identify what are the columns we need in the new group table, in this case, I need “Category, Buyer, Sale Value, and Cost Value”.
Copy and paste the data directly to Power BI or you can copy the data to excel file and then import to Power BI as Excel file reference. So you can download the excel workbook template from the below link which is used for this example.
I have directly uploaded the data to Power BI.
- Since we are grouping the data we need to store it as a new table, so in Power BI go to “Modelling” and click on “New Table”.
- This will create a new table and in the formula bar, we can see it has highlighted the same.
- First, give a name to this new table as “Group by Category & Buyer”.
- After the table name put equal sing and open Power BI GROUPBY DAX function.
- The first argument of power bi GROUPBY DAX function is Table i.e. out of available two tables from which table we need to group the data, in this example we need to group the data from “Sales” table, so supply the table name as “Sales”.
- The next argument is GroupBy_ColumnName1 i.e. based on which column we need to group the data. So from “Sales Table” we need to group by “Category-wise” so select the “Category” column from “Sales Table”.
- Next from the “Buyers” table, we need to mention the column name to be grouped. So from the “Buyers” table, we need to group by “Buyers-wise”.
- Ok, specifying column names is over, next we need to give a name to the aggregate column since we are aggregating the “Sale Value” column we will give the same name only.
- Next for this new “Sales Value” column, we need applying DAX function to return the aggregate value. Open SUMX function in the Expression 1 argument.
- For this expression, we need to supply special CURRENTGROUP() function.
Note: CURRENTGROUP() function can be used inside the Power BI GroupBy function only. This function access to the sub-table representing the current group in GROUPBY function.
- In the next argument of SUMX function open SUM function.
- Since we are adding “Sales Value” column from the “Sales” table enter the column name. After this close two brackets for SUMX & SUM functions and enter a comma.
- Name 2 is the argument of GROUPBY function i.e. what the name of the second aggregate column is, enter the name as “Cost Value” in double-quotes.
- For this also open SUMX function, inside this open CURRENTGROUP function and for Expression open SUM function and choose “Cost Value” column from “Sales” table.
- Now close three brackets for Power BI SUMX, SUM, and GROUPBY functions and hit enter key to close the DAX formula. Then you can see three tables here. Select the newly created table for grouping, in this, we can see DAX expression in the formula section.
As you can see above we can see the aggregated grouped table.
Note: I have done so much of formatting to this table, you can download the Power BI RANKX Template from below link and apply each formatting technique as applied.
Things to Remember
- CURRENTGROUP is the new function that can be used inside the GROUPBY function only.
- CURRENTGROUP takes the reference of grouping table.
- We cannot use the CALCULATE function in the Power BI GROUPBY DAX function.
This has been a guide to Power BI GroupBy. Here we discuss how to group together the data by collecting the information from two different tables using Power BI GROUPBY DAX function. You can learn more from the following articles –