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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
maashi
Frequent Visitor

Distinct CountX Help

Hi, have tried similar Distinct countx type posts in the forum but can't seem to see an answer to what I want.

 

I have the following code, it is correct but I need the countx bit to be distinct and I have tried adapting workarounds found in this forum but it has not helped.

 

DIVIDE(

        COUNTX(

            FILTER(

                fact_NCP_Reporting,

                fact_NCP_Reporting[NCP]="NCP"

                ),

            fact_NCP_Reporting[Sales Order No]

            ),

        (

         SUMX(

            FILTER(

                fact_NCP_Reporting,

                fact_NCP_Reporting[NCP]="Sales Order"

                ),

            fact_NCP_Reporting[SQM]

            )

            /1000

        )

    )

 

The full dataset is almost 1000000 lines and will keep growing daily so I can't have a solution that's too cumbersome on the processing speed either.

 

The essential columns are below, without copying everything else in:

 

Sales Order NoNCPSQM
1947525NCP2.5
1947529NCP3.5
1947529NCP3.3
1871488Sales Order0.5
1934149Sales Order0.9
1934149Sales Order1.3
1913819Sales Order2.1
1913819Sales Order1.8
1913819Sales Order1.9
1913819Sales Order0.7
1923551NCP1.2
1899619Sales Order2.7

 

I need to divide the number of unique sales orders (By distinct counting the sales order number column), that are classified as NCP, against the total sqm of sales orders (so summing everything in the SQM column that has 'Sales order' not 'NCP' in the same line. The sumx part of the code is fine but how do I get a distinct countx to work? There is also a date column that I haven't put into this snapshot - so I'm after a monthly value of NCP count / Square meter sales.

 

Thank you for your suggestions

 

Regards

 

Marcy

1 ACCEPTED SOLUTION
Anonymous
Not applicable

HI,@maashi 

Regarding the issue you raised, my solution is as follows:

1.First I have created the following table and the column names and data are the data you have given:

vlinyulumsft_0-1718248081862.png

2. I updated your CountX section to the following measure:

 

Measure 2 = CALCULATE(DISTINCTCOUNT(fact_NCP_Reporting[Sales Order No]),FILTER('fact_NCP_Reporting','fact_NCP_Reporting'[NCP]="NCP"))

 

3.The output is as follows:

vlinyulumsft_1-1718248126767.png

4.All code updates are as follows:

 

Measure 3 = DIVIDE(

         CALCULATE(
            DISTINCTCOUNT(
                fact_NCP_Reporting[Sales Order No]),
                FILTER('fact_NCP_Reporting',
                'fact_NCP_Reporting'[NCP]="NCP"))
                ,

        (

         SUMX(

            FILTER(

                fact_NCP_Reporting,

                fact_NCP_Reporting[NCP]="Sales Order"

                ),

            fact_NCP_Reporting[SQM]

            )

            /1000

        )

    )

 

5.The results of this section are as follows:

vlinyulumsft_2-1718248167469.png

6.With your date column requirements, my metrics are updated as follows:

 

Measure 4 = 

DIVIDE(

         CALCULATE(
            DISTINCTCOUNT(
                fact_NCP_Reporting[Sales Order No]),
                FILTER(ALLSELECTED('fact_NCP_Reporting'),
                'fact_NCP_Reporting'[NCP]="NCP"&&'fact_NCP_Reporting'[month]=MAX('fact_NCP_Reporting'[month])))
                ,

        (

         SUMX(

            FILTER(

                ALLSELECTED(fact_NCP_Reporting),

                fact_NCP_Reporting[NCP]="Sales Order"&&'fact_NCP_Reporting'[month]=MAX('fact_NCP_Reporting'[month])

                ),

            fact_NCP_Reporting[SQM]

            )

            /1000

        )

    )

 

7.The end result is as follows:

vlinyulumsft_3-1718248212237.png

Please find the attached pbix relevant to the case.

 

 

Best Regards,

Leroy Lu

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

Hi Leroy

 

Thank you so much, that worked perfectly. I didn't think I could use Distinct Count directly, but couldnt work out that i could nest it within Calculate.

 

Have a great day

 

Marcy

Anonymous
Not applicable

HI,@maashi 

Regarding the issue you raised, my solution is as follows:

1.First I have created the following table and the column names and data are the data you have given:

vlinyulumsft_0-1718248081862.png

2. I updated your CountX section to the following measure:

 

Measure 2 = CALCULATE(DISTINCTCOUNT(fact_NCP_Reporting[Sales Order No]),FILTER('fact_NCP_Reporting','fact_NCP_Reporting'[NCP]="NCP"))

 

3.The output is as follows:

vlinyulumsft_1-1718248126767.png

4.All code updates are as follows:

 

Measure 3 = DIVIDE(

         CALCULATE(
            DISTINCTCOUNT(
                fact_NCP_Reporting[Sales Order No]),
                FILTER('fact_NCP_Reporting',
                'fact_NCP_Reporting'[NCP]="NCP"))
                ,

        (

         SUMX(

            FILTER(

                fact_NCP_Reporting,

                fact_NCP_Reporting[NCP]="Sales Order"

                ),

            fact_NCP_Reporting[SQM]

            )

            /1000

        )

    )

 

5.The results of this section are as follows:

vlinyulumsft_2-1718248167469.png

6.With your date column requirements, my metrics are updated as follows:

 

Measure 4 = 

DIVIDE(

         CALCULATE(
            DISTINCTCOUNT(
                fact_NCP_Reporting[Sales Order No]),
                FILTER(ALLSELECTED('fact_NCP_Reporting'),
                'fact_NCP_Reporting'[NCP]="NCP"&&'fact_NCP_Reporting'[month]=MAX('fact_NCP_Reporting'[month])))
                ,

        (

         SUMX(

            FILTER(

                ALLSELECTED(fact_NCP_Reporting),

                fact_NCP_Reporting[NCP]="Sales Order"&&'fact_NCP_Reporting'[month]=MAX('fact_NCP_Reporting'[month])

                ),

            fact_NCP_Reporting[SQM]

            )

            /1000

        )

    )

 

7.The end result is as follows:

vlinyulumsft_3-1718248212237.png

Please find the attached pbix relevant to the case.

 

 

Best Regards,

Leroy Lu

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

 

 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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