DAX Formula to Count Duplicates

phillipsg

New Member
Joined
Sep 17, 2013
Messages
2
Hello,

I have a formula to count duplicate rows in Excel. It marks the first instance of a duplicate with a 1 and each duplicate thereafter with a 0.

=IF(COUNTIF($B$2:$B2, B2)>1,0,1)

What I'm trying to write is an equivalent DAX formula that will do the same thing, but I'm not sure how to go about it. I'm almost certain that I need to combine the CALCULATE and COUNTROWS functions to get what I need, but I can't seem to nail down the correct syntax. Any help would be appreciated.

Thanks!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Code:
=IF(
          CALCULATE(
                 COUNTA(TableName[ColumnName]),
                 FILTER(TableName, TableName[ColumnName] = EARLIER(TableName[ColumnName]))
           )>1, 
           0,
           1
     )

This should do it. The =IF() pattern is the same as regular Excel. Replace TableName with your table's name and ColumnName with the column that you want to check for dups.

Basically this is saying..."count the current row value everytime the same value appears earlier in the column"
 
Upvote 0
Mike - thanks for taking the time to respond. That's very close to what I need. In fact, I found a similar formula online that uses the COUNTROWS function instead of COUNTA and achieves the same result. The only problem I haven't been able to resolve is getting each initial duplicate flagged with a 1 and each subsequent dupe with a 0.

I'm getting this:

a1
b1
c0
c0
c0
d1
e0
e0
f1

<colgroup><col style="width:48pt" span="2" width="64"> </colgroup><tbody>
</tbody>

What I want to achieve is this:

a1
b1
c
1
c
0
c0
d1
e1
e0
f1

<colgroup><col style="width:48pt" span="2" width="64"> </colgroup><tbody>
</tbody>
Thanks
Greg

<colgroup><col style="width:48pt" span="2" width="64"> </colgroup><tbody>
</tbody>
 
Upvote 0
Do you have a unique row ID in your data?

If so, a second Calculated Column like this should do the trick:

Code:
=IF(
       TableName[CalculatedColumn1] = 0 &&
       TableName[UniqueRowID] = CALCULATE(
                                MIN(TableName[UniqueRowID]),
                                ALLEXCEPT(TableName, TableName[ColumnName])
                          ),
     1,
     0
  )

CalculatedColumn1 is the first Calc Column from above and ColumnName is the column with dups again.
 
Upvote 0
Do you have a unique row ID in your data?

If so, a second Calculated Column like this should do the trick:

Code:
=IF(
       TableName[CalculatedColumn1] = 0 &&
       TableName[UniqueRowID] = CALCULATE(
                                MIN(TableName[UniqueRowID]),
                                ALLEXCEPT(TableName, TableName[ColumnName])
                          ),
     1,
     0
  )

CalculatedColumn1 is the first Calc Column from above and ColumnName is the column with dups again.

and what mean field TableName[UniqueRowID] in your formula above, from where did you get it ?
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,554
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top