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
Anonymous
Not applicable

Distinct Count in DAX

Hi there!

 

I have a question about a certain type of distinct count in DAX.

 

I have a table that lists different customers, their contract number, and the number of locations each customer has. The customer may have different contracts but the total number of locations that is recorded on each contract remains the same. For example, Customer A has three different contracts but the total number of locations the customer has remains the same, in this case 1 location. 

 

I now want to get a total number of locations for this table. The result in the example below should be 5, as customer A has 1 location, Customer B has 3 locations, and Customer C has 1 location. However, if I just sum it up, the result is 18, since it counts everything. Could somebody tell me what measure would be best to get the result I am after? I hope this all makes sense. Thanks a lot!

 

Location Count.PNG

1 ACCEPTED SOLUTION
selimovd
Super User
Super User

Hey @Anonymous ,

 

with DAX you have to take one step more.

First you should create a virtual table with the customers and the number of locations. Then you can summarize by company.

The following measure is doing that and should solve your problem:

Number of Locations V2 = 
-- Create a virtual table of the distinct Customer Values and add the maximum number of locations per Customer
VAR vSummarizedTable =
    ADDCOLUMNS(
        VALUES( myTable[Customer] ),
        "@NumberOfLocations",
            CALCULATE(
                MAX( myTable[Number of Locations] )
            )
    )
RETURN
-- Summarize the number of locations of the table from above
    SUMX(
        vSummarizedTable,
        [@NumberOfLocations]
    )

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 

View solution in original post

2 REPLIES 2
selimovd
Super User
Super User

Hey @Anonymous ,

 

with DAX you have to take one step more.

First you should create a virtual table with the customers and the number of locations. Then you can summarize by company.

The following measure is doing that and should solve your problem:

Number of Locations V2 = 
-- Create a virtual table of the distinct Customer Values and add the maximum number of locations per Customer
VAR vSummarizedTable =
    ADDCOLUMNS(
        VALUES( myTable[Customer] ),
        "@NumberOfLocations",
            CALCULATE(
                MAX( myTable[Number of Locations] )
            )
    )
RETURN
-- Summarize the number of locations of the table from above
    SUMX(
        vSummarizedTable,
        [@NumberOfLocations]
    )

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 

Hey @Anonymous ,

 

did it work with the Measure I provided?

I'm curious if you could solve this issue 🙂

 

If my approach helped to make it work, I would be happy if you could mark it as solution ✔️. Like this, the next person who is stumbling across this post can see immediately what worked, without trying all the approaches for themselves.
 
If you need any help please let me know.
 
Best regards
Denis
 

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.