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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Griffin_BI
Frequent Visitor

API Call for each row

Hi I have function to return a random name using an API call It's calle "Get Random User Query":

= (x as text) => Json.Document(Web.Contents("https://api.api-ninjas.com/v1/randomuser/", [Headers=[#"X-Api-Key"=x]]))[name]

I would like to return a unique random name for each row in a table.  

I use invoke custom function:
= Table.AddColumn(#"Changed Type", "PseudoNym", each #"Get Random User Query"("API Key"))

 

But it returns the same random name for every row instead of a unique random name for each row. Any ideas please?


3 REPLIES 3
Anonymous
Not applicable

Hi @Griffin_BI ,

 

Agree with Omid_Motamedise and PhilipTreacy.

One approach is to generate a list of random names first and then add them to your table. Here's how you can do it:

First, create a function to generate a list of random names, please refer to this blog:

How to use List.Generate to make API Calls in Power Query M

Please use List.Generate function to return the list of radom names.

Then add an index in your table and do as follows:

= Table.AddColumn( #"AddIndex", "PseudoNym", each GenerateRandomNames{[Index]})

AddIndex: Let's say you've added an index column to your table, named "Index".
GenerateRandomNames: This is a list of random names.
each GenerateRandomNames{[index]}: For each row in the table, [Index] is the index value of the current row. Query3{[Index]} takes the name of the corresponding index from the Query3 list and assigns it to the "PseudoNym" column.

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Omid_Motamedise
Super User
Super User

The issue you're encountering is due to how Power Query evaluates the custom function when adding a new column. When you use a function like Table.AddColumn, it evaluates the function for each row, but if the function returns the same output (like a random name) for each invocation because of caching or other factors, you'll see the same value across all rows.

 

 

Modify your API call to get, for example, 10 random users:

= (x as text) => Json.Document(Web.Contents("https://api.api-ninjas.com/v1/randomuser/?count=10",
Add a column to the table where you pull a random name from the list returned:

= Table.AddColumn(#"Changed Type", "PseudoNym", each
let
users = #"Get Random User Query"("API Key"),
randomIndex = Number.RoundDown(Number.RandomBetween(0, List.Count(users) - 1))
in
users{randomIndex}[name]

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
PhilipTreacy
Super User
Super User

Hi @Griffin_BI 

 

I can't speak for how the API works as I don't have access to it, but does it require a different seed or URL query parameter each time it's called so as to give a different response?

 

Perhaps calling it in the same way each time gives the same result?

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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