Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Taurus
Frequent Visitor

Custom Function Execution

Hi all!

 

I have IIKO online service for api executions. I must first get a token using specific api, then send some api to load data and at the end send specific api to release token. 

I write this function in PBI Query Editor:

-------------------------

(api as text, optional params as text) as binary =>
let
Token = #"IIKO Server Auth"(),
params = if params is null then "" else if params="" then "" else "&" & params,
Data = Web.Contents(IIKO_API_SERVER & api & "?key=" & Token & params),
Unauth= #"IIKO Server Unauth"(Token)
in
Data
------------------

As you can see, first I invoke specific api call #"IIKO Server Auth"(), which gives me token as a text string, then I load data passing this token as "key" parameter within GET -request and finally I release token using another specific api call #"IIKO Server Unauth"(Token). But I return function execution in Data variable.

The question is:   Does Unauth= #"IIKO Server Unauth"(Token)  executes certainly? Or does function execution stops before this step (when Data data received).

 

Thanks

1 ACCEPTED SOLUTION
Nolock
Resident Rockstar
Resident Rockstar

Hi @Taurus,

the M language is a functional one. It isn't executed top-down as procedural languages, but it starts with the expression after the keyword IN and lazily evaluates ony expressions which it really needs to get the result.

In your case the step Unauth will never be executed.

What you can do is for example:

(api as text, optional params as text) as binary =>
let
  Token = #"IIKO Server Auth"(),
  params = if params is null then "" else if params="" then "" else "&" & params,
  Data = Web.Contents(IIKO_API_SERVER & api & "?key=" & Token & params),
  Unauth= #"IIKO Server Unauth"(Token),
  Result = if Data <> null and Unauth <> null then Data else error "Data or Unauth is null"
in
  Result

It will invoke Unauth immediatelly after Data in this order. (At least from my experiments, expressions are executed from left to right.)

View solution in original post

1 REPLY 1
Nolock
Resident Rockstar
Resident Rockstar

Hi @Taurus,

the M language is a functional one. It isn't executed top-down as procedural languages, but it starts with the expression after the keyword IN and lazily evaluates ony expressions which it really needs to get the result.

In your case the step Unauth will never be executed.

What you can do is for example:

(api as text, optional params as text) as binary =>
let
  Token = #"IIKO Server Auth"(),
  params = if params is null then "" else if params="" then "" else "&" & params,
  Data = Web.Contents(IIKO_API_SERVER & api & "?key=" & Token & params),
  Unauth= #"IIKO Server Unauth"(Token),
  Result = if Data <> null and Unauth <> null then Data else error "Data or Unauth is null"
in
  Result

It will invoke Unauth immediatelly after Data in this order. (At least from my experiments, expressions are executed from left to right.)

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors