cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Antesamegutt
Frequent Visitor

Storing value in variable, so it can be referenced without reevaluation

Hi!

 

let

URL = "MyRestApiGetQuery",

Source = if try Web.Contents(URL)[HasError]=true and Web.Contents(URL)[Error]="text that equates to true",

                    then "error identified",

                    else  "something else"

in

Source

 

1: Will the above code send two identical requests to the server? I'm assuming yes

 

2:Is it possible to somehow reuse the return of the first request in the 'and' statement, so that I dont have to query the server twice? 

 

Thanks!

 

Edit: Perhaps and hopefully better questions.

 

    3: Is it possible to store the result of an expression in a variable in memory?  

    4: If yes, and this is what Table/List.Buffer does, how is that particular result referenced?

 

var = Table.Buffer(Mytable)

var2 =  var

 

var2 will just force evaluation of Table.Buffer(Mytable) again? Does something akin to below exist?

 

var = Table.Buffer(Mytable)

var2 =  FromMemory(var)

 

I read that this is possible in DAX, if i understood that correctly

https://docs.microsoft.com/en-us/dax/var-dax,

 

"By using a variable, you can get the same outcome, but in a more readable way. In addition, the result of the expression is stored in the variable upon declaration. It doesn't have to be recalculated each time it is used, as it would without using a variable. This can improve the measure's performance."

 

Thanks again!

2 REPLIES 2
Jimmy801
Community Champion
Community Champion

Hello @Antesamegutt 

 

you can do like this and store the complete result in a variable, but probably is quering the website twice also here, because of lazy evaluation of M

let

URL = "MyRestApiGetQuery",

QueryWEB= try Web.Contents(URL)


in

if QueryWEB[HasError]=true then 
    if QueryWEB[Error]="text that equates to true" then

                    then "error identified",

                    else  "something else"
else null //When nor error was identified


If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

Thanks!

 

I abused a random joke generating API https://official-joke-api.appspot.com/random_joke 

 

If the calls were done through a variable, like in your code, then the contents of the variable would never change and so I think the query was run only once

 

If the calls were done 'naked', then they would often but not always have the same contents, leading me to believe the query was ran multiple times. The results of second below query are kind of mysterious

 

let
URL = "https://official-joke-api.appspot.com/random_joke",
Jokesetup = Json.Document(Web.Contents(URL))[setup],
Jokeoutput =
Jokesetup&Jokesetup&Jokesetup&Jokesetup&Jokesetup&Jokesetup&Jokesetup&Jokesetup&Jokesetup&Jokesetup

// to 100 -- jokesetup is always the same

in Jokeoutput

 

vs

 

let

URL = "https://official-joke-api.appspot.com/random_joke",

 

Jokeoutput =
Json.Document(Web.Contents(URL))[setup]&Json.Document(Web.Contents(URL))[setup]&Json.Document(Web.Contents(URL))[setup]&Json.Document(Web.Contents(URL))[setup]&Json.Document(Web.Contents(URL))[setup]&Json.Document(Web.Contents(URL))[setup]

// - to 100, result of Web.Contents vary, but hard to figure out what is going on. The server should return a 4xx error if 100 requests are received from same ISP in 15 minutes, but above code will work even with 150 Web.Contents. It seems some under the hood power query features kick in after 10 or so W.C() and the same result is returned for the remainder of W.C()'s

in Jokeoutput

 

Helpful resources

Announcements
PBI_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Top Kudoed Authors