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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Ronny75
New Member

Looping Power query

Hi

 

I'm fairly new to power query and I don't understand exactly how I'm suppose to solve this.

So i have a query to takes out data for each month from Azure Billing API Rest web service and creating a table that I then use in excel later on.

The Query works just fine but it's "static" meening that I have to add the query manually for each month  and i can't do the whole year since then it fails.

So i would like to find a way to do a loop on month level and in that loop i need to get a value that I can then use in my query so i can get a dataset that i can "add" to my table.

That way it would be possible for me to use that power query table the whole year without haveing to update it each month.

 

Is this possible?

 

This is the structure of my query currently:

let
Data1 =
let
Source = Json.Document(Web.Contents("https://management.azure.com/providers/Microsoft.Billing/billingAccounts/.../invoices?api-version=20...periodEndDate=2021-02-01&periodStartDate=2022-01-01")),
<doing some more stuff>
in
#"MyDatatable",
Data2 =
let
Source = Json.Document(Web.Contents("https://management.azure.com/providers/Microsoft.Billing/billingAccounts/.../invoices?api-version=20...periodEndDate=2021-03-01&periodStartDate=2022-02-01")),
<doing some more stuff>
in
#"MyDatatable",
Data3 =
...

DataTable = Table.Combine({Data2,Data3,Data4,Data5,Data6,Data7,Data8,Data9})

in
DataTable

 

My aim is to just have ONE query that I can use in a loop "for each month  from year start to current month", and in that loop the periodEndDate and periodStartDate values change depending on where in the loop I am and then adding that data to a the "DataTable".

That way i don't need to add the a query when we get to a new month...

the 2 problems i have in this is that i don't know how i should do the loop and get the date to "change" and how I add the data in my query to the data table that will be returned in the end (the "DataTable") in each loop.

 

Any help is appriciated

 

Best Regards

Ronny

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Hello @Ronny75 ,
you can create a table with columns for start- and end-date and then one row for each page.
Then add a column to this table that calls a function doing the extraction. When expanding that column you'll receive one table with all your data consolidated.
I've described the general method here: Web Scraping 2: Scrape multiple pages at once and refresh in Power BI service – The BIccountant

 

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

2 REPLIES 2
ImkeF
Super User
Super User

Hello @Ronny75 ,
you can create a table with columns for start- and end-date and then one row for each page.
Then add a column to this table that calls a function doing the extraction. When expanding that column you'll receive one table with all your data consolidated.
I've described the general method here: Web Scraping 2: Scrape multiple pages at once and refresh in Power BI service – The BIccountant

 

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

Thanks alot for that.

Had some problems first, since my parameters where Date and not Text i had to choose to get "Column name" instead of Date when i did the "Invoke Custom Function" in my Months Table but when i found that it worked like a charm.

Very nice solution, thanks for the help!

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors