Ray of light

Shed some light on arrays or collections in Power Automate

Are you struggling with arrays in Power Automate? This post will help you master arrays.

Power Automate arrays and collection

Today I was asked about how arrays (aka collections) work in Microsoft Flow. In this post I will shed some light on arrays and many of the common scenarios that you may face with arrays in Power Automate.

I will start with creating arrays, then I will look at manipulating arrays, but first of all what are arrays?

In most common programming languages you will recognize the term array as a block of related data elements. You could see this as a table or a list list alike data structure.

Creating arrays

I’m going to start with the following table of data.

BrandColorAgeRegistration Date
NissanRed301/07/2015
CitroenGreen221/09/2016
AudiBlue401/01/2014

I want to create an array like the above table.

In flow I could now create an array using the createArray function.

createArray('Nissan','Citroen','Audi')
Compose action with an array or collection of car brands in Power Automate

I could even take this further and create arrays in arrays.

createArray(createArray('Nissan','Red',3,'01/07/2015'),createArray('Ctiroen','Green',2,'21/09/2016'),createArray('Audi','Blue',4,'01/07/2014'))

Nested Arrays in Json

And I will get an array of arrays

[
  [
    "Nissan",
    "Red",
    3,
    "01/07/2015"
  ],
  [
    "Citroen",
    "Green",
    2,
    "21/09/2016"
  ],
  [
    "Audi",
    "Blue",
    4,
    "01/07/2014"
  ]
]

Now that we know how to create arrays, it is time to do something with all these arrays.

Where do I find arrays in flows?

Within Flow you can find arrays everywhere. The most common place and most visible place is within the Apply to each step. In this step flow will take an array and step through the elements in the array. So If we take a further look at the previous example then a Compose delivering an array can split by an Apply to each step:

Apply to each when stepping through arrays in Power Automate

As Power Automate is running through the Apply to each you will find the separate elements of my array.

Each element in collections looked at in Apply to each

So far this post is all about creating arrays and then stepping through arrays. Now that we have some basic understanding of arrays in Flow it is time to get some real arrays. For this I’m going to create a list in SharePoint with the same data as in the above table.

SharePoint list

Using the Get Items action from the SharePoint connector I can now read the data from this list.

Get Items from a SharePoint list

Looking at the Body of the Get Items action you will see some json as shown below:

