Power Automate - Dynamic Variables
david wyatt
Posted on July 4, 2023
Let's start with what a dynamic variable is, then why it's so cool.
A dynamic variable is when we are able to select the variable based on another variable, i.e. dynamically construct the parts of an expression.
In Power Automate if we want to choose between 2 values, we can use either the if() expression, or Conditions/Switches. But this has the drawback of either binary choices, or over complexity (just imagine a Switch with a 100 options).
But what you can do is select a variable/action property dynamically, let me give an example.
Lets say if a variable = Integer then use SharePoint Field_Integer, if it is String then get SharePoint Field_String.
if(equals(variable('var'),'Integer'),
outputs('Get_items')?['body/value']?['Field_Integer']
,
outputs('Get_items')?['body/value']?['Field_String']
)
Yet with dynamic variables we could do this
outputs('Get_items')?['body/value']?[concat('Field_',variable('var'))]
So any property can be set from a variable.
outputs('Get_items')?['body/value']?[YOUR VARIABLE]
The main time I use this is when I update a html template file with lots of placeholders. Before I would do the below
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
body('Get_file_content_template'),
'{name}',
outputs('Get_item')?['body/Title']
),
'{sdd}',
base64ToString(base64(body('HTTP')))
),
'{type}',
outputs('Get_item')?['body/App_x002f_Automation/Value']
),
'{business}',
outputs('Get_item')?['body/BusinessTeam']
),
'{geo}',
outputs('Get_item')?['body/Location/Value']
),
'{solution}',
outputs('Get_item')?['body/ShortDescriptionoffunctionality']
),
'{storage}',
body('Create_HTML_table_data')
),
'{trigger}',
if(equals(outputs('Get_item')?['body/Trigger/Value'],null),'',outputs('Get_item')?['body/Trigger/Value'])
),
'{connections}',
body('Create_HTML_table_con')
),
'{privileges}',
body('Create_HTML_table_privil')
),
'{output}',
body('Create_HTML_table_out')
),
'{data}',
outputs('Get_item')?['body/ShortDescriptionofDataHandled']
),
'{owner}',
outputs('Get_item')?['body/Author/Email']
)
To update the below file:
Which although efficient, is a nightmare to read let alone maintain/update.
But with dynamic variables I can create a reference table, loop over it and use it to update each placeholder.
The placeholder is the text in the template, the variable is the property I want to replace it with.
I store the template document in a variable, then loop over each item in the list replacing the placeholders. As you can't self reference a variable I pass it to a compose first and then set the variable to the compose.
replace(outputs('Reset_variable'),
items('Apply_to_each')?['Title'],
outputs('Get_item')?['body']?[items('Apply_to_each')?['Variable']]
)
Now this works ok if every property is from same action (e.g Get_item), but what happens if they are from multiple. Well in that case we need to create a consolidation object, and then pass that.
We then reference that output from the object (in this case a compose):
replace(outputs('Reset_variable'),
items('Apply_to_each')?['Title'],
outputs('Object')?[items('Apply_to_each')?['Variable']]
)
I must admit this is a niche feature (I am a big advocate of code instead of actions, so my above example goes against my own principles). But there will be certain scenarios where having this in your back pocket will rescue you (e.g. a dynamic list for SharePoint http action). And others where the extra flexibility is gold (imagine having multiple different templates continually updated, we can filter the placeholder list and return different placeholders for each).
And yes, my idea of cool maybe a little 'unique' 😎
Posted on July 4, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.