cancel
Showing results for
Did you mean:
Helper I

## Power Automate Data Transformation From JSON, Triple Nested Apply to Each Loop

I have fictionalized and simplified this problem for discussion purposes.

Suppose I own a toy factory. Let’s keep it simple: This toy factory has a 2 day workweek (Monday and Tuesday) and a 2 hour day (Hour 1 and Hour 2). Every week, the toy factory produces several types of toys (the quantity of different types of toys that are produced can vary).

Before the start of the week, the toy factory manager creates a Toy Production Plan. The most important thing about the Toy Production Plan is it tells us how many units of a particular toy the factory is planning to make that week. The Toy Production Plan might look something like this:

Or, it might look something like this…:

… the point I am trying to make with these examples is that..

1. There is a variety of different toy types that can be manufactured each week
2. The Toy Production Plan starts on the Monday of that week
3. The Toy Production Plan has 4 columns: [Toy], [Workers per Toy], [Monday Target], [Tuesday Target].

Every week, this data will be passed to a Power Automate flow as a JSON object from a Power App.

I essentially need to do a data transformation with a Power Automate flow. The flow needs to transform the factory manager’s production plan into a structure that works with the data table that the data needs to be stored in (dataverse table) Below, I show what the transformation needs to look like:

In the dataverse data table, each row corresponds to a particular date – hour of day – toy combination. Each row of the dataverse table will contain an “Hour Target”, which is the target number of units that must be produced for that particular hour/date/toy. This value is equal to the target of units for the day, divided by 2. I don’t need to make this column in Power Automate, as I can just have a calculated column in the dataverse table that calculates this value simply by dividing the [Day Target] column’s value by 2.

With my current approach to solving this data transformation problem with Power Automate, I am attempting to make a triple nested “Apply to each” loop. The structure of the triple nested “Apply to each” loop may look something like this:

Loop through dates d of the week

Loop through hours h of the day

Loop through the toys t that will be produced that week

Write d, h, t, Day Target, and Workers per Toy to new row in dataverse table

However, when I try to build this flow, I end up running into tons of problems. If someone was able to figure out this flow in Power Automate, I’d worship you like the Power Automate god/goddess that you are.

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

Hopefully this is what you're looking for. Note that I've built it to cater for X number of hours (would need to be passed into the flow, as well as the WC date). It will work out the number of days dynamically based on the Target properties (Monday Target, Tuesday Target, etc.).

For this example, I've used a single JSON object that contains the WC Date, Number of hours, and actual data. Assume this would all be passed into the flow via Power Apps in your solution. Also, note that I haven't added the action to add to your Dataverse table - just building up the objects that you can then easily use.

See full flow below. I'll go into each of the actions.

Body is a Compose that contains the data that would be passed in via Power Apps.

``````{
"WC": "2023-03-20",
"Hours": 3,
"Values": [
{
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Monday Target": 300,
"Tuesday Target": 200
},
{
"Toy": "Racecar",
"Workers per Toy": 18,
"Monday Target": 200,
"Tuesday Target": 250
},
{
"Workers per Toy": 25,
"Monday Target": 180,
"Tuesday Target": 200
}
]
}``````

XML is a Compose that converts our Values array (our data) to XML so we can use XPath expressions later in the flow. The expression used is.

``xml(json(concat('{"root": { value:', outputs('Body')?['Values'], '}}')))``

Select extracts out the Target property names from our data. This will allow us to retrieve the correct Targets for each day, and to dynamically calculate the number of days. The expressions used are:

``````//From
xpath(outputs('XML'), '//root/value[1]/*[contains(name(), "Target")]')

//Map
replace(xpath(item(), 'name(/*)'), '_x0020_', ' ')``````

Initialize variable Items creates a new variable called Items of type Array. This will eventually contain all of our objects.

Apply to each Day iterates over the number of days (starting with the value 0). If there were 3 days, we would iterate over 0, 1, and 2. It uses the following expression to generate the range we want to iterate over.

``range(0, length(xpath(outputs('XML'), '//root/value[1]/*[contains(name(), "Target")]')))``

