cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ArchieGoodwin
Advocate I
Advocate I

Using "If" condition in Initialize Variable expression

Hello all. I'm parsing JSON from an online form, and I need to change the date formats for any date fields. That expression works fine (I initialize a variable with the raw data first, then initialize a "fixed" variable underneath that uses FormatDateTime to convert the format). What I did NOT test for were the two optional date fields, which are now returning a "null" value in JSON if not filled out, and it breaks my flow because I get a string error about the data not being in ISO date format (well, there's no data, so duh).

I've tried using the following expression in the Initialize Variable step, and it keeps telling me the expression is invalid:

if(empty(triggerOutputs()?['body/DOM']), 'null', (formatDateTime(variables('Raw_DOM')), 'dd/MM/yyyy'))

Funny thing is, if I remove the bracket in front of formatDateTime and at the end of yyyy, then it allows me to keep the expression, but it breaks when I run the flow because it is thinking it has four elements in the expression instead of three. But when I try to correct it with brackets as above, it won't even let me save the expression because PA says "expression invalid."

Anyone have any solutions for this? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
tom_riha
Super User
Super User

Hello @ArchieGoodwin ,

you've got one extra bracket that closes the formatDateTime(...) expression. FormatDateTime(...) needs 2 parameters, the date and the format, but in your expression you close it before you define the format. It should be:

if(empty(triggerOutputs()?['body/DOM']), 'null', formatDateTime(variables('Raw_DOM'), 'dd/MM/yyyy'))

 



[ If I have answered your question, please Accept the post as a solution. ]
[ If you like my response, please give it a Thumbs Up. ]

[ I also blog about Power Automate solutions even for non-IT people. ]

View solution in original post

2 REPLIES 2
tom_riha
Super User
Super User

Hello @ArchieGoodwin ,

you've got one extra bracket that closes the formatDateTime(...) expression. FormatDateTime(...) needs 2 parameters, the date and the format, but in your expression you close it before you define the format. It should be:

if(empty(triggerOutputs()?['body/DOM']), 'null', formatDateTime(variables('Raw_DOM'), 'dd/MM/yyyy'))

 



[ If I have answered your question, please Accept the post as a solution. ]
[ If you like my response, please give it a Thumbs Up. ]

[ I also blog about Power Automate solutions even for non-IT people. ]
ArchieGoodwin
Advocate I
Advocate I

Thanks, Tom! Clearly, I was not fully functional when I was reviewing that expression!

 

Cheers!

Helpful resources

Announcements
2022 Release Wave 1 760x460.png

2022 Release Wave 1 Plan

Power Platform release plan for the 2022 release wave 1 describes all new features releasing from April 2022 through September 2022.

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Top Solution Authors
Top Kudoed Authors
Users online (1,343)