I have a CSV file. I need to insert a new column at the beginning with the file name in it

I am approaching the problem using the following method.
1.Creating a datatable (dt) with the filename
1.Reading data from csv into another datatable(dt_csv)
now I wanna join those two datatables in such a way that first column value in every row is the filename.
Finally, I am storing it in another csv file.

I am open for any other approaches as well.
Thanks in advance :slight_smile:

@Aravinda_Gangaram

Use Join DataTable activity to join both the DataTables and then use Write CSV activity and pass Join DataTable output to it.


So I created the flow in this way. Everything ran successfully but I couldnt see a file in the destination.

you can use the add data column activity

That will be much easier

to reorganize that
just use a filter datatable
input dt as it is
Just add column sequence in Keep Columns section, as you want, output datatable will be the one that you need.

Hey @lakshman

How join datatable will add the column in the start?
is there any way to do that?

@rahulsharma How to filter or select the column which is required to join?
image
Assuming that the filename is available in dt_csv datatable.
I declared a variable column_name as “filename”

So what should I put up in the join?

I didn’t mention to use Join datatable

i dont think Join datatable is the solution.

You just need to add a new column in the datatable, which you got after reading csv… ads using “Add Data Column” activity for datatable

then just use filter datatable
in input put the datatable you have after adding the column

and in the output, you put the order in which you want to arrange the columns (as you want the newly added column as the first column)

Got it thanks.
For now that works. I will build my solution based on your suggestion.

Thank you.

@rahulsharma

I thought both the DataTables has some common columns and based on that he wants to join the tables.

This is how it works.

Add Data Column to add.

Then, filter datatable can be used to rearrange the sequence of columns. Just pass the datatable and in output columns mention the sequence you want.

AddColumnAtFirst.zip (2.6 KB)

it looks like the file name is always the same fot the particular dt_csv
then we can

  • add the column filename
  • moving it on first position with setOrdinal method used within a Invoke Method activity
  • setting the file name by assign activity | dt_csv(“FileName”).Expression = " ‘YourFileName’ "

so it will set it to all cols from dt_csv without doing it by a for each X activity

Just in place of Filter. I am using Invoke method activity and using SetOrdinal Method inside it to set the required column at the first position. So, Even if there are few more rows in my raw_data file I wont have any issues in the future.

sadly, I am unable to add an attachment :frowning:

Thanks a lot @lakshman , @rahulsharma
for your insights and help. :slight_smile:

I didn’t know that we could assign it that way. Thank you @ppr . I will update that part of my design.

I was using For each loop :stuck_out_tongue:

Thank you :slight_smile:


Find starter help here:
DCExpressions_AddColOnStart_SetFixedString.xaml (7.5 KB)

Hey @ppr it shows that theres an error stating:

Option Strict On disallows implicit conversions from ‘String’ to ‘Integer’.

filename column datatype is string?

Yeah

Please show some screenshots from the imp. as you can see from the immediate panel and helper.xaml was working we have to inspect some parts. Thanks

feel free to share your xaml with us

properties of that column


When I am using Expression