Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Paradroid78
New Member

DAX: Using GROUPBY() to get a simple count of rows in groups

Hi,

 

I have a simple table, let's say 2 columns ("a" and "b"), and I want to do the DAX equivalent of

 

`SELECT a, b, COUNT(*) from TABLE group by a, b`

 

So, I'm looking at GROUPBY and trying all sorts of things but can't work out how to do this:

 

`GROUPBY("table 1", [a], [b], "count", COUNTX(CURRENTGROUP())) // error: Must supply filter argument, but I don't want to filter`

 

`GROUPBY("table 1", [a], [b], "count", COUNTROWS(CURRENTGROUP()) // error: can't use CURRENTGROUP() in COUNTROWS()`

 

`GROUPBY("table 1", [a], [b], "count", COUNT(CURRENTGROUP()) // can't use CURRENTGROUP() in COUNT()`

 

It seems to me this should be the simplest thing ever, but I'm out of options. Could anybody help?

5 REPLIES 5
v-danhe-msft
Employee
Employee

Hi @Paradroid78,

Based on my test, you could refer to below steps:

Sample data:

1.PNG

Create a new table:

Table 2 = GROUPBY('Table1','Table1'[Qtr],"A",COUNTX(CURRENTGROUP(),'Table1'[Value]))

Result:

1.PNG

You could also download the pbix file to have a view.

 

Regards,

Daniel He

Community Support Team _ Daniel He
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks all, this helps a lot.

 

One thing I'm confused about, in the case of all answers using SUMMARIZE, the argument to the summary function seems need the name of the table the summary is being performed on.

 

What though if this is coming from a nested DAX computated table?

 

e.g. take Vic0810's answer:

SUMMARIZE('Table 1', a, b, "RowCount", COUNTROWS('Table 1'))

 

How would I structure the COUNTROWS call if that first argument was inlined, e.g.

 

SUMMARIZE(SELECTCOLUMNS(UNION('Table 1', 'Table 2'), "a", [a], "b", [b]), a, b, "RowCount", COUNTROWS(???))

 

As I haven't got a table name for the computed table, I'm not clear what to pass into the ??? above.

 

It's entirely possible that I may be fundamentally misunderstanding something about how that context sensitive argument to COUNTROWS works, in which case I apologize and hope someone can point me at something that will better my understanding...

 

Hi @Paradroid78,

Could you have used my formula with GROUPBY function?

Could you please tell me if your problem has been solved? If it is, could you please mark the helpful replies as Answered?

 

Regards,

Daniel He

Community Support Team _ Daniel He
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
PattemManohar
Community Champion
Community Champion

@Paradroid78 Please try using this, by adding "New Table" option

 

DAXOutput = SUMMARIZECOLUMNS(SimpleDAX[A],SimpleDAX[B],"Count",COUNTROWS(SimpleDAX)) 




Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




vik0810
Resolver V
Resolver V

SUMMARIZE should do

 

SUMMARIZE('Table 1', a, b, "RowCount", COUNTROWS('Table 1'))

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.