Apply to each Hour iterates over each hour (starting with the value 1). If there were 4 hours, we would iterate over 1, 2, 3, and 4. It uses the following expression to generate the range we want to iterate over.

``range(1, outputs('Body')?['Hours'])``

Apply to each Day iterates over each of our data objects within the Values array. It uses the following expression.

``outputs('Body')?['Values']``

Append to array variable Items appends a new object for the current iteration. Below is the full expression for the object. Beneath that are each of the individual expressions used.

``````{
"Hour of Day": @{items('Apply_to_each_Hour')},
"Toy": @{items('Apply_to_each_Toy')?['Toy']},
"Workers per Toy": @{items('Apply_to_each_Toy')?['Workers per Toy']},
"Day Target": @{items('Apply_to_each_Toy')[body('Select')[items('Apply_to_each_Day')]]},
"Hour Target": @{div(items('Apply_to_each_Toy')[body('Select')[items('Apply_to_each_Day')]], outputs('Body')?['Hours'])}
}``````

``````//Date

//Hour of Day
items('Apply_to_each_Hour')

//Toy
items('Apply_to_each_Toy')?['Toy']

//Workers per Toy
items('Apply_to_each_Toy')?['Workers per Toy']

//Day Target
items('Apply_to_each_Toy')[body('Select')[items('Apply_to_each_Day')]]

//Hour Target (Day Target divided by number of days)
div(items('Apply_to_each_Toy')[body('Select')[items('Apply_to_each_Day')]], outputs('Body')?['Hours'])``````

For your example, you could remove the Items variable completely, and replace it with your Add a new row action and add the data directly into your table using the expressions above.

If we ran the flow, we would see the following output that's stored in our Array variable called Items.

``````[
{
"Date": "2023-03-20",
"Hour of Day": 1,
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Day Target": 300,
"Hour Target": 100
},
{
"Date": "2023-03-20",
"Hour of Day": 1,
"Toy": "Racecar",
"Workers per Toy": 18,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-20",
"Hour of Day": 1,
"Workers per Toy": 25,
"Day Target": 180,
"Hour Target": 60
},
{
"Date": "2023-03-20",
"Hour of Day": 2,
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Day Target": 300,
"Hour Target": 100
},
{
"Date": "2023-03-20",
"Hour of Day": 2,
"Toy": "Racecar",
"Workers per Toy": 18,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-20",
"Hour of Day": 2,
"Workers per Toy": 25,
"Day Target": 180,
"Hour Target": 60
},
{
"Date": "2023-03-20",
"Hour of Day": 3,
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Day Target": 300,
"Hour Target": 100
},
{
"Date": "2023-03-20",
"Hour of Day": 3,
"Toy": "Racecar",
"Workers per Toy": 18,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-20",
"Hour of Day": 3,
"Workers per Toy": 25,
"Day Target": 180,
"Hour Target": 60
},
{
"Date": "2023-03-21",
"Hour of Day": 1,
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-21",
"Hour of Day": 1,
"Toy": "Racecar",
"Workers per Toy": 18,
"Day Target": 250,
"Hour Target": 83
},
{
"Date": "2023-03-21",
"Hour of Day": 1,
"Workers per Toy": 25,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-21",
"Hour of Day": 2,
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-21",
"Hour of Day": 2,
"Toy": "Racecar",
"Workers per Toy": 18,
"Day Target": 250,
"Hour Target": 83
},
{
"Date": "2023-03-21",
"Hour of Day": 2,
"Workers per Toy": 25,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-21",
"Hour of Day": 3,
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-21",
"Hour of Day": 3,
"Toy": "Racecar",
"Workers per Toy": 18,
"Day Target": 250,
"Hour Target": 83
},
{
"Date": "2023-03-21",
"Hour of Day": 3,
"Workers per Toy": 25,
"Day Target": 200,
"Hour Target": 66
}
]``````

----------------------------------------------------------------------
If you like my response, please consider giving it a Thumbs Up.
6 REPLIES 6
Super User

