How can I insert an If condition to check if there are extra columns in a Data Table?

This is the example output, sometimes there is 1 extra column and sometimes 2.

I want a conditional statement to check the dtInputTable if there are extra columns and remove them regardless if its only 1 column or more.

Thank you

Hi @Greggy

You can try with column count

Use Read range activity ā†’ Dt

Use If activity

Dt.Column.Count

Regards
Gokul

1 Like

HI @Greggy

You will have set of standard column count right? I think you 9 standard columns and 2 unwanted column

So you can try like this

  • After Reading the Datatable and Stored in a variable called DT or whatever.
  • If Dt.Columns.Count>9

Hope this Helps

Regards
Sudharsan

1 Like

Iā€™m using this activity, remove data column, it seems that the excess column is not working
image

How can I remove the excess column dynamically, like what if the excess is 3 columns or 4 moving forward?

Thank you.

we do see the extra columns with the auto generated name ColumnX

Assign Activity
arrExtraColNames | String() - String Array =

yourDataTableVar.Columns.Cast(Of DataColumn).Where(Function (x) x.ColumnName.toUpper().Trim().StartsWith("COLUMN") ).ToArray()

afterwards you can loop over arrExtraColNames and remove the columns with e.g. Remove DataColumn Activity

1 Like

Hi @Greggy ,

If you do know the Column Names that are required before hand, You could store it as an Array of String and use it with DefaultView.ToTable() to preserve only the mentioned required columns.

For Example,Considering Name, ID, Place are the column names you would want to keep/Preserve, we could have the array initialised in the below manner :

columnsToKeep = {"Name","ID","Place"} 

Now, we could use the below Expression to Filter for only the Required columns :

DT = DT.DefaultView.ToTable(false,columnsToKeep)

Here, DT is your Datatable variable.

1 Like