{
  "value": [
    {
      "@odata.etag": "\"1\"",
      "ItemInternalId": "1",
      "ID": 1,
      "Title": "Nissan",
      "Colour": {
        "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
        "Id": 0,
        "Value": "Red"
      },
      "Colour#Id": 0,
      "Age": 3,
      "Registration_x0020_Date": "2015-01-07",
      "Modified": "2018-07-10T08:36:27Z",
      "Created": "2018-07-10T08:36:27Z",
      "Author": {
      "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
      "Claims": "i:0#.f|membership|pieter@pieterveenstradev.onmicrosoft.com",
      "DisplayName": "Pieter Veenstra",
      "Email": "pieter@PieterVeenstraMVP.onmicrosoft.com",
      "Picture": "https:/ /pieterveenstraMVP.sharepoint.com/_layouts/15/UserPhoto.aspx?Size=L&AccountName=pieter@PieterVeenstraMVP.onmicrosoft.com",
      "Department": null,
      "JobTitle": null
    },
    "Author#Claims": "i:0#.f|membership|pieter@pieterveenstradev.onmicrosoft.com",
    "Editor": {
       "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
       "Claims": "i:0#.f|membership|pieter@pieterveenstraMVP.onmicrosoft.com",
       "DisplayName": "Pieter Veenstra",
       "Email": "pieter@PieterVeenstraMVP.onmicrosoft.com",
       "Picture": "https:/ /pieterveenstraMVP.sharepoint.com/_layouts/15/UserPhoto.aspx?Size=L&AccountName=pieter@PieterVeenstraMVP.onmicrosoft.com",
       "Department": null,
       "JobTitle": null
    },
    "Editor#Claims": "i:0#.f|membership|pieter@pieterveenstraMVP.onmicrosoft.com",
    "{Identifier}": "Lists%252fCars%252f1_.000",
    "{Link}": "https://pieterveenstradev.sharepoint.com/_layouts/15/listform.aspx?PageType=4&ListId=51c5fad6-6c45-4bc4-8632-556674abe0be&ID=1&ContentTypeID=0x01004819843C002BE14D97CADA4CCA608281",
    "{Name}": "Nissan",
    "{FilenameWithExtension}": "Nissan",
    "{Path}": "Lists/Cars/",
    "{HasAttachments}": false
  },
  {
     "@odata.etag": "\"1\"",
     "ItemInternalId": "2",
     "ID": 2,
     "Title": "Citroen",
     "Colour": {
     "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
     "Id": 2,
     "Value": "Green"
   },
   "Colour#Id": 2,
   "Age": 2,
   "Registration_x0020_Date": "2016-09-23",
   "Modified": "2018-07-10T08:37:04Z",
   "Created": "2018-07-10T08:37:04Z",
   "Author": {
   "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
   "Claims": "i:0#.f|membership|pieter@pieterveenstraMVP.onmicrosoft.com",
   "DisplayName": "Pieter Veenstra",
   "Email": "pieter@PieterVeenstraMVP.onmicrosoft.com",
   "Picture": "https://pieterveenstraMVP.sharepoint.com/_layouts/15/UserPhoto.aspx?Size=L&AccountName=pieter@PieterVeenstraMVP.onmicrosoft.com",
   "Department": null,
   "JobTitle": null
 },
   "Author#Claims": "i:0#.f|membership|pieter@pieterveenstraMVP.onmicrosoft.com",
   "Editor": {
     "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
     "Claims": "i:0#.f|membership|pieter@pieterveenstraMVP.onmicrosoft.com",
     "DisplayName": "Pieter Veenstra",
     "Email": "pieter@PieterVeenstraMVP.onmicrosoft.com",
     "Picture": "https://pieterveenstraMVP.sharepoint.com/_layouts/15/UserPhoto.aspx?Size=L&AccountName=pieter@PieterVeenstraMVP.onmicrosoft.com",
     "Department": null,
     "JobTitle": null
   },
   "Editor#Claims": "i:0#.f|membership|pieter@pieterveenstraMVP.onmicrosoft.com",
   "{Identifier}": "Lists%252fCars%252f2_.000",
   "{Link}": "https:/ /pieterveenstradev.sharepoint.com/_layouts/15/listform.aspx?PageType=4&ListId=51c5fad6-6c45-4bc4-8632-556674abe0be&ID=2&ContentTypeID=0x01004819843C002BE14D97CADA4CCA608281",
   "{Name}": "Citroen",
   "{FilenameWithExtension}": "Citroen",
   "{Path}": "Lists/Cars/",
   "{HasAttachments}": false
},
{
"@odata.etag": "\"1\"",
"ItemInternalId": "3",
"ID": 3,
"Title": "Audi",
"Colour": {
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
"Id": 1,
"Value": "Blue"
},
"Colour#Id": 1,
"Age": 4,
"Registration_x0020_Date": "2014-01-01",
"Modified": "2018-07-10T08:37:42Z",
"Created": "2018-07-10T08:37:42Z",
"Author": {
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
"Claims": "i:0#.f|membership|pieter@pieterveenstradev.onmicrosoft.com",
"DisplayName": "Pieter Veenstra",
"Email": "pieter@PieterVeenstraMVP.onmicrosoft.com",
"Picture": "https://pieterveenstraMVP.sharepoint.com/_layouts/15/UserPhoto.aspx?Size=L&AccountName=pieter@PieterVeenstraDev.onmicrosoft.com",
"Department": null,
"JobTitle": null
},
      "Author#Claims": "i:0#.f|membership|pieter@pieterveenstraMVP.onmicrosoft.com",
      "Editor": {
        "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
        "Claims": "i:0#.f|membership|pieter@pieterveenstraMVP.onmicrosoft.com",
        "DisplayName": "Pieter Veenstra",
        "Email": "pieter@PieterVeenstraMVP.onmicrosoft.com",
        "Picture": "https://pieterveenstraMVP.sharepoint.com/_layouts/15/UserPhoto.aspx?Size=L&AccountName=pieter@PieterVeenstraDev.onmicrosoft.com",
        "Department": null,
        "JobTitle": null
      },
      "Editor#Claims": "i:0#.f|membership|pieter@pieterveenstraMVP.onmicrosoft.com",
      "{Identifier}": "Lists%252fCars%252f3_.000",
      "{Link}": "https://pieterveenstraMVP.sharepoint.com/_layouts/15/listform.aspx?PageType=4&ListId=51c5fad6-6c45-4bc4-8632-556674abe0be&ID=3&ContentTypeID=0x01004819843C002BE14D97CADA4CCA608281",
      "{Name}": "Audi",
      "{FilenameWithExtension}": "Audi",
      "{Path}": "Lists/Cars/",
      "{HasAttachments}": false
    }
  ]
}

