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
Celador
Frequent Visitor

DAX - countif after grouping

Hi,

I need a series of measures to group some results by two columns and then count specific results in the 3rd column - and I'm very very new to Power BI and DAX.

I've done a bunch of googling, read through a number of posts on here, but I just can't seem to amend the measures I've seen to fit my scenario

 

A mock up of some data is in the image below:

Measure #1 would need to count the number of 'Red' Results based on distinct UserId's / ResponseId's

Measure #1 would need to count the number of 'Blue' Results based on distinct UserId's / ResponseId's

 

Using the example data below:

The result of Measure #1 would be 

The result of Measure #2 would be 2 

 

DAX_Group.PNG

 

Or in more visual turns, this is effectively what the measure would be doing (see image below) - grouping by User and Response - followed by a count of 'Red' or 'Blue'

 

DAX_Result.PNG

1 ACCEPTED SOLUTION

Hi @Celador

 

Try these MEASURES as well

 

Red =
COUNTROWS (
    FILTER (
        SUMMARIZE ( Table1, Table1[UserId], Table1[ResponseId], Table1[Result] ),
        Table1[Result] = "Red"
    )
)

And

 

Blue =
COUNTROWS (
    FILTER (
        SUMMARIZE ( Table1, Table1[UserId], Table1[ResponseId], Table1[Result] ),
        Table1[Result] = "Blue"
    )
)

Regards
Zubair

Please try my custom visuals

View solution in original post

12 REPLIES 12
Abduvali
Skilled Sharer
Skilled Sharer

Hi @Celador,

 

  • Measure #1 would equate to 3: Red = Calculate(count('your table name'[Result]),'your table name'[Result] = "Red")
  • Measure #2 would equate to 2: Blue = Calculate(count('your table name'[Result]),'your table name'[Result] = "Blue")

 

Regards

Abduvali

Hi @Abduvali

 

Apologies, I initially marked this as the solution, but it doesn't appear to resolve the issue of grouping by UserId and ResponseId before performing the Count. I was a little presumptious because the measure actually produced results, where all my other attempts have so far just generated errors! 🙂 🙂

 

Your solution looks like it would generate the following:

 

Measure #1 -  7

Measure #2 -  6

 

Instead of  3 & 2

Yes it will give you 6 and 7 unless you will group them by UserID once grouped it will give you 2 and 3

  • Do you know how to use grouping in Power BI?

@Abduvali Not really, I have a rough understanding of the SUMMARIZE function - which is what I thought I would need, but not enough to generate the answer I'm after.

 

Sorry if i wasn't clear in the original post, it's the combination of grouping by those columns, followed by the count that I'm struggling with.

See below on how to group your table to get to desired table look:

Group By function in Edit Query ModeGroup By function in Edit Query Mode

Final ResultFinal Result

 

Hope this helps.

 

 

Regards

Abduvali

Is there no way to write a single measure that will result in the Answer of 3 & 2?  

Create new column with IF statement in it:

Column =

if(Sheet2[Level] = "red",

       CALCULATE(COUNT(Sheet2[level]),Sheet2[Level] = "Red"),

       CALCULATE(COUNT(Sheet2[level]),Sheet2[Level] = "Blue")

)

 

Regards

Abduvali

Hi @Celador

 

Try these MEASURES as well

 

Red =
COUNTROWS (
    FILTER (
        SUMMARIZE ( Table1, Table1[UserId], Table1[ResponseId], Table1[Result] ),
        Table1[Result] = "Red"
    )
)

And

 

Blue =
COUNTROWS (
    FILTER (
        SUMMARIZE ( Table1, Table1[UserId], Table1[ResponseId], Table1[Result] ),
        Table1[Result] = "Blue"
    )
)

Regards
Zubair

Please try my custom visuals

Hi @Zubair_Muhammad - this is what I was after, I've put it in place and it is working as expected - thank you kindly.

 

@Abduvali - really appreciate your time & you did show me a few things, it just wasn't quite what I was after, but I do appreciate it.

Hi @Celador

 

Just for knowledge sharing. Another way of doing this

 

Blue =
COUNTROWS (
    FILTER (
        ALL ( Table1[UserId], Table1[ResponseId], Table1[Result] ),
        Table1[Result] = "Blue"
    )
)

Similar way for Red


Regards
Zubair

Please try my custom visuals

Thanks @Zubair_Muhammad - is there any significant performance cost difference between the two solutions?

You IF statement, create new column:

Column = if(Sheet2[Level] = "red",

                                    CALCULATE(COUNT(Sheet2[level]),Sheet2[Level] = "Red"),

                                         CALCULATE(COUNT(Sheet2[level]),Sheet2[Level] = "Blue")

                   )

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.