Hello All,
I ran into some issues when pulling CSV data in from Power Automate Desktop because of commas in the actual data. I wanted to change the delimiter so I could more easily parse the data in a single Select action without commas in the actual data messing things up. I also may be parsing CSV files with hundreds or thousands of rows, so I didn’t want to use all my daily actions on this in a slow Apply to each loop.
Attached is the scope/flow I built so anyone can easily select their CSV data that has quotes around the comma-containing records, enter a new delimiter, and get the new delimiter separated data from the final compose action without the usual errors. And it only takes a few actions to do this, even on very large files.
I've found that many CSV files don't put quotes around their records with in-data commas, and this only works when there are quotes around those records. But if the file is saved as a text file, then it often puts quotes around the right records.
If you are using Power Automate Desktop, program the file to be saved as .txt and read that into your output variable.
It’s currently set to handle up to 50 comma-containing columns, but you can expand that to as many columns as needed by adding extra lines & expressions to the 1st Select action. Just follow the pattern by replacing some of the array numbers, like [50] with [51].
Also if your data has more unique values like an array with mixed quoted string data, Ex: ["string1", 2, 03/05/2022, "string3"], then this will create errors in the output.
The template for parsing CSV to JSON & entering it into a dataset uses the same change delimiter set-up: https://powerusers.microsoft.com/t5/Power-Automate-Cookbook/CSV-to-Dataset/m-p/1508191#M584
*Copying the template scope into another flow may not work as it may mess up the expressions. You may need to start with a copy of this template and copy & paste the rest of your flow into this template flow.
Thanks for any feedback, & please subscribe to my YouTube channel (https://youtube.com/@tylerkolota?si=uEGKko1U8D29CJ86)
Version 3 Uploaded 03/26/2022 (Adjusted the 1st Select input so it can now also deal with in-data commas in the 1st column. Added more lines to the 1st Select so it can now handle up to 50 columns with commas in them.)
Google Drive Link: https://drive.google.com/file/d/11uT15hXY0VjnOKDvFxdVgkuMtXqTmA0c/view?usp=sharing
Version 4 Uploaded 04/09/2022
(More minor fixes & additions.
I adjusted several expressions so it can now handle a few more scenarios with arrays in the CSV data. It should handle any array that doesn't include double quotes and any array that is all strings with double quotes, so ["String1", "String2", "String3"], but it will have issues if it is a mixed array with some double-quoted strings and some other values, for example ["String", 4, 03/05/2022, "String2"] won't work.
I also adjusted how the LineBreak setting is set-up so it now uses the /r/n for the LineBreak. I also provided this link in the flow so anyone can look up the right string for the decodeUriComponent expression(s) if they happen to have different LineBreak characters. This change also made it possible to differentiate between in-data line-breaks and CSV row line-breaks on the files I tested, so it should now replace the in-data line-breaks, like the multiple-choice fields some sites use, with semi-colons. That should make those records much easier to deal with & parse in later actions.
I also looked over a problem with in-data trailing commas. I added a line in the settings where anyone can toggle whether they want it to adjust for trailing OR leading commas in the data, it just can't handle both in one dataset. So if one column in one row has ",String1 String2" and another column in another row has "String 3 String4," then it will have errors.)
Google Drive Link: https://drive.google.com/file/d/1ZbhFGVKHSpaH2Duv8qXwMnNww8czdgc4/view?usp=sharing
Version 5
More adjustments for comma edge cases and for instances with one comma-containing value following another in the file data.
Google Drive Link: https://drive.google.com/file/d/1il_wI9fJRk11YaI4EPQvk2efrbBNRBwr/view?usp=sharing
Update 06/01/2022
Microsoft Power Platform & Paul Murana recently did a video demonstration of how to handle CSV & other files in dataflows: https://youtu.be/8IvHxRnwJ7Q
But it currently only outputs to a dataverse or Dataverse for teams table.
I spent way too much time learning that I had to remove one of the LineBreak components because my csv only had \n (I probably should have started with a simple csv, but that would have been too easy and I went straight to a complex one), but my gosh am I happy happy happy that I got this working. Thanks so much for your amazing flow.
Thanks for the wonderful solution. If possible, can you please provide the expression for the Select CSV to JSON action for the Version 5 flow
Thanks in Advance
Tarun.
Thanks for the wonderful solution. If possible, can you please provide the expression( From and Map )for the for the Select CSV to JSON action for the Version 5 flow.
I tried the below, but not working
From :
To:
Thanks in Advance
Tarun.
@TarunEY
This is the expression for selecting the correct lines of the CSV data to put in an array for the Select action & omitting the number of headers and/or footers the user specifies in the set-up action.
take(skip(split(outputs('Compose_Reformat_back_to_CSV_with_new_delimiter'), outputs('Compose_CSV_data_+_Set-up')['LineBreak']), outputs('Compose_CSV_data_+_Set-up')['HeaderRowsToSkip']), sub(length(skip(split(outputs('Compose_Reformat_back_to_CSV_with_new_delimiter'), outputs('Compose_CSV_data_+_Set-up')['LineBreak']), outputs('Compose_CSV_data_+_Set-up')['HeaderRowsToSkip'])), outputs('Compose_CSV_data_+_Set-up')['FooterRowsToSkip']))
Please clarify, what are you trying to do and why are you trying to do it?
Thanks,
New template for people trying to take in most CSV data & output a new Excel table
The unique part of this is it automatically maps the header row onto the column headers. So if you want a process that can take in several different CSV tables & post them as Excel tables somewhere, then this is a great start.
https://powerusers.microsoft.com/t5/Power-Automate-Cookbook/CSV-To-New-Excel-Table/td-p/1826096
The only catch is the header row has to be the same row in each CSV. For example, the 1st row in the file.
Hello @takolota ,
When I put the delimiter " ; " my decimal number column lost " . ".
Do you maybe know how to prevent that?
All the best,
Mario
Sorry I don’t know anything in the program that would remove a “ . “ from using a different delimiter
Hi @takolota ,
All fine, I found a solution. It is important that in the column where you have a value change "." into ",", and then in the Compose action change the delimiter to ";".
All the best,
Mario
Thanks @takolota, I tried your solution and works a treat. Easy to implement, only in 4 steps and a few seconds executes. Thanks again
Using the script, and specifying the file in a folder on my OneDrive for Business works fine.
But I'm getting stuck trying to add an "apply to each" so that I can process every file in that OneDrive Folder.
I've added List files in folder (OneDrive for Business) -> Apply to Each specify value-> Get File Content (ID) -> Then the Scope Change CSV delimiter...
But as soon as I add Scope Change CSV delimiter, Flow Checker immediately throws errors.
Any thoughts how to make this work?