Forum Discussion

papi0506's avatar
papi0506
Frequent Visitor
2 years ago
Solved

Perform function on list of items in m code

Hello,

 

I'm looking for the basic structure for how to take a single column from a table or list and run a function with each item in the list as parameters or variable in the function. I read a post where danextian was able to anwser 'looping in m code' for someone else and I'm trying to do something similar and get a better understanding.

 

I'm trying to take each row from a list of Ids and append the url with that value.

 

Apologies if I'm going in the wrong direction with List.Generate but it seemed possible. I don't care if I have to create a new table of results or if I have to add it as a column to an existing table, I'm fine with either as long as I can get the results and learn the proper syntax 🙂

 

Similar Post:

https://community.fabric.microsoft.com/t5/Power-Query/Syntax-for-Looping-through-a-List-in-an-M-function/m-p/957718#M33073

 

Example:

let
    visa = SpecialProject,
    visaWithoutQuotes = Text.Remove(visa,""""),
    Auth2 = "Bearer "&visaWithoutQuotes&"",


    listOfResults = List.Generate ( ()=>
         JustAccounts[AccountId],         //List I'm starting with
        each JustAccounts[AccountId]([_]) {[Index]+1} <> null,   //If the next line in the list is not empyt then run the function
        each [Result = Json.Document(Web.Contents(Text.Combine("https://api/statistics/devices/id/",[_]), [Headers = [#"Authorization" = Auth2]]))]                                                    //function to run on each line

)

in

    listOfResults

 

 

 

 

Thanks in advance for any help suggested 🙂

 

  • Hi papi0506 ,
    sorry, wrote if from the head. Please try the following:

    Json.Document(Web.Contents("https://api/statistics/devices/id/"  & [AccountId] ))

5 Replies

  • ImkeF's avatar
    ImkeF
    Community Champion

    Hi papi0506 ,
    sorry, wrote if from the head. Please try the following:

    Json.Document(Web.Contents("https://api/statistics/devices/id/"  & [AccountId] ))
    • papi0506's avatar
      papi0506
      Frequent Visitor

      Thank you, your suggestion looks to have helped the call progress but the results mention that we cannnot convert a value of record type to number. I had to use text.from to help pass a previous error to the current results.
      still get a tablereturns a record, but wants to convert it to a number?

      • papi0506's avatar
        papi0506
        Frequent Visitor

        ImkeFThank you, very much. after clearing some cache and results, I was able to get a 404, so the command is working properly I just need to work on the call and why its returning 404, not a power querie issue.

        Thank you so much.

        same call and worked 🙂
        #"Added Custom" = Table.AddColumn(AccountListAsTable, "Results", each Json.Document(Web.Contents("https://api/statistics/devices/id/" & Text.From([IDS] as any),[Headers = [#"Authorization" = Auth2]])))

  • ImkeF's avatar
    ImkeF
    Community Champion

    Hi papi0506 ,
    if you add a column to a table using the UI, a simple and easy loop will be performed, as every action that you define will be applied to every row of the table. No need to use List.Generate here, this function is only required if you need some recursive features.
    So if you add a column with the following code, it should work in Power BI Desktop:

     

    Json.Document(Web.Contents(Text.Combine("https://api/statistics/devices/id/" , [AccountId] )))

     

    • papi0506's avatar
      papi0506
      Frequent Visitor

      Thank you for your response, but I may not be following this properly. When I try with just the line you provided it presents an error and wants the text of the url as a list?