Forum Discussion
Storing value in variable, so it can be referenced without reevaluation
Hello Anonymous
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
- Anonymous5 years agoNot applicable
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