Challenge accepted 🙂

A couple of questions:

1. Will the Week Commencing Date also be passed into Power Automate from the Power App?
2. Does it need to work for any number of days and hours? And if so, how would you know the total number of hours.
3. Would it be a different number of days and/or hours each time you pass it into Power Automate from Power Apps?

I have it working for the simple set of data you provided in your example, but not flexible with regards to number of days/hours.

----------------------------------------------------------------------
If you like my response, please consider giving it a Thumbs Up.
Helper I

@grantjenkins Yes, the week commencing date will be passed into Power Automate from the Power App.

While I am interested in seeing what a solution would look like for any number of days and hours, for now I think its best to keep it simple (because my Power Automate skills are not advanced yet) and assume the number of work days and work hours stays constant. In the real-world situation where I will have to apply this solution, I will need to make it work for 5 business days in a work week, and 9 work hours in a work-day.

Super User

Hopefully this is what you're looking for. Note that I've built it to cater for X number of hours (would need to be passed into the flow, as well as the WC date). It will work out the number of days dynamically based on the Target properties (Monday Target, Tuesday Target, etc.).

For this example, I've used a single JSON object that contains the WC Date, Number of hours, and actual data. Assume this would all be passed into the flow via Power Apps in your solution. Also, note that I haven't added the action to add to your Dataverse table - just building up the objects that you can then easily use.

See full flow below. I'll go into each of the actions.

Body is a Compose that contains the data that would be passed in via Power Apps.

``````{
"WC": "2023-03-20",
"Hours": 3,
"Values": [
{
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Monday Target": 300,
"Tuesday Target": 200
},
{
"Toy": "Racecar",
"Workers per Toy": 18,
"Monday Target": 200,
"Tuesday Target": 250
},
{
"Workers per Toy": 25,
"Monday Target": 180,
"Tuesday Target": 200
}
]
}``````

XML is a Compose that converts our Values array (our data) to XML so we can use XPath expressions later in the flow. The expression used is.

``xml(json(concat('{"root": { value:', outputs('Body')?['Values'], '}}')))``

Select extracts out the Target property names from our data. This will allow us to retrieve the correct Targets for each day, and to dynamically calculate the number of days. The expressions used are:

``````//From
xpath(outputs('XML'), '//root/value[1]/*[contains(name(), "Target")]')

//Map
replace(xpath(item(), 'name(/*)'), '_x0020_', ' ')``````

Initialize variable Items creates a new variable called Items of type Array. This will eventually contain all of our objects.

Apply to each Day iterates over the number of days (starting with the value 0). If there were 3 days, we would iterate over 0, 1, and 2. It uses the following expression to generate the range we want to iterate over.

``range(0, length(xpath(outputs('XML'), '//root/value[1]/*[contains(name(), "Target")]')))``

Apply to each Hour iterates over each hour (starting with the value 1). If there were 4 hours, we would iterate over 1, 2, 3, and 4. It uses the following expression to generate the range we want to iterate over.

``range(1, outputs('Body')?['Hours'])``

Apply to each Day iterates over each of our data objects within the Values array. It uses the following expression.

``outputs('Body')?['Values']``

Append to array variable Items appends a new object for the current iteration. Below is the full expression for the object. Beneath that are each of the individual expressions used.

``````{
"Hour of Day": @{items('Apply_to_each_Hour')},
"Toy": @{items('Apply_to_each_Toy')?['Toy']},
"Workers per Toy": @{items('Apply_to_each_Toy')?['Workers per Toy']},
"Day Target": @{items('Apply_to_each_Toy')[body('Select')[items('Apply_to_each_Day')]]},
"Hour Target": @{div(items('Apply_to_each_Toy')[body('Select')[items('Apply_to_each_Day')]], outputs('Body')?['Hours'])}
}``````

``````//Date

//Hour of Day
items('Apply_to_each_Hour')

//Toy
items('Apply_to_each_Toy')?['Toy']

//Workers per Toy
items('Apply_to_each_Toy')?['Workers per Toy']

