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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
powerbirookiee
Frequent Visitor

DAX Function for Count with filter

Hi everyone,

 

I am new to Power BI and I have no idea in complex DAX functions. I need to create a calculated column or measure (whichever is more appropriate) which counts unique combinations of companies + tags but with some applied rules.

 

articleIDCompanyTagCompany + TagCountDesired Count
123aABCBlackABC - Black11
123aABCPinkABC - Pink2-
456bDEFBlackDEF - Black32
789cXYZPinkXYZ - Pink43
111dAAAWhiteAAA - White54
222eBBBBlackBBB - Black65
222eBBBPinkBBB - Pink7-
222eBBBWhiteBBB - White86
    Count Distinct - 8Desired Count - 6

 

In the sample table above, "PINK" should NOT be counted if and whenever it belongs to the same article as tag "BLACK". If they have the same article ID, BLACK should be the only one counted and Pink should be blank (articleIDs 123a and 222e). Pink can only be counted if it is a standalone tag (articleID 789c).

 

What is the DAX function for this scenario? Any help is appreciated. Thanks!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @powerbirookiee ,

 

 Greg_Deckler's measure will only return 1 in subtoatl. Please try this code to create a measure.

Desired Count = 
VAR __ADD =
    ADDCOLUMNS (
        'Table',
        "Count",
            VAR _COUNTBYID =
                CALCULATE ( COUNTROWS ( 'Table' ), ALLEXCEPT ( 'Table', 'Table'[articleID] ) )
            RETURN
                SWITCH ( TRUE (), [Tag] = "Pink" && _COUNTBYID > 1, BLANK (), 1 )
    )
RETURN
    SUMX ( __ADD, [Count] )

Result is as below.

RicoZhou_0-1668503729796.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @powerbirookiee ,

 

 Greg_Deckler's measure will only return 1 in subtoatl. Please try this code to create a measure.

Desired Count = 
VAR __ADD =
    ADDCOLUMNS (
        'Table',
        "Count",
            VAR _COUNTBYID =
                CALCULATE ( COUNTROWS ( 'Table' ), ALLEXCEPT ( 'Table', 'Table'[articleID] ) )
            RETURN
                SWITCH ( TRUE (), [Tag] = "Pink" && _COUNTBYID > 1, BLANK (), 1 )
    )
RETURN
    SUMX ( __ADD, [Count] )

Result is as below.

RicoZhou_0-1668503729796.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Greg_Deckler
Community Champion
Community Champion

@powerbirookiee So try this:

Desired Count =
  VAR __article = MAX('Table'[articleID])
  VAR __tag = MAX('Table'[Tag])
  VAR __tags = COUNTROWS(FILTER(ALL('Table'),[articleID] = __article))
  VAR __Result = 
    SWITCH(TRUE(),
      __tag = "Pink" && __tags > 1,BLANK(),
      1
    )
RETURN
  __Result

Then you will need this:

First, please vote for this idea: https://ideas.powerbi.com/ideas/idea/?ideaid=082203f1-594f-4ba7-ac87-bb91096c742e

This looks like a measure totals problem. Very common. See my post about it here: https://community.powerbi.com/t5/DAX-Commands-and-Tips/Dealing-with-Measure-Totals/td-p/63376

Also, this Quick Measure, Measure Totals, The Final Word should get you what you need:
https://community.powerbi.com/t5/Quick-Measures-Gallery/Measure-Totals-The-Final-Word/m-p/547907

Also: https://youtu.be/uXRriTN0cfY
And: https://youtu.be/n4TYhF2ARe8



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors