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
Mathais_
New Member

COUNT IF With Group By ?

Hi,
Hello, I have a table with this datas.
I would like to add a new column to count the number of IDs per Order for which the OtherID field is not null.

 

My datas :

Order              ID                         OtherID          
J4-11111112121212525252
J4-11111112121212 
J4-11111112121212925874
JF-33333313131313999999

 

And the Result I would want :

Order                ID            OtherID  NotNullOtherID  
J4-11111112121212  525252  2
J4-11111112121212 2
J4-111111121212129258742
JF-333333131313139999991

 

Thank you very much.

5 REPLIES 5
Arul
Super User
Super User

@Mathais_ ,

Create a calculated column with this formula,

NotNullOtherID = 
CALCULATE(
    COUNTROWS(Table1),
    FILTER(
        Table1,
        NOT(ISBLANK(Table1[OtherID])) && 
        Table1[Order] = EARLIER(Table1[Order])
    )
)

Thanks,

Arul





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

Proud to be a Super User!


LinkedIn


A criterea was missing :

Now, how can I force 0 instead of blank value when All Table1[OtherID] values are blank ?

NotNullOtherID = 
CALCULATE(
    COUNTROWS(Table1),
    FILTER(
        Table1,
        NOT(ISBLANK(Table1[OtherID])) && 
        Table1[Order] = EARLIER(Table1[Order]) &&
        Table1[ID] = EARLIER(Table1[ID])
    )
)


 

@Mathais_ ,

where is that 0 is coming from?

Thanks,

Arul





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

Proud to be a Super User!


LinkedIn


Thank you for your help !

When Table1[OtherID] is blank, the calculated field show a blank value exemple :

I want to replace BLANK by 0 

Order              ID                         OtherID      NotNullOtherID 
JJ-222222123456 BLANK
KK-999999876545555551
KK-99999987654 1
FF-55555999999 BLANK

 

I wrote this code but maybe it's not optimized ?

NotNullOtherID = 
If(
    CALCULATE(
        COUNTROWS(Table1),
        FILTER(
            Table1,
            NOT(ISBLANK(Table1[OtherID])) && 
            Table1[Order] = EARLIER(Table1[Order]) &&
            Table1[ID] = EARLIER(Table1[ID])
        )
    ) = BLANK(),
    0,
    CALCULATE(
        COUNTROWS(Table1),
        FILTER(
            Table1,
            NOT(ISBLANK(Table1[OtherID])) && 
            Table1[Order] = EARLIER(Table1[Order]) &&
            Table1[ID] = EARLIER(Table1[ID])
        )
    )
)

@Mathais_ ,

just add +0 at the end of your measure,

NotNullOtherID = 
If(
    CALCULATE(
        COUNTROWS(Table1),
        FILTER(
            Table1,
            NOT(ISBLANK(Table1[OtherID])) && 
            Table1[Order] = EARLIER(Table1[Order]) &&
            Table1[ID] = EARLIER(Table1[ID])
        )
    ) = BLANK(),
    0,
    CALCULATE(
        COUNTROWS(Table1),
        FILTER(
            Table1,
            NOT(ISBLANK(Table1[OtherID])) && 
            Table1[Order] = EARLIER(Table1[Order]) &&
            Table1[ID] = EARLIER(Table1[ID])
        )
    )
)+0

Thanks,

Arul





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

Proud to be a Super User!


LinkedIn


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.