Removing Duplicates from Excel Data Based on Selected Columns

I am new to UiPath, My data is stored in an Excel file, and I need to remove duplicates based on one or two columns.

If you suggest any code, please provide it in C#.

Hi @Priya_Rajput

Can you share the input and expected output.

Regards

Hi

Please find the attached screenshot for your reference. In the file, there is a column “TC Key” which needs to be unique in the same sheet.

I want to remove duplicates based on the “TC Key” column, keeping only the first entry of each duplicate. The rest should be deleted, and the updated data should remain in the same sheet.

Screenshot of input data

Screenshot of output data

Hi @Priya_Rajput

Try this

distinctRows=dt.AsEnumerable().GroupBy(Function(x) x.Field(Of String)("TC Key")).Select(Function(grp) grp.First()).CopyToDataTable()

Regards,

Hi

I tried this also but it’s not working (Just to clarify, I’m using C#, not VB.)

@Priya_Rajput

Can you try the below

Code:

// Use LINQ to remove duplicates based on the "TC Key" column
var distinctRows = dt.AsEnumerable()
    .GroupBy(x => x.Field<string>("TC Key"))
    .Select(grp => grp.First())
    .CopyToDataTable();

// Update the existing DataTable with the distinct rows
dt = distinctRows;

Regards,

based on you image you want to keep only the first one that is duplicated.

(From row In  Datatable.AsEnumerable()
                   Group row By TCKey = row("TC Key") Into Group
                   Select Group.First()).CopyToDataTable()

Datatable remove it with you datatable.
image

also you have an activity which help to remove the rows that has all the column the same
image

if this is not working please show us the errore or the output, by ussing output datatable and writeline
image

Hi @lrtetala,

I tried the solution you shared, but it is throwing an error. Please see the attached screenshots for reference.

Screenshot of error:
image

Screenshot of the code Implemented:


Hey @Priya_Rajput

Try This

dataTable = dataTable.AsEnumerable()
.GroupBy(Function(i) i.Field(Of String)(“YourColumnamehere”))
.Select(Function(g) g.First)
.CopyToDataTable

Hammad rafiq Tukdi :slight_smile:

@Priya_Rajput

Can you please open locals panel and show exception details from there?

or if you can share a sample file it would be better

as the above should be working

cheers

locals panel and show exception details:

@Priya_Rajput

please click on magnifier glass to expand…so that you can see the actual exception there

cheers

Here is the complete exception
RemoteException wrapping System.Ref.txt (1.9 KB)

@Priya_Rajput

Your datatable is passing to be null please check that…If possible share the screenshot of workflow

Regards,

@Priya_Rajput

this is exception System.ArgumentNullException: Value cannot be null. (Parameter ‘source’)

basically your dt is not passed properly

and you dont need invoke code same can be done in assign

cheers

I am sharing a sample workflow and input file for your reference.

Sample workflow
testreport.xaml (9.2 KB)

Sample Input File
Sample inputs.xlsx (10.2 KB)

I’ve tried both activities, but I am unable to solve my issue. Could you please help me with how to pass the DataTable?

Screenshot of code implemented
image

Screenshot of Argument

@Priya_Rajput

You haven’t passed the variables properly, you need to pass the variables like below i have modified

Please try this

BlankProcess23.zip (47.0 KB)

Regards,

@Priya_Rajput

Check if there is data in DataTableFinal…looks like it is empty

cheers

I tried this and it works as expected.

Thank you for your support. :blush:

1 Like