//Day Target
items('Apply_to_each_Toy')[body('Select')[items('Apply_to_each_Day')]]

//Hour Target (Day Target divided by number of days)
div(items('Apply_to_each_Toy')[body('Select')[items('Apply_to_each_Day')]], outputs('Body')?['Hours'])``````

For your example, you could remove the Items variable completely, and replace it with your Add a new row action and add the data directly into your table using the expressions above.

If we ran the flow, we would see the following output that's stored in our Array variable called Items.

``````[
{
"Date": "2023-03-20",
"Hour of Day": 1,
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Day Target": 300,
"Hour Target": 100
},
{
"Date": "2023-03-20",
"Hour of Day": 1,
"Toy": "Racecar",
"Workers per Toy": 18,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-20",
"Hour of Day": 1,
"Workers per Toy": 25,
"Day Target": 180,
"Hour Target": 60
},
{
"Date": "2023-03-20",
"Hour of Day": 2,
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Day Target": 300,
"Hour Target": 100
},
{
"Date": "2023-03-20",
"Hour of Day": 2,
"Toy": "Racecar",
"Workers per Toy": 18,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-20",
"Hour of Day": 2,
"Workers per Toy": 25,
"Day Target": 180,
"Hour Target": 60
},
{
"Date": "2023-03-20",
"Hour of Day": 3,
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Day Target": 300,
"Hour Target": 100
},
{
"Date": "2023-03-20",
"Hour of Day": 3,
"Toy": "Racecar",
"Workers per Toy": 18,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-20",
"Hour of Day": 3,
"Workers per Toy": 25,
"Day Target": 180,
"Hour Target": 60
},
{
"Date": "2023-03-21",
"Hour of Day": 1,
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-21",
"Hour of Day": 1,
"Toy": "Racecar",
"Workers per Toy": 18,
"Day Target": 250,
"Hour Target": 83
},
{
"Date": "2023-03-21",
"Hour of Day": 1,
"Workers per Toy": 25,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-21",
"Hour of Day": 2,
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-21",
"Hour of Day": 2,
"Toy": "Racecar",
"Workers per Toy": 18,
"Day Target": 250,
"Hour Target": 83
},
{
"Date": "2023-03-21",
"Hour of Day": 2,
"Workers per Toy": 25,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-21",
"Hour of Day": 3,
"Toy": "Ping poing ball",
"Workers per Toy": 20,
"Day Target": 200,
"Hour Target": 66
},
{
"Date": "2023-03-21",
"Hour of Day": 3,
"Toy": "Racecar",
"Workers per Toy": 18,
"Day Target": 250,
"Hour Target": 83
},
{
"Date": "2023-03-21",
"Hour of Day": 3,
"Workers per Toy": 25,
"Day Target": 200,
"Hour Target": 66
}
]``````

----------------------------------------------------------------------
If you like my response, please consider giving it a Thumbs Up.
Helper I