This is where the data in Power Automate can become difficult to understand. The above JSON might need a second look before you really understand what data can be found. json can be difficult to understand, especially when a lot of json is thrown at you. Flow is making your life easy here! Use dynamic content and often you can forget about the json, you simply get your SharePoint list columns back. It even helps you with the complexities around lookups, dates and other column types.

Dynamic content with Current Item

Array functions

Time for a step into the deep end. There is a lot more we can do with arrays. I’m going to start with the arrays functions available within Microsoft Flow. You will notice that in the referenced article the term Collections is also used.

Collection functionTask
containsCheck whether a collection has a specific item.
emptyCheck whether a collection is empty.
firstReturn the first item from a collection.
intersectionReturn a collection that has only the common items across the specified collections.
joinReturn a string that has all the items from an array, separated by the specified character.
lastReturn the last item from a collection.
lengthReturn the number of items in a string or array.
skipRemove items from the front of a collection, and return all the other items.
takeReturn items from the front of a collection.
unionReturn a collection that has all the items from the specified collections.

Hey, I want to do more! Where is the sort function?

Well remember when we got the data from my SharePoint list? In the Get Items action you can sort your data. So you might want to sort your data when you collect your data.

Get Items advanced options

Also in the Data operations connector there is no sort:

Data Operations in flow

If you need to sort your array however, please have a look at Sort an array or collection in Power Automate

The examples of functions on Arrays

In this section I will look at operations you can do on arrays such as join, select and filter.

Contains

The contains functions checks your array for any values or elements in your collection or array.

Looking at the following 3 examples. The first 2 return false where the last one returns true.

contains(body('Get_items')?['Value'],'Nissan')

contains(outputs('Compose'),'Nissan')

contains(first(outputs('Compose')),'Nissan')

Within the last example I selected a single record from my array before checking with the contains function for my car brand. Where in the first two examples I’m supplying an array of array to the contains function.

Empty

Empty checks the length of the Collection if no items are found in the array then true is returned. Note that this is not the same as comparing an array to a null value.

First item in collections

The First function returns the first item from the collection. See the 3rd example from the contains section above for a real example.

You could also user the construction array[0] to get to the first item, however when the array isn’t defined you will find that your flows will error while first returns a null value.

Intersection

Imagine that you have two arrays and you want to get the items that exist in both collection. and Now you want to find the items that exist in both collections.

So you could for example have two lists with cars. One list is called my cars and one is called insured cars if you now want to find out which of your cars are insured you could use the intersection function.

intersection(body('Get_items')?['Value'],body('Get_items_2')?['Value'])

Join collections

When you have an array of strings and you would like to create a character separated list of strings then you could do this with the join function. Have you ever has a list of users in SharePoint and you wanted to email them all?

join(outputs('Compose'),';')

Last item in collections

The last function is similar the the first function, other than that you get the last element in the array rather than the first one.

Length of collections

