groupby in power bi

GroupBY in Power BI vs CURRENTGROUP

GroupBy in Power BI is used to Group columns in a table based on a certain field. It is similar to the Summarize function in Power BI, but groupby cannot do an implicit Calculate to an extension column (Extension columns are columns that are added to an existing table). But it can use a new function CURRENTGROUP(), to do the aggregations for the extension columns. Groupby function returns a table with a selected set of columns. The table which is created by the group by function can be reused which makes it highly efficient.

GROUPBY (<table>, [<groupBy_columnName1>,<groupBy_columnName1>], [<name>, <expression>])

  1. Table – The table where the groupby functions needs to be applied.
  2. groupBy_columnName – The existing columname by which the data is to be grouped. This shouldn’t be an expression
  3. name – the name given to the new column that is being added to the list of GroupBy columns. This should be placed with double quotes.
  4. Expression – A DAX expression that returns a single scalar value, where the expression is to be evaluated for each set of GroupBy values. The expression used in DAX can be of any ‘X’ aggregation functions like SUMX,AVERAGEX,MAXX,MINX,COUNTX,PRODUCTX etc.
  5. The value has to be returned from the DAX expression, which will have the selected columns for the groupBy_columnName arguments and the grouped by column name parameters.

GroupBy function evaluates as mentioned below:

  1. Takes the specified table name and all the related table as first argument
  2. Groups the data based on all the grouping columns specified in DAX expression, that should exist in the table specified as the first argument
  3. Each group will be a single result in the expression ,but will be multiple rows in the underlying table.

Example : The below ProductSales, has four columns for Bike when grouping based on Product and aggregating based on sales , the bike will have only one entry in the resultant table.

ProductCountrySalesQuantity
BikeNewYork1005
BikeNewYork15010
BikeLondon1004
CarLondon3003
CarNewYork3507
TruckNewYork4502
TruckLondon4501
ProductTotal Sales
Bike350
Car600
Truck900

GROUPBY (

ProductSales,

ProductSales [Product],

“Total Sales”, SUMX( CURRENTGROUP(), ProductSales[sales])

)

The function groups a selected set of rows into a set of summary rows by the values of one or more groupBy columnName. One row is returned for each group as shown above.

  • For each group an extension column will be added. An implicit calculate won’t be performed inside the group by function and hence filter cannot be added within it.

Limitations:

CALCULATE functions are not supported within DAX, hence groupby cannot be used within measures and in directquery when used calculated columns or Row Level Security (RLS) rules.

CURRENTGROUP() function can be used only within the groupby function which will perform a table scan at the top level in expressions.

Let’s see an example below

Say, We have a table called Productsales as below, we are grouping the table based on the products and naming the column as Sales ,then the MAX sales in each category is calculated within the GroupBy function. Then we are calculating the total of maximum sales in each category which is being returned as the final result.

ProductSales AmountQuantitySubCategory
Televison and Video919410Sony LED TV
Audio32245Boat Headphone
Toys and Video Games763814PS3
Home Appliances37466Oven
Entertainment422111Netflix
Cellphones47817Apple 10
Cameras83499Sony
Televison and Video71969LG Video System
Audio156413Airpods
Toys and Video Games54565Soft Toy
Home Appliances240212Refrigerator
Entertainment47903Amazon Prime
Cellphones16737Apple X6
Cameras46126Canon
Audio44968Bluetooth Speaker
SalesAmount = var a =GROUPBY(ProductSales,ProductSales[Product],"Sales",MAXX(CURRENTGROUP(),ProductSales[Sales Amount]))
return MAXX(a,ProductSales[Sales Amount]*ProductSales[Quantity])
GroupBy in Power BI

Summary

GROUPBY function can be used instead of ADDCOLUMNS/SUMMARIZE whenever we want to access the rows of a group in an iterator. This is necessary whenever there is nested grouping operations. Summarize function is hard to use and have few performance issues when computing aggregated values. Also the result from groupby is reusable which makes its performance better.

For more useful blogs, please visit Learn DAX

Similar Posts