@grantjenkins
FYI: I'm still working on your solution idea, I will update you as soon as I've got it worked out (I've got some new concepts to learn here like XML and Xpath)

Helper I

@grantjenkins
Thank you for this. Very impressive solution. I just mimicked your solution on my environment. I understand it for the most part.
Next I am going to try to implement this proof of concept into the real-world business problem I'm facing. I'll keep you posted.

Helper I

@grantjenkins  Just did a bigger, more realistic test of this solution. It worked! Just "Accepted as Solution".

This solution will be good for helping people do some advanced data transformations in Power Automate.

Announcements

#### Announcing | Super Users - 2023 Season 1

Super Users – 2023 Season 1    We are excited to kick off the Power Users Super User Program for 2023 - Season 1.  The Power Platform Super Users have done an amazing job in keeping the Power Platform communities helpful, accurate and responsive. We would like to send these amazing folks a big THANK YOU for their efforts.      Super User Season 1 | Contributions July 1, 2022 – December 31, 2022  Super User Season 2 | Contributions January 1, 2023 – June 30, 2023    Curious what a Super User is? Super Users are especially active community members who are eager to help others with their community questions. There are 2 Super User seasons in a year, and we monitor the community for new potential Super Users at the end of each season. Super Users are recognized in the community with both a rank name and icon next to their username, and a seasonal badge on their profile.  Power Apps  Power Automate  Power Virtual Agents  Power Pages  Pstork1*  Pstork1*  Pstork1*  OliverRodrigues  BCBuizer  Expiscornovus*  Expiscornovus*  ragavanrajan  AhmedSalih  grantjenkins  renatoromao    Mira_Ghaly*  Mira_Ghaly*      Sundeep_Malik*  Sundeep_Malik*      SudeepGhatakNZ*  SudeepGhatakNZ*      StretchFredrik*  StretchFredrik*      365-Assist*  365-Assist*      cha_cha  ekarim2020      timl  Hardesh15      iAm_ManCat  annajhaveri      SebS  Rhiassuring      LaurensM  abm      TheRobRush  Ankesh_49      WiZey  lbendlin      Nogueira1306  Kaif_Siddique      victorcp  RobElliott      dpoggemann  srduval      SBax  CFernandes      Roverandom  schwibach      Akser  CraigStewart      PowerRanger  MichaelAnnis      subsguts  David_MA      EricRegnier  edgonzales      zmansuri  GeorgiosG      ChrisPiasecki  ryule      AmDev  fchopo      phipps0218  tom_riha      theapurva  takolota     Akash17  momlo     BCLS776  Shuvam-rpa     rampprakash  ScottShearer     Rusk  ChristianAbata     cchannon  Koen5     a33ik  Heartholme     AaronKnox  okeks      Matren  David_MA     Alex_10        Jeff_Thorpe        poweractivate        Ramole        DianaBirkelbach        DavidZoon        AJ_Z        PriyankaGeethik        BrianS        StalinPonnusamy        HamidBee        CNT        Anonymous_Hippo        Anchov        KeithAtherton        alaabitar        Tolu_Victor        KRider        sperry1625        IPC_ahaas      zuurg    rubin_boer   cwebb365   Dorrinda   G1124   Gabibalaban   Manan-Malhotra   jcfDaniel   WarrenBelz   Waegemma   drrickryp   GuidoPreite   metsshan    If an * is at the end of a user's name this means they are a Multi Super User, in more than one community. Please note this is not the final list, as we are pending a few acceptances.  Once they are received the list will be updated.

#### Check out the new Power Platform Communities Front Door Experience!

We are excited to share the ‘Power Platform Communities Front Door’ experience with you!   Front Door brings together content from all the Power Platform communities into a single place for our community members, customers and low-code, no-code enthusiasts to learn, share and engage with peers, advocates, community program managers and our product team members. There are a host of features and new capabilities now available on Power Platform Communities Front Door to make content more discoverable for all power product community users which includes ForumsUser GroupsEventsCommunity highlightsCommunity by numbersLinks to all communities Users can see top discussions from across all the Power Platform communities and easily navigate to the latest or trending posts for further interaction. Additionally, they can filter to individual products as well.   Users can filter and browse the user group events from all power platform products with feature parity to existing community user group experience and added filtering capabilities.     Users can now explore user groups on the Power Platform Front Door landing page with capability to view all products in Power Platform.      Explore Power Platform Communities Front Door today. Visit Power Platform Community Front door to easily navigate to the different product communities, view a roll up of user groups, events and forums.

#### Microsoft Power Platform Conference | Registration Open | Oct. 3-5 2023

We are so excited to see you for the Microsoft Power Platform Conference in Las Vegas October 3-5 2023! But first, let's take a look back at some fun moments and the best community in tech from MPPC 2022 in Orlando, Florida.   Featuring guest speakers such as Charles Lamanna, Heather Cook, Julie Strauss, Nirav Shah, Ryan Cunningham, Sangya Singh, Stephen Siciliano, Hugo Bernier and many more.   Register today: https://www.powerplatformconf.com/

Top Solution Authors
Top Kudoed Authors
Users online (3,719)