Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Foreach loop in PowerQuery

Hi,

Maybe a stupid question but I'm kinda new to PowerQuery and was looking for something similar to a foreach. 

 

Basically what I'm trying to do is perform an API call and from results in the first query perform a number of second queries to fill a table with results. In the second query I'm also looking to change part of the URL with values from the first results. I have no idea where to begin. Had a look at a couple of articles but for a beginner like me it doesn't make a lot of sense. 


Here is the first results I got

 

 

 

  • Hi Anonymous 

     

    I've got a blog post that deals with just this situation but I don't know the rules here for linking out to such things so I won't.

     

    Using a custom function may do the job but without knowing a lot more about what it is you are actually trying to achieve, it's very hard to give you a solution.

     

    What API are you using?

     

    What data does the 1st query return?

     

    How do the 2nd queries get affected by this?

     

    How do you want to change the URL in the 2nd query/ies.

     

    Regards

     

    Phil

5 Replies

  • Hi Anonymous 

     

    I've got a blog post that deals with just this situation but I don't know the rules here for linking out to such things so I won't.

     

    Using a custom function may do the job but without knowing a lot more about what it is you are actually trying to achieve, it's very hard to give you a solution.

     

    What API are you using?

     

    What data does the 1st query return?

     

    How do the 2nd queries get affected by this?

     

    How do you want to change the URL in the 2nd query/ies.

     

    Regards

     

    Phil

  • Anonymous's avatar
    Anonymous
    Not applicable

    Thanks for the help. I gradually learned how I was supposed to do this. In hindsight I now know how open my questions. Really needed some first step guidance :). I got it working by making a custom function and call that from the first query. Like in the line below. The part "each TestRuns([testId])" was crucial in this. With that, it uses the function for each test ID it finds and adds the results to the existing table.  

     

    #"Invoked Custom Function" = Table.AddColumn(ExpandTests, "TestRuns", each TestRuns([testId])),

     

     

  • Chad_OB's avatar
    Chad_OB
    Frequent Visitor

    Hi Anonymous 

     

    I'm curious to know if you had any issues after publishing to the Power BI Service.

     

    I have a similar solution to yours which refreshes successfully in Power BI Desktop, but not in the Power BI Service. If its the latter, then what did you do to solve this?

    • Anonymous's avatar
      Anonymous
      Not applicable

      Chad_OB, For me it worked perfectly after I built a connector.