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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors