5
14 Comments

How to de-duplicate rows in Google Sheet based on the first column only?

Hi guys, I need help with Google Sheet de-duplication. As the title says, I am trying to remove duplicate rows of data but based only on the first column.

I have researched for the last 2 days, read and watched a dozen tutorials, but seems it was not that easy. I had success until I added the 4th column which screwed everything.

In case you wanna help, have in mind that I tried and I am aware of a few ways:

  • Built-in Google Sheet duplicate remover (I need to work automatically, not manually)
  • I am aware of the Google Sheet (shit) Addon which I tried and didn't work
  • And the last thing was formulas, which worked until I added the Date column.

The formula I used was to create a new sheet/table and add this formula =UNIQUE('Master list'!A:D). As I said it worked until I added the date column, which makes every row unique. So this formula is not de-duplicating based on Column A only (which is what I need) but on overall all columns.

To summarize, the sheet I have will periodically and automatically update with new rows, so the duplicate rows should be removed. I need to get unique data based on Column A either in the same table or a new table.

I appreciate any help, but honestly would really be grateful if you could provide with copy/paste solution, so I don't need to spend time learning the Google Sheets formulas currently.

Peace ✌

on January 11, 2021
  1. 2

    @daveagill @Freeboots @hieunc @adroitboss Thank you a lot for the time and trying to help. Here I recorded a short video going trough the solution. https://youtu.be/tomNAHbXr5E

    1. 1

      That's awesome Stefan!

  2. 2

    Here is my attempt at winging this with formulas: https://docs.google.com/spreadsheets/d/1WBlnaIODteIQL52jjp65rE1aY1PxdqbezPzJ3sbMQgs/edit?usp=sharing

    The cells with formulas are highlighted in yellow in the sheet.

    Explanation:

    The 'is duplicate?' column F simply tells us if the row is a duplicate or not with this formula:

    =COUNTIF(A$1:INDIRECT("A" & Row()), INDIRECT("A" & Row())) > 1
    

    The INDIRECT("A" & Row()) parts just give us a cell reference based on the current row number, if the row is row #5 then it constructs a reference to A5.

    So as you read down column F we can basically interpret the formula as:

    =COUNTIF(A$1:A2, A2) > 1
    =COUNTIF(A$1:A3, A3) > 1
    =COUNTIF(A$1:A4, A4) > 1
    =COUNTIF(A$1:A5, A5) > 1
    .... etc ...
    

    And you really could just write those in like that or use the mouse to drag the formula down, but I chose to use INDIRECT because you said rows were added dynamically and with INDIRECT we get to use literally the exact same formula down all rows which makes automating the addition of rows easier.

    The COUNTIF is counting how many times the current row's 'A' cell value occur in a range from A1 down to the current row (so not the whole of column A, only from top-to-current-cell).

    We consider the row to be a duplicate if there are previous occurrences of the cell's value above it in the table. Otherwise if there is only 1 occurrence (the one in our current cell) then it is considered unique.

    Finally, the formula over in cell IJ uses the FILTER formula which works a lot like UNIQUE except that it has the ability to check our 'is duplicate' column and filters the table to select only unique rows:

    =FILTER(A:D, F:F = FALSE)
    
    1. 1

      @daveagill Thanks a lot for your efforts to build a sheet and write such a long and detailed explanation. I appreciate you so much man!

      However, I read and tried multiple times, but somehow it didn't de-duplicate. Here I recorded a video showing what and how I did https://youtu.be/gTUNv3p261c .

      Also, if you wanna connect outside of IH, my email is stefan@vanila.io, feel free to write to me there and we can connect over my Slack channel or whatever.

      1. 1

        Hi Stefan, I watched your video (cool way to reply!) and I think the reason it is not working for you is because in your version the "is duplicate" column is in column E but the FILTER formula is looking for it in column F. So assuming column E is where you want it then you can adjust your formula to look there:

        =FILTER(A:D, E:E = FALSE)
        

        ... and it should work.

        As for how to apply the COUNTIF formula automatically - Could you have whatever it is that adds rows to the sheet also add the formula in column E at the same time as adding the data for columns A,B,C,D?

        If not, perhaps you could manually pre-apply the formula all the way down the sheet (or at least a very long way) in preparation for new rows.

  3. 2

    Hi Stefan, its been a while since I did anything in GAppScript, but this guide does almost exactly what you're asking.

    Scroll down to the end of the explanation, you will see a 'Variation' heading, be sure to read that section as it explains how to adjust the script to remove rows with matching data in particular columns.

    In your case I think you would want to replace:

    if(row.join() == newData[j].join()){ duplicate = true; }

    with:

    if(row[0] == newData[j][0]){ duplicate = true; }

    This script will remove duplicates from the currently active (open) sheet. If you wish to alter it to run on a particular sheet at particular times or via an api call, thats beyond the scope of a comment here but certainly possible with a little tweaking.

    1. 2

      @Freeboots I appreciate your effort to help. I am currently testing David πŸ‘† solution, which seems almost there. In case I can't succeed I will try this.

      Thanks once again buddy! πŸ‘Š

  4. 2

    Hey Stefan, just to confirm, you want to filter unique value based on column A, then remove the entire row wherever there is a duplicate, right?

    If so, you can do the following:

    1. Select the entire sheet (Ctrl + A)
    2. From the top toolbar, choose Data > Remove Duplicates
    3. In the Column to analyze, select only column A (in your case)
    4. Click Remove duplicates and done

    1. 1

      hey @hieunc thanks for the suggestion. Well I tried that method before, but as far I understand it didn't work for me, because I don't need this to be done once, but automatically every time new rows of data comes in. So I think this will not work, but I appreciate your time to come here and response.

      Thanks mate!

      1. 1

        I get what you mean by not manual now. I tried this solution, similar idea to @daveagill

        1. Select Column A and "Insert 1 left" to add a column. Your table data is moved over one to columns B - D.
        2. In your newly blank Cell A2, enter formula
          =IF(COUNTIF($B$2:$B2,B2)=1, "Unique", "Duplicate")
        3. Copy the formula from A1 and paste it down the A column range for as far as you have data. The "$" absolute references will ensure everything works smoothly.
        4. Add a new sheet. Copy and paste your old sheet's B - D column headers to the new sheet's A - C.
        5. In the your new sheet's cell A2, enter formula
          =FILTER(OldSheet!B2:D5, OldSheet!A2:A5 = "Unique")
          Your new sheet now has your old sheets data -- Deduplicated!

        Source: https://webapps.stackexchange.com/a/78220

        1. 2

          I am close to this solution, thanks for sharing this.

          I just edited something, in order to capture the range automatically:

          =FILTER(shettt!B:E, shettt!A:A = "Unique")

          Now my question is how do I extend the first formula automatically to infinity rows since the rows will auto-update. Currently, I have 5k rows, and copy/pasting or expanding manually is not what I need.

          1. 1

            I tried a few things and ran out of idea too.

  5. 2

    @AndrewKamphey Andrew, I don't know better person who can help with this on IH, so I am shamefully mentioning you.

    And here the other guys: @jesselnieman @alexisgrant @CMOnline @hieunc @royledoyle

    1. 1

      You can also have the data flow to one tab and query the unique data out of it to another. I think that might achieve what you're looking for. However, be careful because queried data isn't static and will change every time the original data changes. I'm not at a computer at the moment, but I'm confident I can solve your problem.