web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Adding Variable As A C...
Power Automate
Answered

Adding Variable As A Column To Datatable

(0) ShareShare
ReportReport
Posted on by 80

How do I add a variable as a column to each row of a datatable? Example

 

Datatable contains Names and Ages, in this case all the members of the group are males so I want value of the variable "sex". which is Male to appear on each row of Datatable.

 

Thank you

Before

GenAi_0-1718394886612.png

 

After, should look like this

GenAi_2-1718394991935.png

 

 

 

I have the same question (0)
  • Verified answer
    VishnuReddy1997 Profile Picture
    2,656 Super User 2025 Season 2 on at

    Hi @GenAi ,

     

    Please find the below.If you want you can set that Sex into variable and call that in Update data table item.

    VishnuReddy1997_0-1718426286119.png

     

    Code:

    Variables.CreateNewDatatable InputTable: { ^['Name', 'Age', 'Gender'], [$'''Sam''', 10, $''''''], [$'''Peter''', 20, $''''''], [$'''Mike''', 30, $''''''], [$'''Ray''', 40, $''''''] } DataTable=> DataTable
    SET RowValue TO 0
    LOOP FOREACH CurrentItem IN DataTable
     Variables.ModifyDataTableItem DataTable: DataTable ColumnNameOrIndex: $'''Gender''' RowIndex: RowValue Value: $'''Male'''
     Variables.IncreaseVariable Value: RowValue IncrementValue: 1
    END

     

    (Note:- if you got your solution you can mark as solution and gives kudos)


    Thanks & Regards

    Vishnu Reddy

     

  • GenAi Profile Picture
    80 on at

    I see your logic and love it but realized that I asked the wrong question.  My datatable was extracted from an html table and doesn't contain a column header "Account".  I have updated the flow based on your logic but not sure how I can add the column header " Account" to the table. Please see below my exact flow.  The single datatable "Header_048" is what I want to appear in the column " Account" for each row. How do I get this done? Thank you

     

    GenAi_0-1718639086961.pngGenAi_1-1718639456557.png

     

  • Verified answer
    Deenuji_Loganathan_ Profile Picture
    6,250 Super User 2025 Season 2 on at

    @GenAi 

     

    Lets make it simple. I go with your iterated example:

    Deenuji_1-1718679757517.png

     

    Then I pass this datatable to .Net script where it will do all the magics.

     

    After executing .Net script:

    Deenuji_2-1718679817463.png

    Overall flow screenshot:

    Deenuji_4-1718679929934.png

     

     

    To integrate this logic into your workflow:

    1. Drag and drop the .Net script into your workspace.

    2. Click the Edit button on the script parameter and create a .Net parameter as shown in the screenshot below.

    3. Insert the C# code as displayed below and modify it if you require a different default value (for instance, "Male" is set as the default here).

     

    The function will accept your datatable as input, insert an "Account" column with the default value "Male," and then return the updated datatable.

     

    Deenuji_3-1718679882527.png

     

     

    Full workflow code (To understand how it works, simply copy and paste the full workflow code into your PAD flow):

    Variables.CreateNewDatatable InputTable: { ^['Name', 'Age'], [$'''Sam''', 10], [$'''Peter''', 22], [$'''Mike''', 30], [$'''Ray''', 40] } DataTable=> Header_048
    Scripting.RunDotNetScript Imports: $'''System.Data''' Language: System.DotNetActionLanguageType.CSharp Script: $''' // Check if the Gender column already exists
     if (!dt.Columns.Contains(\"Gender\"))
     {
     // Add the Gender column with default value \"Male\"
     DataColumn genderColumn = dt.Columns.Add(\"Gender\", typeof(string));
     genderColumn.DefaultValue = \"Male\";
    
     // Set default value for existing rows (if any)
     foreach (DataRow row in dt.Rows)
     {
     row[\"Gender\"] = genderColumn.DefaultValue;
     }
     }''' @'name:dt': Header_048 @'type:dt': $'''Datatable''' @'direction:dt': $'''InOut''' @dt=> Header_048

     

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

  • Verified answer
    GenAi Profile Picture
    80 on at

    I tried your suggestion an got an error message (Please see below). Also, I was wondering with your solution, do I need to state all the variable inputs? There are lots of rows, almost impossible to state all of them.   What do you suggest? Sorry I am a beginner and making it harder to get help. Thank you

    Variables.CreateNewDatatable InputTable: { ^['Name', 'Age'], [$'''Sam''', 10], [$'''Peter''', 22], [$'''Mike''', 30], [$'''Ray''', 40] }

     

     

    GenAi_0-1718747299421.png

     

  • GenAi Profile Picture
    80 on at

    I took @VishnuReddy1997 procedure and used @Deenuji code to add the column. It worked beautifully. Thank you two so much. I don't know which one to accept as the solution since its a joint effort.

  • Verified answer
    Deenuji_Loganathan_ Profile Picture
    6,250 Super User 2025 Season 2 on at

    @GenAi 

     

    Please accept both suggestions as solution. Thank you and Happy Automation!!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 503 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 321 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard