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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Export a collection da...
Power Apps
Answered

Export a collection data to CSV in the column order of the collection

(0) ShareShare
ReportReport
Posted on by 565
Hi there!
 
I have several galleries to export to CSV.
So, for example, I put a button with this code:
 
_________________________________________________________________________________________
ClearCollect(colDataTempDestination , SortByColumns(Inventory_Destinations,"cr815_name"));
 
ForAll(colDataTempDestination, Collect(ColDataExport,
{
Destination:ThisRecord.Name,
Code: ThisRecord.Code,
Address: ThisRecord.Address,
City:  ThisRecord.City,
State: ThisRecord.State,
ZIP:  ThisRecord.ZipCode,
Country:  ThisRecord.Country.Name,
Status_Active: ThisRecord.Active
}));
 
Set(varJSONData, JSON(ColDataExport));
Set(VarLinkToFile, ExportDataToCSV.Run(varJSONData,"Destinations_" & Now() & ".csv").linktocsvfile);
 
Launch(VarLinkToFile);
 
Clear(ColDataExport)
_________________________________________________________________________________________
 
So, the code creates the collection and send this data to a Flow that receives the data, create a CSV Table and save it in OneDrive as CSV file.
 
 
Yes, I choosed Automatic for Columns... So the CSV file created, sorts the columns names in alphabetical order, instead the order I made in the collection:
 
Destination
Code
Address
City
State
ZIP
Country
Status_Active
 
And I want to use the same flow for all the exportations.
 
Is there any way to achive that? Or I need to create a flow for each gallery and format the columns in each of them?
 
Thanks for any help!
Categories:
I have the same question (0)
  • EmilioRoqueta69 Profile Picture
    565 on at
    I realized that the collection shows the columns in alphabetical order...
  • Suggested answer
    WarrenBelz Profile Picture
    154,799 Most Valuable Professional on at
    If you want to put CSV columns in your desired order, you need to go back to the pre-JSON process as below
    With(
       {
          _Data:
          SortByColumns(
             Inventory_Destinations,
             "cr815_name"
          )
       },
       With(
          {
             _Export: 
             "Destination,Code,Address,City,State,ZIP,Country,Status_Active" & Char(10) & 
             Concat(
                _Data,
                Name & "," & Code & "," & Address & "," & City & "," & State & "," & ZipCode & "," & Country.Name & "," & Active,
                Char(10)
             )
          },
          Launch(
             ExportDataToCSV.Run(
                _Export,
                "Destinations_" & Now() & ".csv"
          ).linktocsvfile
       )
    );
    You also need to remove the JSON element from the Flow and simply create the file with the valid CSV now sent from Power Apps.
     
    This structure also eliminates a number of "one use" Collections and Variables "hanging around" in your app.
     
    Please ✅ Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like♥.
    Visit my blog Practical Power Apps    LinkedIn   
  • EmilioRoqueta69 Profile Picture
    565 on at
     
    Many thanks for your help.
     
    I tell you, If I sent the CSV created with your formula, and removing the json in the Create CSV Table, I get this error:
     
     
    So, I added a step before , a Compose with:
     
    split(triggerBody()['text'], ',')
     
     
    And now, I get this error message:
     
     
    The result of the Compose is this:
     
     
    [
      "Destination",
      "Code",
      "Address",
      "City",
      "State",
      "ZIP",
      "Country",
      "ActiveLanzarote",
      "ACE",
      "C/Reina Sofia 17",
      " Local 4",
      "Puerto del Carmen",
      "Lanzarote",
      "35510",
      "SPAIN",
      "YesADB - Kusadasi",
      "ADB",
      "Mahallesi Kirazlı Yolu Caddesi A Blok No:3AA",
      "Aydin",
      "Aydin",
      "09400",
      "TURKEY",
      "YesAGA - Agadir",
      "AGA",
      "Rue Mohamed Iqbal Amsernat",
      "Agadir",
      "Agadir",
      "8000",
      "MOROCCO",
      "YesAGP - Malága",
      "AGP",
      "Avda. Antonio Machado",
      " 28",
      " Edificio Maryola 1",
      " Planta 1A",
      " 29630",
      " Benalmadena",
      "Malága",
      "Andalucía",
      "29630",
      "SPAIN",
      "YesALC - Alicante"
    ]
     
    I do not know, but if you take a look, the last column Active is not separated from the first column of the record.
     
    "YesAGA - Agadir",
    "YesAGP - Malága",
     
    I must to be:
     
    "Yes",
    AGA - Agadir",
    "Yes",
    "AGP - Malága",
     
    If I added a comma after the Active column:
     
     
       With(
          {
             _Export:
             "Destination,Code,Address,City,State,ZIP,Country,Active," & Char(10) &
             Concat(
                _Data,
                Name & "," & Code & "," & Address & "," & City & "," & State & "," & ZipCode & "," & Country.Name & "," & Active & ","
             )
          },
     
    I get the Active column separately:
     
    [
      "Destination",
      "Code",
      "Address",
      "City",
      "State",
      "ZIP",
      "Country",
      "Active",
      "Lanzarote",
      "ACE",
      "C/Reina Sofia 17",
      " Local 4",
      "Puerto del Carmen",
      "Lanzarote",
      "35510",
      "SPAIN",
      "Yes",
      "ADB - Kusadasi",
     
    But I get the same error on creating the CSV table:
     
    The property 'columns' must be specified unless the 'from' property value is an array of objects.
     
    Thanks for all your help!
  • Verified answer
    WarrenBelz Profile Picture
    154,799 Most Valuable Professional on at
    You also need to remove Create CSV Table - the output from Power Apps is already one of these.  Simply create the CSV file in OneDrive directly with the output received from Power Apps.
    Also note the small addition to my original post - I omitted the Line Return Char(10) from the end of the Concat function
     
    With(
       {
          _Export:
          "Destination,Code,Address,City,State,ZIP,Country,Active" & Char(10) &
          Concat(
             _Data,   
             Name & "," & Code & "," & Address & "," & City & "," & State & "," & ZipCode & "," & 
             Country.Name & "," & Active,
             Char(10)
          )
       },
     
    You also had extra commas at the end of both lines (header and rows). I can assure you this works - I use it on a number of prod apps.
     
    Please ✅ Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like♥.
    Visit my blog Practical Power Apps    LinkedIn   
  • EmilioRoqueta69 Profile Picture
    565 on at
    Thank you so much @WarrenBez ! 

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 530

#2
WarrenBelz Profile Picture

WarrenBelz 459 Most Valuable Professional

#3
Haque Profile Picture

Haque 314

Last 30 days Overall leaderboard