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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
Msri
Regular Visitor

DAX self join query to get distinct count based on filter criteria

Hi All,

I am getting stuck in converting a SQL query to DAX. 

I want distinct count of ColumnA when all IDs in columnB has value = 'NK'(ColumnC). This needs to be checked for every Date(grouped by date). So from the below example, for 11/8/2024, the DAX needs to return the total of 1(Because only 145 has all column B values with NK and the others have a mix of K and NK). For 11/1/2024, the total will be 0. Basically counting Column A when all the grouped values in Column B have NK

This DAX is used in a matrix which splits these counts based on aging bucket and area.

Greatly appreciate the help.

The SQL that  wrote is as below (wrote based on checking the negative condition)

select distinct ColumnA from TableA 

     (select count(1) from TableA TA1 where TA.ColumnA = TA1.ColumnA and TA.Date=TA1.Date and (TA1.ColumnB='K' or                   TA1.columnA is null)

     )=0

)

 

Table A

DateColumnAColumnBColumnC
11/8/2024123345K
11/8/2024123333 
11/8/2024145678NK
11/8/2024145666NK
11/8/2024145786NK
11/8/2024157567NK
11/8/2024157555K
11/1/2024145666K
11/1/2024145786NK

 

1 ACCEPTED SOLUTION
FreemanZ
Community Champion
Community Champion

hi @Msri ,

 

try like:

measure =

VAR _table =

ADDCOLUMNS(

    SUMMARIZE(

        data,  

        data[date],

        data[ColumnA]

    ),

    "ColC",

    CALCULATE(

        CONCATENATEX(

            VALUES(data[ColumnC]),

            data[ColumnC], ", "

        )

    )

)

VAR _result =

COUNTROWS(

    FILTER(

       _table,

       [ColC]="NK"

    )

)

RETURN _result

View solution in original post

4 REPLIES 4
FreemanZ
Community Champion
Community Champion

hi @Msri ,

 

try like:

measure =

VAR _table =

ADDCOLUMNS(

    SUMMARIZE(

        data,  

        data[date],

        data[ColumnA]

    ),

    "ColC",

    CALCULATE(

        CONCATENATEX(

            VALUES(data[ColumnC]),

            data[ColumnC], ", "

        )

    )

)

VAR _result =

COUNTROWS(

    FILTER(

       _table,

       [ColC]="NK"

    )

)

RETURN _result

@FreemanZ : Thanks a lot, the solution works perectly

Msri
Regular Visitor

Thanks for the reply. I added the above measure, but I am getting 0 as total. From SQL I got 9 rows

AntrikshSharma
Super User
Super User

@Msri This works?

AntrikshSharma_0-1731525086477.png

Measure 2 = 
INT ( 
    COUNTROWS ( Msri ) = 
        CALCULATE ( 
            COUNTROWS ( VALUES ( Msri[ColumnB] ) ), 
            Msri[ColumnC] = "NK" 
        )
)

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.