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
papi0506
Frequent Visitor

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-func...

 

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

Screenshot 2023-09-14 at 3.55.14 PM.png

 

 

 

 

Thanks in advance for any help suggested 🙂

 

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

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

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

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

5 REPLIES 5
ImkeF
Super User
Super User

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

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

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

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 tablestill get a tablereturns a record, but wants to convert it to a number?returns a record, but wants to convert it to a number?

@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
Super User
Super User

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] )))

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

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? 

Screenshot 2023-09-15 at 2.17.19 PM.png

 

Screenshot 2023-09-15 at 2.18.22 PM.png

 

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