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 summarized function in Power BI, but groupby doesn’t allow us to do any kind of implicit calculations by using the 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), the 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 an excel file and then import it to Power BI as an 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.
Follow the steps to use Group By Dax function in Power BI.
- 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 u0026 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 the “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 to apply the DAX function to return the aggregate value. Open SUMX function in the Expression 1 argument.
- For this expression, we need to supply a 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 functionOpen SUM FunctionThe SUM function in excel adds the numerical values in a range of cells. It is categorized under the math and trigonometry function entered by typing “=SUM” followed by the values to be summed. The values can be numbers, cell references or ranges..
- Since we are adding the “Sales Value” column from the “Sales” table, enter the column name. After this, close two brackets for SUMX u0026 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 the “Cost Value” column from the “Sales” table.
- Now close three brackets for Power BI SUMXPower BI SUMXSUMX is a function in power BI which returns the sum of expression from a table. It is an inbuilt mathematical function. The syntax used for this function is SUMX(
)., SUM, and GROUPBY functions and hit the 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 the below link and apply each formatting technique as applied.You can download this Power BI GroupBy Template here – Power BI GroupBy Template
Things to Remember
- CURRENTGROUP is the new function that can be used inside the GROUPBY function only.
- CURRENTGROUP takes the reference of the grouping table.
- We cannot use the CALCULATE function in the Power BICALCULATE Function In The Power BIPower BI Calculate serves as a base function to apply other DAX functions in various scenarios. Assume you wish to apply a filter to get the average sales for a specific location; you may use the CALCULATE function to apply the filter and arrive at calculations. 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 –