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

DAX formula to compute the distinct number of accounts that have duplicate amounts

Hello everyone,

I am new to the forum and I would be glad to anyone who could help me resolve the next issue :

The next image is a screenshot of my power bi report.

It is a table that contains :

  • First column “account number & name” (taken from my table of facts called "FEC")
    Second column is a list of amounts also taken from the same table (not summarized)
  • third column is the count of the amounts in my facts table (in order to identify duplicates) that I calculated by using this DAX formula : COUNT(FEC[Montant])

CheikhnaML_0-1628542080429.png

 

This table is done as follows :

CheikhnaML_1-1628542080434.png

 

Now, I would like to compute the number of distinct accounts that have duplicates (ie counts >=2).

I tried the next formula :

 

#Distinct accounts =
CALCULATE(

DISTINCTCOUNT(FEC[CompteNum]),

    FILTER(

    FEC,

    [#Occurences]) >= "2"

    )

)

but I am running into an error that basically says that 

 

In case, you would need my data model : here it is :

CheikhnaML_2-1628542080444.png

 

Sorry for the fact that my power bi interface is in French

Thank you in advance

2 ACCEPTED SOLUTIONS
thedatahiker
Employee
Employee

@CheikhnaML  You can try something like this. I added in the Montant as I assumed you only wanted to include accounts where they had the duplicate of the same Montant values. 

 

CALCULATE (
DISTINCTCOUNT ( FEC[CompteNum] ),
FILTER (
ADDCOLUMNS (
SUMMARIZE (
'FEC',
'FEC'[CompteNum],
'FEC'[Montant] ),
"#Occurences", [#Occurences] )
[#Occurences] >= 2))

 

View solution in original post

thedatahiker
Employee
Employee

Hi @CheikhnaML 


Happy to try and explain.

 

This is a simplistic example of what is happening in the DAX query I shared with you

  1. With Summarize we are creating a temporary table that only includes the unique combination of account id and montant from the SEC table.
  2. Using the temporary table in the step above we are using the function ADDCOLUMN to add a new column for the # occurrences.
    1. You can think of this similar to using a group by query in SQL 
  3. Now that we have that table constructed in memory we can filter the rows of that table for only the values that have #Occurences >= 2
    1. You can think of this as similar to a having clause in SQL
  4. We now use the previously returned table as the basis for our CountDistinct of account id

    In your original query you were applying the filter on a row context. Essentially you are saying give me rows of this table where #occurences >= 2. Since every row in your table can only have #occurences = 1 you don't get any results.

This is my best attempt at explaining the logic. It would likely be helpful to review documentation from SQLBI about this topic. Best Practices Using SUMMARIZE and ADDCOLUMNS - SQLBI

View solution in original post

4 REPLIES 4
thedatahiker
Employee
Employee

Hi @CheikhnaML 


Happy to try and explain.

 

This is a simplistic example of what is happening in the DAX query I shared with you

  1. With Summarize we are creating a temporary table that only includes the unique combination of account id and montant from the SEC table.
  2. Using the temporary table in the step above we are using the function ADDCOLUMN to add a new column for the # occurrences.
    1. You can think of this similar to using a group by query in SQL 
  3. Now that we have that table constructed in memory we can filter the rows of that table for only the values that have #Occurences >= 2
    1. You can think of this as similar to a having clause in SQL
  4. We now use the previously returned table as the basis for our CountDistinct of account id

    In your original query you were applying the filter on a row context. Essentially you are saying give me rows of this table where #occurences >= 2. Since every row in your table can only have #occurences = 1 you don't get any results.

This is my best attempt at explaining the logic. It would likely be helpful to review documentation from SQLBI about this topic. Best Practices Using SUMMARIZE and ADDCOLUMNS - SQLBI

@thedatahiker Thank you for taking the time to answer my question. 

I appreciate it. 

thedatahiker
Employee
Employee

@CheikhnaML  You can try something like this. I added in the Montant as I assumed you only wanted to include accounts where they had the duplicate of the same Montant values. 

 

CALCULATE (
DISTINCTCOUNT ( FEC[CompteNum] ),
FILTER (
ADDCOLUMNS (
SUMMARIZE (
'FEC',
'FEC'[CompteNum],
'FEC'[Montant] ),
"#Occurences", [#Occurences] )
[#Occurences] >= 2))

 

Hi @thedatahiker,

Thank you for your reply. 

It worked. 

Would you mind tell me why my formula was not working ?

Why was I obliged to used the "Summarize" fonction inside my "Filter" function in order to make it work ?

 

Thank you in advance. 

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.