cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

if statement and early exit in m function

OK, so say I make a simple function (in the advanced editor):

(test1 as text)=>
let
    if test1 = "yes" then Source = "OK" else Source = "Dang"
in
    Source

This has an error of Token Identifier expeted for the if. What am I doing wrong??

 

The other thing I was wondering, is if test1 is not equal to "yes", instead of setting source as "Dang", am I able to exit the function with a text string as an error? In most programming languages you just return a value there and it will stop processing the rest of the function, but this doesn't seem an option in Power Query.

1 ACCEPTED SOLUTION
LivioLanzo
Solution Sage
Solution Sage

Hi @Anonymous  - 

 

the right synthax is:

 

(test1 as text)=>
let
    Source = if test1 = "yes" then "OK" else "Dang"
in
    Source

 

for the other question, it depends on what else have in your query, is the function the only code?

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

View solution in original post

2 REPLIES 2
LivioLanzo
Solution Sage
Solution Sage

Hi @Anonymous  - 

 

the right synthax is:

 

(test1 as text)=>
let
    Source = if test1 = "yes" then "OK" else "Dang"
in
    Source

 

for the other question, it depends on what else have in your query, is the function the only code?

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

Anonymous
Not applicable


@LivioLanzo wrote:

it depends on what else have in your query, is the function the only code?


This function would be called from another function. However, it is only this function I want to exit, and just return an error string. The parent function should continue to process and will just display the error.

Helpful resources

Announcements
collabdays lisbon

CollabDays Lisbon - 26 November 2022

Sessions include practical, hands-on experience that will help you take the next step in your career and know-how.

Difinity Conference

Difinity Conference 2022

Difinity is the largest Microsoft Data, AI, Power BI, Power Platform and Business Applications Conference in New Zealand.

European Share Point Conference

European Share Point Conference

This conference returns live and in-person in Copenhagen this 28 November-01 December with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Microsoft 365 Conference â__ December 6-8, 2022

Microsoft 365 Conference - 06-08 December

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.