Forum Discussion

kellyylx's avatar
kellyylx
Helper I
2 years ago
Solved

Group duplicates based on different matching columns

Hi, I have this unsorted table of names below and I want to group duplicates based on different duplication matches. Any unique values will be in group 0.

 

name   address
ACurrie Street 
BMary Road Block 4    
CStadium 6
ACurrie Street 
GLuck Ave 
DStanley Road
DStanley Rd
EChuan Road
FGeorgia Ave 4
HKhatib Street
IPeace Road
GLuck Ave 
G

Luck Avenue 

 

Expected Output:

nameaddressdup group by name only     dup group by name and address
ACurrie Street 11
BMary Road Block 4    00
CStadium 600
ACurrie Street 11
GLuck Ave 32
DStanley Road20
DStanley Rd20
EChuan Road00
FGeorgia Ave 400
HKhatib Street00
IPeace Road00
GLuck Ave 32
GLuck Avenue 30

 

How can i do this in power bi?

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi All
    Firstly  HotChilli thank you for your solution!
    And kellyylx   ,We can use the rank function to help you achieve this.

     

    dup group by name only(1) = RANK
    (DENSE,
    FILTER(ALL('Table'),
    CALCULATE(COUNT('Table'[name]),FILTER(ALLSELECTED('Table'),'Table'[name]=MAX('Table'[name])))>=2),
    ORDERBY('Table'[name],ASC),
    DEFAULT)
    dup group by name only (2) = RANK(DENSE,ALL('Table'),ORDERBY([dup group by name only(1)],ASC BLANKS LAST),DEFAULT)
    dup group by name only(3) = 
    VAR A=MAXX(ALL('Table'),'Table'[dup group by name only (2)])
    RETURN 
    IF(
        'Table'[dup group by name only (2)]>=A,
        0,
        'Table'[dup group by name only (2)])
    

     

    For your second requirement, we just need to add adress to the constraints, and everything after that is the same as (2), (3).

     

     

    dup group by name and address(1) = RANK
    (DENSE,
    FILTER(ALL('Table'),CALCULATE(COUNT('Table'[name]),ALLSELECTED('Table'),'Table'[name]=MAX('Table'[name])&&'Table'[address]=MAX('Table'[address]))>=2),
    ORDERBY('Table'[name],ASC),
    DEFAULT)

     

    If in doubt, check out the pbix file I uploaded and I'd be honoured if I could help you out!

     

    Best regards,
    Community Support Team_ Tom Shen

     

    If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

     



5 Replies

  • HotChilli's avatar
    HotChilli
    Community Champion

    Something seems off in the sample table.

    Example: Stanley Road  shows as 2 in 'dup group by name only' - there's only one Stanley Road in the data.
    Currie Street shows as 1 in 'dup group by name only' - there's two in the data.

    • kellyylx's avatar
      kellyylx
      Helper I

      the values are the group number of duplicated records and not the count

       

      if we look at the dup group by name only column, the two A belongs to group 1, D's belong to group 2 and G's belong to group 3

       

      if we look at the dup group by name and address column, the two 'A - Currie Street' belongs to group 1, and the two 'G - Luck Ave' belongs to group 2 

  • HotChilli's avatar
    HotChilli
    Community Champion

    dup group by name only - This is an arbitrary group number?

    Why does name G have group 3? Shouldn't it have been allocated to 2 if we are working down the list? and D would be group 3

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi All
    Firstly  HotChilli thank you for your solution!
    And kellyylx   ,We can use the rank function to help you achieve this.

     

    dup group by name only(1) = RANK
    (DENSE,
    FILTER(ALL('Table'),
    CALCULATE(COUNT('Table'[name]),FILTER(ALLSELECTED('Table'),'Table'[name]=MAX('Table'[name])))>=2),
    ORDERBY('Table'[name],ASC),
    DEFAULT)
    dup group by name only (2) = RANK(DENSE,ALL('Table'),ORDERBY([dup group by name only(1)],ASC BLANKS LAST),DEFAULT)
    dup group by name only(3) = 
    VAR A=MAXX(ALL('Table'),'Table'[dup group by name only (2)])
    RETURN 
    IF(
        'Table'[dup group by name only (2)]>=A,
        0,
        'Table'[dup group by name only (2)])
    

     

    For your second requirement, we just need to add adress to the constraints, and everything after that is the same as (2), (3).

     

     

    dup group by name and address(1) = RANK
    (DENSE,
    FILTER(ALL('Table'),CALCULATE(COUNT('Table'[name]),ALLSELECTED('Table'),'Table'[name]=MAX('Table'[name])&&'Table'[address]=MAX('Table'[address]))>=2),
    ORDERBY('Table'[name],ASC),
    DEFAULT)

     

    If in doubt, check out the pbix file I uploaded and I'd be honoured if I could help you out!

     

    Best regards,
    Community Support Team_ Tom Shen

     

    If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

     



    • kellyylx's avatar
      kellyylx
      Helper I

      Thank you! this is exactly what i need!

      Can you explain to me how the If statement works in dup group by name only works? I dont understand what the VAR A is.