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

Join us at the 2025 Microsoft Fabric Community Conference. March 31 - April 2, Las Vegas, Nevada. Use code FABINSIDER for $400 discount. Register now

Migasuke

Self referencing query in Power Query

Problem description

As described in the teaser above - sometimes you need to create additional "helping" queries, which might not be optimal and your number of queries can enormously grow.

Let's take a look at simple example here:

I have one query with basic information like Order ID, Customer ID, Revenue etc.

Migasuke_0-1729852143310.png

 

We assume that one customer can have multiple orders. What if we want to bring information about average revenue by Customer ID? Something like this:

Migasuke_1-1729852355039.png

 

With a basic approach you could create two separate queries and merge them together:

Migasuke_2-1729852465887.png

 

But why to duplicate some steps (because referencing a whole query and combining it again with original does not work), if you can avoid it?
I will explain in next steps.

Solution description
1. Let me introduce my easy query:

Migasuke_3-1729852678427.png


As you can see I barely do any transformations, only changing some data types and removing unncessary columns.

2. In my next step I need to find the average revenue for each Client ID. The easiest way is just to use Group By with Average. To better demonstrate my result I will also do simple count of orders for each Client ID.

Migasuke_5-1729853090669.png


Which results into:

Migasuke_6-1729853188984.png


3. Now we need to bring those aggregated results back to our original step. If you remember, it was called "Removed Other Columns". We can do it by merging TWO STEPS into one:

Migasuke_0-1729854297500.png

 

As you can see above I am mixing to different query steps in Merge operation. My initial "Removed Other Columns" is used as a left table and later "Grouped Rows" as right table.

Result of the step looks like this:

Migasuke_1-1729854445132.png

 

4. At this moment we can just simple expand Agg Data column.

Migasuke_2-1729854575678.png

 


5 (TEST). So far we can' really tell if the result is correct, so just for testing purposes I will filter on Client IDs, where we have multiple orders:

Migasuke_3-1729855052617.png


As we can see, the Average matches with our column.



Final note: You can do those references not only with merge, but with different transformations like append, custom column and more!

 

In case of any questions - let me know either here or ask on my Linkedin profile:
https://www.linkedin.com/in/milan-mejzr-74884012a/