Conditions in Power Automate

skills
intermediate
Power Automate
Published

February 27, 2025

No feedback found for this session

Prerequisites for this session

  • good general digital skills
  • prior experience of writing your own Flows in Power Automate
  • appropriate M365 access and permissions for Power Automate, Teams, and OneDrive

Introduction

  • Control flow is the technical term that coders use to describe software that changes its behaviour depending on circumstances.
  • This session looks at using variables and conditions to do control flow in Power Automate

Plan

  • We’ll build a series of H&SC-relevant mini-projects:
    • connecting to a OneDrive folder
    • counting files in that folder
    • posting a count of those files in a Teams message
    • varying the text of that Teams message by count
    • varying the content of that Teams message by the time of day (or day of week…)

Project one: connect with a OneDrive folder

  • we’ll start with a very basic Flow that connects to a directory of files in your OneDrive.
Setup
  • download the sample files, and extract to your OneDrive
  • or, create a directory in your OneDrive containing some text files, labelled 0.txt, 1.txt, 2.txt9.txt
  • create a new instant flow
  • after the Manually trigger a flow box, add a List files in folder connector
    List files in folder
  • supply the address of your directory of text files in the List files in folder connector
  • save and test your flow
Tip
  • don’t expect any interesting output, but do run this flow to check for errors and permissions
  • in the next project, we’ll collect part of the information that Power Automate makes available about your files
  • Power Automate has connectors for most network/cloud file storage platform, so this approach should be adaptable to whatever platform you use

Project two: counting files

  • we’ll now update that flow to count the number of files in the folder
  • we’ll need to create variables to hold that number
  • in later stages, we’ll then pass that count variable into some useful output (like a Teams message)
Task
  • add an Initialize variable block
    Add an Initialize variable block
  • now populate the Initialize variable fields as shown below:
    Populate the Initialize variable fields
    • give your variable a name: file_content
    • set the type, which tells Power Automate what sort of value to expect: array
    • finally, set a starting value: body/value
  • we now have our file_content variable that contains all our file info
  • in a real flow, we’d probably want to put other parts of that to use
  • but in this simple example, we’ll just count the number of entries (= the number of files in our directory)
Task
  • add a second Initialize variable block
  • populate the fields as shown below:
    • give your variable a name: file_count
    • set the type, which tells Power Automate what sort of value to expect: integer
    • finally, set a starting value. That’s more involved than the previous variable: you need to write a formula to calculate the length of your file_content - length(variables('file_content'))
      Formula to calculate length
  • that should leave you with a flow like the following, which you should save and test:
    File counting flow
  • once run, you should see that the value of file_count is 10
    Test value of file_count

Digression: data types

  • a note on data types: Power Automate has several different built-in data types:
    Data types
  • Power Automate is inflexible about data types: so, for example, if you accidentally try to set an integer to 2.4, your flow will fail
  • you’ll need to think carefully about what sort of value your variable is intended to hold

Project three: posting a count of those files in a Teams message

This one should be easy! At least, once you’ve been added to the KIND test Team…

Task
  • add a Post message in a chat or channel connector
  • populate the fields - it’s recommended to post as User as this allows you to edit any embarrasing errors in your posts once they’ve been posted
  • for the message, write some suitable text, then include the file_count variable
    Compose a Teams message

Sample Teams output

Project 4: varying the text of that Teams message by count

  • so far, our message just repeats the contents of file_count with a bit of static text
  • we can use a Condition to change the text that accompanies that value
    Condition to alter text
  • we should be able to adapt our previous flow to use that condition

Project 5: varying text by time of day

  • we could now add another variable to our flow
  • this will set a value based on the time of day
    Variable based on time of day
  • we can set our condition to use this variable instead of file_count