Forum Discussion

powerbirookiee's avatar
powerbirookiee
Frequent Visitor
3 years ago
Solved

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!

  • Anonymous's avatar
    Anonymous
    3 years ago

    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.

     

    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.

     

2 Replies