Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Retrieve distinct values by category

Hello, the table on the left (image below) contains duplicates in Column1 but the value in Column2 can have repeating values (or not). From this table, I would like to display a table that would in...
  • PhilipTreacy's avatar
    4 years ago

    Hi Anonymous 

    You can do this in Power Query.

    Download the PBIX file with the solution.

    Create a composite column for Col1+Col2 e.g. A1, A1, A2 etc.  Removing duplicates from this gets rid of multiple A1's etc.

    Secondly, by counting the number of times a value in Col1 appears, you can then remove any row where the value only appears once.

    This is the code

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJUitVBZxmBWU5AljGY5QxkmcBZpmCWC5BlBmeZYxGDsFyBLAs4yxILy9AAzHQDMYFuiAUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column2", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each [Column1] & Text.From([Column2])),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Count Val", each List.Count(List.PositionOf(#"Added Custom"[Column1], [Column1] ,2))),
        #"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([Count Val] <> 1)),
        #"Removed Duplicates" = Table.Distinct(#"Filtered Rows", {"Custom"}),
        #"Removed Columns" = Table.RemoveColumns(#"Removed Duplicates",{"Custom", "Count Val"})
    in
        #"Removed Columns"

    This is the resulting table

    Regards

    Phil

     

  • v-jingzhang's avatar
    4 years ago

    Hi Anonymous 

     

    If you want a DAX method, it would be:

     

    First add a column in original table to count the unique values in Column2 for each Column1 item.

     
    UniqueCount = CALCULATE(DISTINCTCOUNT('Table'[Column2]),ALLEXCEPT('Table','Table'[Column1]))

     

    Then create a new table with

     
    New Table = DISTINCT(SELECTCOLUMNS(FILTER('Table','Table'[UniqueCount]>1),"Column1",'Table'[Column1],"Column2",'Table'[Column2]))

     

    Best Regards,
    Community Support Team _ Jing
    If this post helps, please Accept it as Solution to help other members find it.