Handle commas in CSV files in Power Automate

In the last week I’ve been asked 3 times how to handle commas in csv files.

Reading CSV files

A while back I wrote a post about how to read CSV Files in Power Automate. Although this post handles some of the issues around reading CSV, It didn’t cover handling commas your csv.

Example CSV

In this post I will use the following example CSV

Name,Description,Amount
First item,This is the test of the first item,1
Second Item,"The second, and last item",2

So I’ve got one header line. Then I’ve got a simple line with some data followed by a more complicated line where my description contains a comma.

Note that the csv will have a double quote around the data with a comma. If only every field could have double quotes around all data then that would be great.

In my case i want to replace the commas that separate the fields with 3 hashes (###). But I don’t want to replace the comma in my data.

Creating a flow

As so often I’m going to create a manually started flow. in this flow I will initialize 3 variables. In general i try to avoid variables as much as possible and use compose steps instead, but there will be too loops and conditions for that to work this time.

With these 3 variables I will control the manipulation of the csv file.

Reading the CSV data

Now I’ve got 3 steps to get my CSV data and turning it into an array of lines.

For the CSV content compose action i’m using the following expression:

base64ToString(
                body('Get_file_content')?['$content']
              )

for the CSV Lines split, I’m using the following expression to split by the new lines.

split(outputs('CSV_content'),'
')

Note that I added a new line in the middle of my expression!

When I run this flow I’ve now got the following array of CSV lines.

Constructing new CSV lines

In the next step I will process the CSV lines in an Apply to each step. this Apply to each will take the output from the earlier CSV Lines compose action and take it as its input.

Then for each line we want to split the data.

The expression used to split the fields is

split(items('Apply_to_each'),',')

All quite simple so far. But what does the result look like? Especially that second item in our CSV will now not look right.

[
  "Second Item",
  "\"The second",
  " and last item\"",
  "2"
]

there are a couple of different approaches, but in my approach in this post I will go for standard flow options available. I could have called and Azure Function to do all the work, but I want to stick to standard Power Automate actions today.

The general process is shown below.

The New CSV line is set to an empty value using the concat function ( concat(”) will do this).

Then Inside the Apply to each 2 will will set the New CSV line to the required ### separated text like this:

Second Item,"The second, and last item",2

Then we will use a compose action to get each of the lines. So that we can use the Pieter’s method to merge all of the lines.

Inside the Apply to each 2

Inside the Apply to each 2 we will need to check if a field of data starsts with a double quote (“). If it does then we have found data that has commas and my csv editor (e.g. Excel) decided that the double quotes were needed.

First I created a compose action which gets the first character

We can now use this in a condition step. when the data starts with a double quote we will simply store the text in the merge Text variable. Nothing to complicated.

If we find data without a double quote then there are two options. Either we have found data to merge before or we haven’t.

If we have found a field starting with a quotes before then we set the merged text variable to merge the values found. If we haven’t found quotes before then we simply set the merged text to the value of the field.

The code for the nothing to merge is as follows:

When I need to merge two text together I use the following 4 steps:

So now we have a New CSV Line that looks like the has separated line that I mentioned before.

The only thing left to do is now to set s compose with the content of the variable so t that a second compose can collect all the lines.

This final Compose will now hold the following data:

This solves my problem.

I could still remove the initial 3 hashes, but that would clutter the post rather than anything else. They aren’t sitting in my way for now.

Other Power Automate posts?

Have a look at the Power Automate User Guide with many other posts.

Share
Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

View Comments

  • you don’t show the expression for the sub string you use to merge the two texts together. Can you please show

  • I too am a little confused, specifically at the formula being used for the second item in "merge text with current text", if you're using a substring formula don't you have to define start position and character length? doesn't this change with the data? wouldn't it be better to just use current item? Can you show that specific formula?

    • Hi Brett,

      The substring expression is as follows:

      substring(items('Apply_to_each_2'),0, sub(length(items('Apply_to_each_2')),1))

      So even though the content length may change with the length function it is possible to calculate the length of the specific text.

  • Pieter, Thanks for the quick reply, I have a suggestion that probably would take more expertise than I have. I noticed that your solution is good if were dealing with a cell with only a single comma in the .CSV. What about something that can deal with multiple commas in a single cell, like "1,000,000" for instance, or multiple sentences containing commas. I changed the delimiter to a pipe for testing purposes.

    Input- This is a Test, This is, a Test.
    output - |\"This is a Test, This is| a Test.\"|

    The startsWith condition could remain, but some where we would need to do an endsWith, then merge everything in between, I assume. (haven't figured this part out yet)

    This might create something that is very slow to run. I have no idea why Microsoft does not support CSV processing with the Excel connector. It would save a ton of hassle.

Recent Posts

Create new group alert using Power Automate

Do you want to know when a new group has been created with for example…

3 days ago

Delegation Warnings in Power Apps

We have all seen them, Delegation Warnings, but what is delegation and how do you…

3 days ago

1 Top tip: PowerPlatformLearn.com and master the Power Platform within no time

It can be difficult to get started with the Power Platform, blogs help, training helps,…

1 week ago

Compare 2 Excel files using Power Automate

This is such a common scenario. You receive Excel files on a regular basis and…

2 weeks ago

Rename SharePoint folders using Power Automate in 2 easy steps

Have you ever wanted to rename SharePoint folders with Power Automate? REST API call to…

3 weeks ago

Read dates from Excel with Power Automate

To read dates from Excel files can be tricky, as Excel only give you the…

4 weeks ago
%%footer%%