Forum Discussion
Find duplicates in a column and give a value
Hi All,
i want to create a calculates column with specific value when duplicates are found.
if the colum has duplicates give "duplicated"
ths formula i have used is:
You can use this in a Calculated Column Anonymous
Duplicates = VAR varCurrentValue = 'Sample'[Column1] VAR varInstances = COUNTROWS( FILTER( 'Sample', 'Sample'[Column1] = varCurrentValue ) ) var Result = IF( varInstances > 1, "Duplicate", "Unique" ) RETURN Result
You don't need ALL or CALCULATE. ALL in this context removes filters. Tables and Calculated Columns have no filter context, only row context.You can try this:
Duplicates = VAR varCurrentValue = 'Sample'[Column1] VAR varInstances = COUNTROWS( FILTER( 'Sample', 'Sample'[Column1] = varCurrentValue && NOT 'Sample'[Column1] IN { "House", "Table" } ) ) VAR Result = IF( varInstances > 1, "Duplicate", "Unique" ) RETURN Result
10 Replies
- edhans
Community Champion
You can use this in a Calculated Column Anonymous
Duplicates = VAR varCurrentValue = 'Sample'[Column1] VAR varInstances = COUNTROWS( FILTER( 'Sample', 'Sample'[Column1] = varCurrentValue ) ) var Result = IF( varInstances > 1, "Duplicate", "Unique" ) RETURN Result
You don't need ALL or CALCULATE. ALL in this context removes filters. Tables and Calculated Columns have no filter context, only row context. - AnonymousNot applicable
HI,
this help worked. however, now i need an step worward.
is it possible to add into the formula exceptions??
for example give all duplicates except "house" & "table"
Regards
- AnonymousNot applicable
- edhans
Community Champion
You can try this:
Duplicates = VAR varCurrentValue = 'Sample'[Column1] VAR varInstances = COUNTROWS( FILTER( 'Sample', 'Sample'[Column1] = varCurrentValue && NOT 'Sample'[Column1] IN { "House", "Table" } ) ) VAR Result = IF( varInstances > 1, "Duplicate", "Unique" ) RETURN Result- AnonymousNot applicable
Thanks edhans,
just last question, how would you change the "in" for "contain" to avoid all the words containing "house"?Thanks very much for your help
- danishefaFrequent Visitor
- edhans
Community Champion
I would generally not recommend this in Power Query danishefa as it would need to do a table scan. If it was a few hundred rows or perhaps low thousands, it might perform ok, but if more than that, even partitioning data Power Query bogs down, and it would be best done upstream in the source, or downstream in DAX.
- dufoq3
Community Champion
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclSK1YGRTmDSGUy6IJGuYNINSSWQHQsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t]), Ad_DuplicateUnique = Table.AddColumn(Source, "Duplicate/Unique", each if List.Count(List.Select(List.Buffer(Source[Value]), (x)=> x = [Value])) > 1 then "Duplicate" else "Unique", type text) in Ad_DuplicateUnique
- TimWilsensRegular Visitor
Works as a charm!! Thanks!!