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 / Convert CSV to Excel a...
Power Automate
Unanswered

Convert CSV to Excel and send email

(1) ShareShare
ReportReport
Posted on by 8
Hello mates, hope everyone's doing well.

To explain it, when the user clicks in a button on our powerBI, it sends a table like this one to power automate:



What I have right now is the following flow:



So after "filter array", I can create a csv/html table with elements for each row of X email and could send it in the email's body.
What I'm missing here is a way to send it as an excel file attached to the email. Can somebody help me with that?
Categories:
I have the same question (0)
  • Suggested answer
    rzaneti Profile Picture
    4,241 Super User 2025 Season 2 on at
    Hi,
     
    You can send the report as an Excel file, but you will need some actions for that. The general idea is to keep an Excel table template somewhere in SharePoint or OneDrive and use it for creating your reports. Here is the step-by-step:
    - Copy file: First, you will need to create a copy of your template file, so you can populate the copy while preserving the template clean. You can name it on your convenience, but I recommend you to include a timestamp based name, as it will prevent any unexpected overwriting of previous reports. 
    Apply to each + Add row to a table: After that, you must populate the Excel table in your copied file. For that, you will need to loop all records retrieved by Power BI and add them to the table. At this moment, as you are selecting the file dynamically, you will need to add the records in a JSON notation (this thread contains some examples on how this entry may looks like: https://community.powerplatform.com/forums/thread/details/?threadid=82fef936-1c7a-ef11-a671-6045bdfe9661). 
    - Get file content: In Power Automate, you cannot simply attach the file to an email, you first need to get the file content, and then add it to the Send email action.
    - Send email: Finally, you can pass the Body property from your Get file content action as Attachment content input, and name it on your convenience (don't forget to include the file extension).
     
    If the instructions are not clear enough or if you need some additional details on any of the steps, let me know and I will be glad to help. In the next few days, if needed, I can provide you with some screens to illustrate these steps.
     
    Let me know if it works for you or if you need any additional help!
    -------------------------------------------------------------------------
    If this is the answer for your question, please mark the post as Solved.
    If this answer helps you in any way, please give it a like.
     
     
     
  • RodrigoRLF Profile Picture
    8 on at
    Rzaneti, it's clear but I'm not sure about the Copy file part, I have a template in a sharepoint, how do I get a copy of it? Also, if its not asking too much, I would like to see some screenshots of those steps, it would help a lot!
  • Verified answer
    rzaneti Profile Picture
    4,241 Super User 2025 Season 2 on at
    Hi,
     
    Sorry for the late responses: I was fixing some small issues in the recommended flow design :)
     
    Knowledge base
    As we are talking about a lot of different topics in this answer, I'm sharing some articles from different complexity levels that I wrote in the past about some of the processes discussed here, so whoever finds this thread in the future will be able to explore it in more details.
     
     
    Overview
    Here is the flow overview: 
     
     
     
    For this example, I'm working with the following dataset (from your end, you will work with the data pulled from PBI):
     
    And my goal is to populate the following template:
     
     
    Copying template file
    We start by capturing the file content from the template, with the Get file content action, and then using its output for creating a new file where the table will be populated. To achieve it, you need to add a Create file action, populating the File content field with the dynamic content from the previous action (marked in red). For the File name input, make sure to insert a unique name and to include the .xlsx extension. I usually name it with a timestamp, which can be achieved with a formatDateTime expression (marked in blue):
     
     
     
    Populating the copy
    Then we will need to loop all records from the variable (in your case, from Power BI) with an Apply to each action. Inside the Apply to each, insert an Add a row into a table action, using the id dynamic content from Create a file action (marked in blue). It will dynamically reference the copy extracted from the template. You can manually type the table name (it uses the same name from the template's table), and for the Row input you will need to use a JSON notation, where the column names will operate as keys (like the hardcoded id, name and logged hours). As value for each of these keys, you must insert the dynamic content from your current iterated record, as marked in red:
     
    Note: for some reason, my Power Automate is representing these records' dynamic contents with the Manual trigger icon. You can ignore it, as these value are actually coming from the records variable.
     
     
    Optional and extra step: removing the empty first row in the table
    At this point, your table may look like this:
     
    The data is there, but you have this annoying extra first row in the table. If you are ok with that, you can skip this step. If you want to remove it, then you can use an Office Script for that.
     
    In any Excel file, go to Automate tab (highlighted in red) and click in New Script (highlighted in blue). If this is the first time that you are creating a script, click in Write code, and then the Code Editor will appear in the right side of your screen (marked in orange):  
     
    Replace everything within the the Editor for the following script:
     
    function main(workbook: ExcelScript.Workbook) {
      //Script to remove first row from a table, if it's an empty row
     
      //Get table by name. Replace employees_hours for your table's name 
      const tbl = workbook.getTable('employees_hours')
     
      //Get values from the first row, which is represented in an array with the elements qty corresponding to the number of cols
      const firstRowValues = tbl.getRangeBetweenHeaderAndTotal().getRow(0).getValues()[0]
     
      //Test if first row is fully empty. If yes, delete it, shifting the cells to the top
      if(firstRowValues.every(e => e === "")){
        tbl.getRangeBetweenHeaderAndTotal().getRow(0).delete(ExcelScript.DeleteShiftDirection.up)
      }
     
    }
     
     
    This script is basically accessing your Excel file and checking if the first row of a given table is empty. If yes, it will delete this first row. In the const tbl line of code, make sure to replace employees_hours for your actual table name. 
     
    Save your script and let's get back to Power Automate. Out of the Apply to each block, add a Delay of at least 1 minute. This step is important, because sometimes the updates from Power Automate to Excel take some seconds (or minutes) to be executed. I ran some tests and 2 minutes worked great for me.
     
    After that, add a Run script action, using the Id dynamic content from your Create file action as input for File. Select the name of the script that you just created from the dropdown. This action will execute the script for you:
     
    Getting populated template file content
    Now let's give some seconds or minutes of delay to Excel and get the content of the populated template. You can achieve it by adding another Get file content action and using the Id property from your Create file action as input for File Identifier:
     
     
    Sending the email
    Finally, we can add the Excel file as attachment to an email. In the Attachments input, set the Attachments Content as the File Content property from your Get file content 2 (the latest one, from where we pulled the populated template data), as marked in red. Also, make sure to name the attachment, including its extension (highlighted in blue):
     
     
    Testing the flow
    After running the flow, an email was received with the report as attachment:
     
     
     
    When accessing the attachment, we find the same content from the populated template, as expected (without the annoying empty row):
     
    Let me know if it works for you or if you need any additional help!
    -------------------------------------------------------------------------
    If this is the answer for your question, please mark the post as Solved.
    If this answer helps you in any way, please give it a like.
     
     
     
  • RodrigoRLF Profile Picture
    8 on at
    @Rzaneti, thanks! That totally works, im struggling with another problem now, when I have a few rows it runs perfectly fine, but i've tested it with 1k+ rows and it took around 36min, more in some cases. I saw some people talking about using graph api but it seems a lot of workaround
  • rzaneti Profile Picture
    4,241 Super User 2025 Season 2 on at
    Hi @RodrigoRLF,
     
    I'm glad it worked! 
     
    About Graph API, you can use it to add one or multiple row into a table (see the docs here), and it absolutely will be much quicker than having multiple Add row into a table actions within a loop. However, you will need a Premium license for making an API request, since it's a premium connector.
     
    Additionally, the authentication process can be tricky: you would need to use your individual account token, which needs to be updated every day or every week, or you need to set up an App Registration in Azure, which can be a little complex. This second option is the best approach, and there are some online articles/videos on how to set it up, like this one
  • Suggested answer
    takolota1 Profile Picture
    4,974 Moderator on at
    This template converts a CSV to a new Excel file with a filled in table. Really fast & premium connectors.
    https://community.powerplatform.com/galleries/gallery-posts/?postid=d14f3b67-c4a9-4579-80f5-0e1dac0d80ee

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 522 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 364 Moderator

#3
abm abm Profile Picture

abm abm 243 Most Valuable Professional

Last 30 days Overall leaderboard