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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
cmplieger
Frequent Visitor

Looping in PowerQuery

I am trying to run the following Calculation using PowerQuery in PowerBi:

"Service1 Revenue"*"Service1 Rating" + "Service2 Revenue"*"Service2 Rating" + ...

The data comes from these two tables:

╭───╥────────────┬─────────────╮
│ 1 ║   Service  │   Revenue   │
╞═══╬════════════╪═════════════╡
│ A ║ Service 1  │ 10          │
│ B ║ Service 2  │ 100         │
│ C ║ etc        | etc         │
└───╨────────────┴─────────────┘

╭───╥────────────┬─────────────╮
│ 2 ║   Service  │   Rating    │
╞═══╬════════════╪═════════════╡
│ A ║ Service 1  │ 1           │
│ B ║ Service 2  │ 5           │
│ C ║ etc        | etc         │
└───╨────────────┴─────────────┘

I cannot figure out how to write the looping to perform the calculation per service and then sum all the results up.

In excel I guess a vba loop would have worked but not sure how to do that here.

1 ACCEPTED SOLUTION

@cmplieger

 

Since you already build the relationship between that two tables, you can directly add the product row by row with SUMX() function as @BhaveshPatel mentioned.

 

99.PNG

 

Regards,

View solution in original post

4 REPLIES 4
Greg_Deckler
Community Champion
Community Champion

There is no "looping" in Power Query, only recursion:

http://social.technet.microsoft.com/wiki/contents/articles/33920.power-query-using-recursion-to-solv...

 

In your case, you could possibly do a join Merge query step) of the two tables and then a Group By or you could just import the two tables, relate them to one another and do you calculations in DAX.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

I have linked the two tables together and I understand how I could manually calculate it by using filters in dax to select the row I want. But I am dealing with >100k rows so manually selecting each service is not an option.

@cmplieger

 

Since you already build the relationship between that two tables, you can directly add the product row by row with SUMX() function as @BhaveshPatel mentioned.

 

99.PNG

 

Regards,

"But I am dealing with >100k rows so manually selecting each service is not an option."

 

What do you mean by manually selecting each service.

 

You can use SUMX and RELATED function in a measure to get the aggrgated total of both columns. In DAX, this looping concept is called ROW CONTEXT and Iterator functions are well suited to do the job. It is bit slower but I guess faster than PowerQuery and VBA looping.

 

TotalRevenue&Ratings:=SUMX(Table1,Table1[Revenue]*RELATED(Table2[Ratings))

 

Thanks & Regards,
Bhavesh

Love the Self Service BI.
Please use the 'Mark as answer' link to mark a post that answers your question. If you find a reply helpful, please remember to give Kudos.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.