The length function gives you the number of elements in a collection. Nothing exciting here.

Skip

With the skip function you can select all elements after a certain position.

skip(outputs('Compose'),2)

Take

The take function is similar to the skip function but now you can collect all the elements up to a certain point in your array.

take(outputs('Compose'),2)

Union

In my earlier examples I had two arrays in my json. One that I manually created and and one that I get from the Get Items action. To merge these arrays into one array you could run the following:

union(body('Get_items')?['Value'],outputs('Compose'))

Did you notice that Flow really doesn’t care that these two arrays are not exactly the same. As Flow will just consider these two arrays to be JSON. It is just gluing the two collections of data together. this is where a sort might be important, although most of the times you will find that you will just want to process each element in your collection and the order really doesn’t matter.

Select

The select action I also want to include here. Although Select is not a function within Power Automate it can help you transform an array of elements.

Select action in flow

Taking an input array and transforming the arrays using a select often helps improve the processing of arrays.

Filter arrays

If you want to filter your array or collection data in Power Automate then please have a look at my filter data in arrays using the select action posts. I that post I’m giving more details on filtering data using the select.

Often it can be wise to filter an array first before pushing it through an apply to each. For performance reasons this is definitely worth a look. It is quite easy to gain multiple minute in the run of a flow.

42 thoughts on “Shed some light on arrays or collections in Power Automate

  1. Thank you so much for this page. I keep returning to it. Just one question though. In your examples you read a SP list and turn it into an array. So your array has field names, for example when you use Select you can search for ‘ID’. But when you create an array using createarray how can you reference a field in the Select?
    Thanks!
    George Mu’ammar

    1. Hi George,

      You can select the items in an array in two different ways:

      SomeArrayReturningFunction()?[‘FieldName’]

      or

      SomeArrayReturningFunction()?[0] or SomeArrayReturningFunction()?[1]

      The first option means that you need to name the attributes.
      [
      {
      ‘Name’:’Mr X’,
      ‘Age’: 35
      },
      {
      ‘Name’:’Mrs Y’,
      ‘Age’: 25
      }
      ]

      So in the above example you could select the Name of the second person

      variables(‘myArray’)[1]?[‘Name’]

      Does this help?

      1. Of course! Perfectly logical if I had thought for a moment 🙂 thanks for your reply and for your necessary blogs. Microsoft Flow would be hardly usable if we had to depend on Microsoft for documentation

    1. Hi Priyanka,

      For all connectors ( including the Trello one) you will find that json is used to move data around. When you start with building flows it is important to understand the json structures.

      You might also find this post helpful:
      https://sharepains.com/2019/09/16/query-json-in-microsoft-flow/

      Are you able to share the output that you are getting from the Trello actions? Once you have identified the data that you are after in there then querying the data should be quite easy with a compose action.

  2. Hi,
    Could you please let me know how we can dynamically combine arrays which are like this into one array list.

    [{“Dato “:”12.07.2020”,”Navn “:”Namma\n Employee 3″,”Jubilæum”:10,”Titel”:””,”Økonomisk kontorsted “:””,”Arbejdssted “:” “,”Privatadresse”:””,”Nærmeste leder “:null}]

    [{“Dato “:”12.07.2020”,”Navn “:”Test\n Employee 4″,”Jubilæum”:10,”Titel”:”Test->Senior Partner”,”Økonomisk kontorsted “:”TEST”,”Arbejdssted “:” “,”Privatadresse”:””,”Nærmeste leder “:null}]

    [{“Dato “:”12.07.2020”,”Navn “:”James\n Doe”,”Jubilæum”:10,”Titel”:”Trainee”,”Økonomisk kontorsted “:”TEST”,”Arbejdssted “:”Kystvejen 29 8000 Aarhus C Denmark”,”Privatadresse”:”Testvej 123 8000 Aarhus C DK”,”Nærmeste leder “:”ner”}]

    [{“Dato “:”12.07.2020”,”Navn “:”Test\n Employee 6″,”Jubilæum”:10,”Titel”:”Trainee”,”Økonomisk kontorsted “:”TEST”,”Arbejdssted “:” “,”Privatadresse”:”Testvej 123 8000 Aarhus C DK”,”Nærmeste leder “:null}]

    1. Hi Raj,

      You could consider converting the json objects into text then remove the [ and the ].

      then concatenate the texts.

      then add the [ and the ] again and convert it back to a json object with the json function.

      Does that help?

      1. Hi,
        That is not working well. it was supposed to work fine, my main issue was to actually append tables

        since the main data was in array i am converting the array to string and appending the array. that is not working fine,
        so tried to create the tabular format and append it to array.

        that is multiplying the tables instead of combining all the data into one table.

      2. Just so that I understand it correctly you have 4 array variable of 1 item each. will you know how many arrays you have? I.e. you always have 4?
        And every array has always got 1 item in it?

      3. it is dynamic . so we will not be knowing whether it is 1 or 2
        could you please let me know if there is any formula to append [ in the first letter and ] at the last .

      4. That worked well . i am able to consolidate all the data by first using select in the for loop and then setting up a variable to store all the json data at the end of the for loops and editing the json data to clear ][ brackets and sending the data to create table and triggering an email .

        that sent all the information of the workers dynamically.

  3. HI Pieter,

    As others have said a really helpful article. Can I ask for help? I have the following JSON returned from a graph query (reading an .xlsm file hence graph being used!!):

    {
    “@odata.context”: “https://graph.microsoft.com/v1.0/$mxxxxxxxxxxx’)/rows”,
    “value”: [
    {
    “@odata.id”: “/drives(‘b%21mxxxxxxxxxx)/rows/itemAt(index=0)”,
    “index”: 0,
    “values”: [
    [
    “Storage HW”,
    3649800,
    375980,
    455928,
    501520.8,
    551672.88,
    5534901.68
    ]
    ]
    },

    After parsing the JSON I can’t select a specific item in the “values array”! i can get the whole “values” array back for a specific “value” index, but really want to get each value so I can align each to a variable. So far the only way I have found is to convert to a string and then use split recursively to get each of the 7 values! Not super efficient, hence asking you as I’m sure I’m just being a twit with my expression!

    Thanks in advance.

    1. Hi Jonathan,

      The values section is an array in an array.

      you can select the following bit of data with the first() function.

      [
      “Storage HW”,
      3649800,
      375980,
      455928,
      501520.8,
      551672.88,
      5534901.68
      ]

      Then to select the items you can use [0] or [1] the select the 1st or 2nd item.

      You could even consider pushing the values array into a select action. Then you can map those values and give each value a name. That makes querying them just that little bit more friendly.

  4. Hi Pieter

    Thanks for getting back so quickly. What you said in your comment was what I though I would be able to do, however I just can’t get it work! (apologies for the long post coming!).

    From the parsed JSON I did a compose to get the first “values:” item which gave me:

    {
    “@odata.id”: “/drives(‘b%21m9wlSvyeUUeZpdshk2qUt50nqlCqVkdMt7WDTh4Ejtiv-Tfa-uNCSqOCCxyi4otz’)/items(‘016HPV57IUWMU57UH3PFEJPQ763ICUZGWH’)/workbook/tables(%27%7B9E25A436-A9A2-46D0-8BD6-91A01420BB71%7D%27)/rows/itemAt(index=1)”,
    “index”: 1,
    “values”: [
    [
    “Storage HW Mnt”,
    1843160,
    152732.8,
    141250.56,
    103583.744,
    56971.0592,
    2297698.1632
    ]
    ]
    }

    I then set a second compose action to just pull the first item within “values:” i.e. “Storage HW Mnt”. I used the following expression:

    first(outputs(‘Compose_2’))

    But got the error:

    InvalidTemplate. Unable to process template language expressions in action ‘Compose’ inputs at line ‘1’ and column ‘2267’: ‘The template language function ‘first’ expects its parameter be an array or a string. The provided value is of type ‘Object’. Please see https://aka.ms/logicexpressions#first for usage details.’.

    When I tried to be specific and did

    outputs(‘Compose_2’)?[‘values’][0]

    I just get:

    [
    “Storage HW Mnt”,
    1843160,
    152732.8,
    141250.56,
    103583.744,
    56971.0592,
    2297698.1632
    ]

    Would really appreciate if you could guide me on the syntax as MS docs don’t help and googling does return any similar example 🙁

    Thanks again.

  5. Pieter,

    just for reference the JSON schema looks like:

    {
    “type”: “object”,
    “properties”: {
    “@@odata.context”: {
    “type”: “string”
    },
    “value”: {
    “type”: “array”,
    “items”: {
    “type”: “object”,
    “properties”: {
    “@@odata.id”: {
    “type”: “string”
    },
    “index”: {
    “type”: “integer”
    },
    “values”: {
    “type”: “array”,
    “items”: {
    “type”: “array”
    }
    }
    },
    “required”: [
    “@@odata.id”,
    “index”,
    “values”
    ]
    }
    }
    }
    }

    1. I set a compose action to the following json

      {
      "odata.context": "https://graph.microsoft.com/v1.0/$mxxxxxxxxxxx’)/rows",
      "value": [
      {
      "odata.id": "/drives('b%21mxxxxxxxxxx)/rows/itemAt(index=0)",
      "index": 0,
      "values": [
      [
      "Storage HW",
      3649800,
      375980,
      455928,
      501520.8,
      551672.88,
      5534901.68
      ]
      ]
      }
      ]
      }

      then in a second compose I used the following expression:

      first(first(outputs(‘compose’)?[‘value’])?[‘values’])?[0]

      And this resulted in the expected fields being returned.

  6. Pieter, thanks so much for the help with the syntax, as soon as I get of my 5th zoom of the day I will try your code! ;o)

  7. Pieter,

    Humph, so I tried exactly the same as you did above and I can’t even get that to work just tells me the expression for the second compose is invalid! Something seems horribly wrong this end.

  8. Hi,

    I came around here to find a solution for my problem, it is really useful, I’m not going to lie.

    However, I have a small problem with my current scenario (it is a bit weird for some people).

    I tried to create a very simple array (for storing weighted score that has 4 score in each question that answered in Microsoft Forms). I already created logic behind. However, when I want to add new item into array, I don’t really know how to do so.

    For example:

    I have empty array in “Result”, and I want to include weights, which are 0, 0.33, 0.66, and 1. There is 70 questions in the surveys.

    Expected results should be something like this:

    [0.66, 0.66, 0.33, …, 1]

    Is it possible to do so? If not, what is the best possible way to do this?

    1. Hi Kittanan,

      As you are having a set length array i would go for the easy option

      In the image below refer to each element in the array using the code below

      outputs('Compose')[0]

      Edit an array

      this will then result in the following flow run:

      Result array

      So I inserted a value in the middle of an array.

      Does this help?

  9. Hi,

    First of all, sorry for extremely late reply. Second, thanks for pointing out, however the solution doesn’t work with my current algorithm.

    Currently, I use Apply to Each in each loop for storing answers (and put them on Excel Online). The structure of project is involved with Microsoft Forms and relied on real-time data.

    My solution is to create answer that filled in from the forms as JSON files (named Answers). Then, I match the answers with the weight (with another JSON file, named Questions). Then, I store the answers to array (named Results).

    Here is the psudo-code of the flow that I am running for my project:

    for (questionList = 1; questionList <= 70; questionList++)
    for (scoreList = 1; scoreList <= Question[questionList].answers.length(); scoreList++)
    if (Answers[questionList].name == Question[questionList].answers[scoreList].name)
    then Results[questionList] = Question[questionList].answers[scoreList].weight;

    The problem is, my current solution is not efficient enough and I want to run parallelly.

    So, if you have any solution for this, I would really appreciate for that.

  10. Hi Pieter,

    I’m trying to use an array variable in the Odata filter for a Get Records action without success.

    Specifically, i have an array variable called vUserIds which contains 3 elements.
    In the filter field for the Get Records action I need to filter the list of return records by the Ids in the above variable.

    I had hoped that the following would work:

    “Id in vUserIds”

    However, the Dynamic Content dialog doesn’t give me the option of choosing the array variable.

    So, can you advise on how I can achieve the above?

    Thanks in advance
    Jon

    1. Hi Jon,

      Sorry for the delay. Your comment ended up in Spam.

      There are two ways that you can do this.

      1. Use the filter action and then count the number of items found.
      2. Join the 3 strings together ( using the join function) this could give you text1;text2;text3 Now you can do a textual search. within that joined up text.

  11. Hi Pieter,
    Thanks for this useful post!
    In looping through an array, what is the best way to determine if the current item is the last one in the array?
    I thought of comparing last(body(‘Filter_array name’)) but couldn’t figure out the way to retrieve current item’s value or index…

  12. Pieter,

    I haven’t seen solutions for array element assignment without using take, array, skip, and union. Union causes drops of array elements where duplicates occur. As such, I’ve come up with a solution using Select and Set Variable actions.

    Select:
    From: @{range(0,length(variables(‘~~my array~~’)))}
    Map: @{if(equals(~~my index~~,item()),~~new array element value~~,variables(‘~~my array~~’)[item()])}

    Set Variable:
    Name: ~~my array~~
    Value: @{body(‘Select’)}

    Tweaking the second parameter of the range function to max() of length of the array and the index could be used for array element assignment with extension. The map would need to fill extended elements before index with an empty value.

    Cheers,
    Marty

    1. Hi Marty,

      I like the idea. I would only use a compose instead of a variable. Variables in branches are slow and I wonder if a compose would be faster.

      Anyways like I said I like the uses of Select, they are so much faster than Apply to each.

  13. I agree. However, in a flow I am building there are concurrent steps that need to reference the resulting array globally. A compose action would only be local to that line of steps, but not outside in a parallel line.

    On thing to consider and perhaps test for are race conditions on assignment with parallelism occurring. I could envision a flow action may be atomic, but not a series of steps — e.g. Select + Set Variable. A concurrent thread could in theory execute a separate Select + Set on the same array in between the execution of another (slower) thread executing Select + Set and changes are lost when the first (slower) thread finishes the Set action after the second (faster) thread.

      1. Follow up:

        (1) I didn’t describe the use case well enough and hence I think there is confusion with concurrency control and degree of parallelism performance versus my problem. What I have is several asynchronous parallel branches in the flow. All the parallel branches will set their respective element in the array at some non-deterministic time.

        (2) I created a simple example flow to check for race conditions. All parallel branches perform the same actions. The result was that there is indeed a race condition between Select and Set Variable actions of different branches — even with Concurrency Control on and Degree of Parallelism 1. In theory it makes sense as flow parallel branches are non-deterministic finite automata. Therefore a semaphore/monitor-esque solution would be needed to control entry and exit to the Select + Set actions preventing competing parallel branches from overwriting each other.

        (3) Time to log a new request for a new atomic action to Set Array Element in the Variable actions… https://powerusers.microsoft.com/t5/Power-Automate-Ideas/idb-p/MPAIdeas

  14. hi Pieter, I do find your posts really handy . With my issue, I easily filtered an array in my Power Automate (item()?[‘DocumentType’] and have now have [{“DocumentType”:”Policy”,”Importance”:1}]

    Next, I would like the Importance value but so far none of these espressions work
    outputs(‘My_compose’ )?[‘Importance’]
    outputs(‘My_compose’)[1]?[‘Importance’]

    Btw .. I didn’t see the array functions like “take” appear under the Expression selector ..
    Aforeach loop seems like overkill, even though this should work.
    Any thoughts please

  15. Pieter , thanks very much for replying. I woke up this morning thinking I had to use [0] to reference the first item in the array ( similar to PowerShell ) but I am glad you have confirmed this . Out of interest, are functions like “take” only available to logic apps ? regards Daniel

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: