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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Ira_27
Frequent Visitor

Power Query - Recursive decomposition of rows

Hello Community Members,

 

I am trying to get some help with a scenario we are dealing with. We have daily holdings data and need a way to drill into the subsequent fund holdings to find % investment in the fund at security level. Data resides in one table see sample below 

 

Account ID 123 has investement in Stock A and "MF A", AccountID joins to FundID to find out respective holdings of "MF A" and applies % investment into MF subsequent security. This needs to be shown as a union of Sec A + each security held in "MF B" and "MF C" because Account ID 123 is invested in "MF A" and "MF A" holds units of "MF B" which holds individual securities.

 

I was able to get the desired output by Mergeing the query using self join however when the semantic model is deployed the deomposed (merge) table gets too big because daily positions record count is over 140000 and decomposing results in over 2-3 million records per day. We have large semantic model enabled with incremental refresh set to retain last 24 months. 

 

I looking to see if anyone had this problem and how to solve it with recursive query in Power BI or if there is a better way to semantic model storage. 

 

HoldingDateAccountIDFundIDSecIDSecNameQtyMV
2025-01-01123 Sec AStock A10100
2025-01-01123996MF AMutual Fund A1001000
2025-01-01996997MF BMutual Fund B505000
2025-01-01996998MF CMutual Fund C707000
2025-01-01997 Sec BStock B10100
2025-01-01997 Sec AStock A70700
2025-01-01997 Sec CStock C80800
2025-01-01998 Sec DStock D90900
2025-01-01998 Sec EStock E1001000
2025-01-01998 Sec FStock F1101100
1 ACCEPTED SOLUTION
Sahir_Maharaj
Super User
Super User

Hello @Ira_27,

 

Can you please try this approach:

 

1. Create a Recursive Table

DecomposedHoldings = 
VAR FundHoldings = 
    GENERATE(
        'Holdings',
        FILTER(
            'Holdings',
            'Holdings'[FundID] = EARLIER('Holdings'[AccountID])
        )
    )
RETURN
    UNION('Holdings', FundHoldings)

 

2. Calculate % Investment

% Investment = 
VAR TotalMV =
    CALCULATE(
        SUM('DecomposedHoldings'[MV]),
        ALL('DecomposedHoldings')
    )
RETURN
    DIVIDE(SUM('DecomposedHoldings'[MV]), TotalMV, 0)

 

3. If the semantic model's size is an issue, filter the data in Power Query:

FilteredHoldings = Table.SelectRows(
    Source,
    each [HoldingDate] >= Date.AddMonths(DateTime.LocalNow(), -24)
)

 

Hope this helps!


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

View solution in original post

3 REPLIES 3
Ira_27
Frequent Visitor

Hi @Sahir_Maharaj 

 

I tried your approach but the decomposition only goes one level down. apparently i have atleast 2 levels to recurse over. Any thoughts on how to handle that? 

 

Ira_27
Frequent Visitor

Thank you @Sahir_Maharaj, i will try the approach and revert back here. In terms of semantic model size, the decomposed holding in prod is about 4Million rows per day. My incremental refresh is set to 24 months rolling so even loading one partition for decomposed table manually fails. I will try your method and see if that solves the issue. 

Sahir_Maharaj
Super User
Super User

Hello @Ira_27,

 

Can you please try this approach:

 

1. Create a Recursive Table

DecomposedHoldings = 
VAR FundHoldings = 
    GENERATE(
        'Holdings',
        FILTER(
            'Holdings',
            'Holdings'[FundID] = EARLIER('Holdings'[AccountID])
        )
    )
RETURN
    UNION('Holdings', FundHoldings)

 

2. Calculate % Investment

% Investment = 
VAR TotalMV =
    CALCULATE(
        SUM('DecomposedHoldings'[MV]),
        ALL('DecomposedHoldings')
    )
RETURN
    DIVIDE(SUM('DecomposedHoldings'[MV]), TotalMV, 0)

 

3. If the semantic model's size is an issue, filter the data in Power Query:

FilteredHoldings = Table.SelectRows(
    Source,
    each [HoldingDate] >= Date.AddMonths(DateTime.LocalNow(), -24)
)

 

Hope this helps!


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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