Face the fear of $expand and get the lookup values you deserve in Power Automate
I love power automate. I appreciate lookup fields, but I' do NOT love power automate and lookup fields together. The issue starts to arise when you are trying to send notifications which include information about records which includes lookup fields.
I covered one way of handling this in a previous blog [1] using the ‘Get Records’ action, however I know full well there is a better way to do this. Its called $expand, there’s lots of blogs and documentation about it [2]. I have been avoiding the $expand query for two reasons:
“if it aint broken don’t fix it“ - I don’t need this formula and I can work around it using ‘Get Record’, I know its not so friendly of the API calls nor so efficient but it works and speed/API call budget isn’t an issue for any of my current scenarios
the name $expand scares me - it seems too ‘codey’
It’s like making changes in the default solution or direct in production - you know its frowned upon but it does the job and you just don’t tell anyone. Fortunately it annoyed George Doubinski enough that he offered to teach me how to do it, so long as I promised to help him work out how to teach others like me (team ‘anything but code’) about it too. So here we go!
The Goal
Send a daily summary of the subscriptions updated in the last 24 hours - I need the members name, the club they belong to and also a link to the members record
We also want to do this in a way which gets rid of the unnecessary ‘Get Records’ actions by creating more powerful ‘List Records’ queries.
rdu_member($select=firstname,lastname,parentcustomerid_account;$expand=parentcustomerid_account($select=name))
Part One: Get the Members (contact) first and last name
So how do you eat an elephant? One bite at a time. Let’s start with getting the Member first name and last name and for this we simply need to use the ‘Show logical names’ button from Level Up!
rdu_member($select=firstname,lastname)
Part Two: Get the name of the Club (account)who the Members (contact) belongs to
Quick recap
A Subscription (rdu_subscription) belongs to a Member (contact) - we got this bit.
A Member (contact) belongs to a Club (account) - we need this bit
We take the query from above, remove the last bracket and ask for another field. According to ‘Show field values’ its called parentcustomerid but if you try to run the expand query with this he will seriously disagree. This pesky customer field is a bit of a special case, it can be used for linking contacts or accounts so for this we need to use the ‘All Fields’ control on Level Up! you can see that the ‘associatednavigation property’ changes based on if the value is a contact or an account.
Now the formulae knows where we want to look. Next, we add another expand query - this time for the Club (account) entity using the ‘associatednavigation property’ field name rather than the actual underlying name. All we want from the Club (account) is the ‘name’. Because this query is nested we need to use the $expand annotation this time.
rdu_member($select=firstname,lastname,parentcustomerid_account;$expand=parentcustomerid_account($select=name))
Where are my dynamic values?!
Logically now you want to go down to the section where you would have previously used ‘Get Records’ - and expect to see some new Dynamics Values in your list coming from the List Records action, in addition to the first name and last name from part one, but instead the last name has maybe disappeared and the Club (account) name is nowhere in sight - whaaaaat!
What’s happened here? Your too smart for Power Automate! Once you start nesting expand queries some values stop coming back in the ‘Dynamic values’ options. This is normally where I give up and decide its broken and stop trying. DONT GIVE UP - YOU ARE SO CLOSE!
Using the formula below you can get the value from the expand query. Where ‘For_each_member’ is the name of my apply to each action. When you first drop it in it will look a bit odd blue and broken, bit if you save and refresh it will render itself to a more CDS field looking item and this also confirms that the code you have written is understood by the flow.
items('For_each_member')?['rdu_member/lastname'] items('For_each_member')?['rdu_member/parentcustomerid_account/name']
And that my friends is a whirlwind tour of the expand and nested expand query. I really hope the pictures and explanations of where the bits of code come from help you to build your own expand queries more easily - even if you don’t code!
A massive shout out to the amazingly patient George Doubinski for taking the time to teach me this terrifying thing, so I could first understand it, then help to explain it in a less ‘developer-esque’ way. Any comments or questions please hit the comments below - I would love to help more people on their flow journey to expand their horizons (terrible pun intended).
[1] My first documented war with lookup GUIDs in HTML tables https://www.ameyholden.com/articles/html-tables-with-lookup-values-in-power-automate
[2] $expand query documentation https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/retrieve-related-entities-query
The Video Version
If you are more of a watch than read kind of person, head over to Citizen Can for audio visual simulation on expand