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

Power Bi GroupBy count and Having

Hi,

 

I am trying to do belwo in BI with measure :

 SELECT   DISTINCT ID FROM <table1>
WHERE <table1.active =1 and table1.id is not null   >
GROUP BY ID
HAVING COUNT(DISTINCT <table.fieldx)>1
ORDER BY ID

 

I am trying to do the same using following but not able to add the filter which I am doing with Having in above sql statement :

 

Measure1 = CALCULATE(
              DISTINCTCOUNT(Table1[ID]),
                   GROUPBY(Table1,Table1[ID]),FILTER('Table1', Table1[Active] = TRUE() && Table1[ID] <> BLANK() )

Kinldy helpout.

 

 

1 ACCEPTED SOLUTION

Hey @vipul03  , 

 

with a slight variation of the "the measure" the IDs will just be counted:

the measure just counting the ids = 
SUMX(
    ADDCOLUMNS(
        SUMMARIZE(
            FILTER(
                'Table2'
                , 'Table2'[Active] = 1
            )
            , Table2[ID]
        )
        , "dc" , [Distinct Count field 2]
    )
    , 
    var _dc = [dc]
    return
    IF(_dc > 1  , 1 , BLANK())
) 

Then it's possible to create something like this:

image.png

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

16 REPLIES 16
smpa01
Super User
Super User

@vipul03  can you please provide a sample data

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Hi kentyler, here is the sample :

 

IDActivefield 2
100
110
111
211
112
311
401
310
511
600

 

Active is boolean. Field 2 is also boolean. We need to get distinct cout of ids where field 2 value count is more than 1

@vipul03  do you mean this

 

Capture.PNG

Count := CALCULATE(COUNT('Table 1'[ID]))
DistinctCountMeasure:= IF([Count]>1, CALCULATE(DISTINCTCOUNT('Table 1'[ID]),VALUES('Table 1'[ID])),0)
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Hi @smpa01 

basically, I need to get dictinct count of ID where Active is 1 and field2 value count is greater than 1

 

so for the given sample : 

 

result must count :

 

ID1,  will be counted , as active =1 and field 2 has three values 0 ,1 and 2

ID 2,  wil not be counted as field 2 has only one value

ID 3 will be counted as actuive =1 and field two has two values 0 and 1

 

IDs 4 ,5 and 6 won't be counted as they have just one value for field 2

 

 

Hey @vipul03  , 

 

with a slight variation of the "the measure" the IDs will just be counted:

the measure just counting the ids = 
SUMX(
    ADDCOLUMNS(
        SUMMARIZE(
            FILTER(
                'Table2'
                , 'Table2'[Active] = 1
            )
            , Table2[ID]
        )
        , "dc" , [Distinct Count field 2]
    )
    , 
    var _dc = [dc]
    return
    IF(_dc > 1  , 1 , BLANK())
) 

Then it's possible to create something like this:

image.png

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

@TomMartens  thanks for the solution. I have additional question on this, what if I want to return set of IDs instead of count and then apply more filters for counting Ids on top. Can this measure also retturn set of ids/records?

Hey @vipul03 ,

 

I have to admit that I do not understand what you are requesting, but nevertheless this measure returns the IDs as a concatenated string:

the measure the IDs as set = 
CONCATENATEX(
    FILTER(
        ADDCOLUMNS(
            SUMMARIZE(
                FILTER(
                    'Table2'
                    , 'Table2'[Active] = 1
                )
                , Table2[ID]
            )
            , "dc" , CALCULATE([Distinct Count field 2] , ALL(Table2[field 2]))
        )
        , [dc] > 1
    )
    , [ID]
    , " | "
)

This allows to create to something like this:

image.png

I guess the measures are quite generic and can adapted to your likings. Regarding your question about additional filters, I think it should work, but w/o detailed knowledge of your data model, there might be some intricacies that I can't currently oversee.

Please provide examples of your expected result.

 

Regards,

Tom

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Thanks again @TomMartens . Let me try the sample:

 

IDActivefield 2
100
110
111
211
112
311
401
310
511
600

 

So measure 1 already priovide count based on the conditions.

 

Measure 2 is expected to return records that has field 2 as value 1 on top of the records that were counted in measure 1. Because, measure 1 alos counted teh records that has field value 0 and 2 as well.

@TomMartenslet me know if more details needed. Help is appreciated.

Hey @vipul03 ,

 

I still do not understand what your expected result should look like, I will reread tonight, guess I need some sleep now.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

thanks for the response. I shall validate the results and will then mark this as solution.

sorrysample is asked by @smpa01 

Hey,

first I created this measure:

Distinct Count field 2 = DISTINCTCOUNT('Table2'[field 2]) 

then I created this measure, the final measure:

the measure = 
SUMX(
    ADDCOLUMNS(
        SUMMARIZE(
            FILTER(
                'Table2'
                , 'Table2'[Active] = 1
            )
            , Table2[ID]
        )
        , "dc" , [Distinct Count field 2]
    )
    , 
    var _dc = [dc]
    return
    IF(_dc > 1  , _dc , BLANK())
) 

This allows to create a table visual like so:
image.png

Wondering if this provides the expected result.

 

Regards,

Tom

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

@vipul03  not sure if you are looking for this

 

granular.PNGDesiredCount: = 
IF (
    CALCULATE (
        COUNT ( 'Table 1'[field 2] ),
        KEEPFILTERS ( FILTER ( ALL ( 'Table 1'[Active] ), 'Table 1'[Active] = 1 ) )
    ) > 1,
    DISTINCTCOUNT ( 'Table 1'[ID] ),
    BLANK ()
)
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
kentyler
Solution Sage
Solution Sage

Someone else also had this question: https://community.powerbi.com/t5/Desktop/Group-by-and-Having/td-p/448502  basically you can use the DAX function SUMMARIZE() to mimic the SQL Group by and Having clauses





Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


sorry but this one doesn't work. Also, as called out I need filtered on second condition as well(whioch is mentioned in having, that's counting different value in the other field)

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.