Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use GROUPBY function in PowerBI?

Tags:

powerbi

dax

I tried using group by DAX function in PowerBI as Measure, New Column, New Table but I get an error in validating the function,

New Table = GROUPBY(
            'tab1',
            'tab1'[color],
            "Group By color",
            sum('tab1'[count_lables])
          )

Error : Function 'GROUPBY' scalar expressions have to be Aggregation functions over CurrentGroup(). The expression of each Aggregation has to be either a constant or directly reference the columns in CurrentGroup().
like image 915
Dave D. Avatar asked Sep 19 '16 10:09

Dave D.


People also ask

Is there a GROUPBY function in Power BI?

GROUPBY is a type of query which is also used in Power BI. There is an editor in Power BI, known as the “Query Editor” window, which has various queries for accessing data. Such a query is GROUPBY which is used to group rows based on a given value.

How do I group two columns in Power BI?

Use an aggregate function to group by one or more columns Select Group by on the Home tab. Select the Advanced option, so you can select multiple columns to group by. Select the Country column. Select Add grouping.

Can we group tables in Power BI?

Grouping tables in Power BI is a powerful modeling technique that allows you to select from overlapping groups either in a slicer or across a matrix. Useful for scenarios where say, you have several ad campaigns going on and an individual product can be a part or more than one campaign.


1 Answers

The error says you need to use an aggregation function over a group, you are using SUM function which doesn't sum values over any group. In your case you need to use SUMX aggregation function with CURRENTGROUP() option. The CURRENTGROUP determines the groups that must be added based on the current row.

Try using the below expression instead.

New Table = GROUPBY (
        tab1,
        tab1[color],
        "Group by color", SUMX ( CURRENTGROUP (), tab1[count lables] )
    )

Let me know if this helps.

like image 148
alejandro zuleta Avatar answered Oct 25 '22 23:10

alejandro zuleta