How to create a new dt with columns coming from another dt

Me again,

I would like to create a new DT with columns from another dt.
I have a DT (let’s call it dt_MAster) with a number of columns (see image)


I have to take only 4 columns (and the data that goes with it) to create another DT which must have the following form (see image).
Dt2_reduce

Can someone tell me how to go about it please ?

@ppr
@oddrationale

Hey @najoua.abbaci

I have made a video on similar use case: Adding Specific Columns From One Data Table To Other - YouTube

Please mark it as solution if it solves your problem.

Cheers,
Parth

1 Like

@najoua.abbaci you can use DataView.ToTable to choose specific columns from the original table. Use the rowfilter constructor to choose something impossible (e.g. 1=2) if you want to copy the structure only and ignore the data from the original table.

2 Likes

@najoua.abbaci
give a try on following:

Assign:
left side: newDT
right side: sourceDataTable.Var.Clone

define an array of String with the columns you would like to keep
ColList = {“Col1”,“Col2”} (Symbolic it would look like this

Assign:
left side: newDT:
right side: newDT.DefaultView.ToTable(False, ColList)

find starter help here:
DTStructure_QuickReduceColList.xaml (7.2 KB)

1 Like

Thank you all for your answer.
@Parth_Doshi I check your video, it is intersting, but my case was, finally, much easier.
@Dave
Actually, I found by using the filterData Table activity. I only had to keep only the columns needed and write them in the order needed too.

Now I have to creat a new column on the first position with the date of the day, any idea ?

Thankx again

always here to help, thank you very much !

@najoua.abbaci
moving a column can be done with an invoke method

SetOrdinal method and providing columnname and new position (0 based index)
grafik

Moving 2nd col to first position

We do it like this as the Ordinal Property is a readonly property

2 Likes

Thank you both of you. Very appreciated !

My last issue is to fill automatically into this “Date” column, the date of the day. I have to create this column and the value coz there is no data about Date in the Master DT.

Thanks again

I deleted my previous post as it looks like .ordinal is a read-only property so it wasn’t letting me change it with an assign - make sure to use the SetOrdinal method mentioned by @ppr instead.

As to how you fill in the column, you can use a for each row activity and assign row.item(“NewColumnName”) = today

or if you want to have it be in a specific format and be a string value (what i usually do when writing back to excel), then you can use today.ToString(“dd/MM/yyyy”) - change the format however you’d like. See here for details on how to format datetime into string: Custom date and time format strings | Microsoft Learn

2 Likes

when creating a new column have a look on the DefaultValue property maybe it can be used for inserting by default todays date. Give a try

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.