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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Iterative/Recursive Calculations

Hi

 

I have a table of products and ingredients:

 

ProductIngredient

Initial price

AD2
AB0
BC0
CE2

 

As you can see, product A contains products D and B. D has a price value and B has no price initially. B contains C, C contains E and E has a starting value of 2. Now, looking at this you can easily tell, that the final price of product A will be 4. This is the problem I'm trying to solve using DAX formulas. 

 

To achieve this in Excel, I add two new columns to the table:

 

ProductIngredientInitial priceLookup valueFinal price
AD2

= IFERROR( INDEX( TableProductsIntermediate[Price]; MATCH([@Ingredient]; TableProductsIntermediate[Product]; 0)); 0)

=[@[Initial price]] + [@[Lookup value]]
AB0  
BC0  
CE2  

 

Here, TableProductsIntermediate is a table that contains all distinct/unique products:

 

ProductPrice
A=SUMIFS( TableProducts[Final price]; TableProducts[Product]; [@Product])
B 
C 

 

Excel somehow manages to run this iteratively/recursive, and gives me the following result:

 

ProductIngredientInitial priceLookup valueFinal price
AD202
AB022
BC022
CE202

 

ProductPrice
A4
B2
C2

 

Now, the way I've tried implementing this in DAX is as follows:

 

ProductIngredientInitial priceLookup valueFinal price
AD2

=LOOKUPVALUE( TableProductsIntermediate[Price]; TableProductsIntermediate[Product]; [Product])

=[Initial price] + [Lookup value]
AB0  
BC0  
CE2  

 

ProductPrice
A=CALCULATE( sum(TableProducts[Final price]); FILTER( 'TableProducts'; 'TableProducts'[Product]='TableProductsIntermediate'[Product] ))
B 
C 

 

This throws an circular dependency error. Any ideas on how this iterative/recursive calculation can be performed in DAX?

 

Best,

mdhor

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Please find the PQ solution attached. When you open, please go to Power Query and have a look at the function I created to get the price of a product. The assumption is that products are in the first column and only for them do you have to calculate the total price.

 

Original Data is the table with your original data.

Products with final prices is the table with final prices.

getTotalProductPrice is the function that does the recursion...

 

You have to check this procedure against a representative number of cases because I used your data and made assumptions which might not be true about the full set. You have to TEST.

 

Best

D

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Easiest way to do it is in Power Query or Python, not in DAX. In DAX dealing with parent-child hierarchies is awkward and the only way to handle this is to flatten the hierarchy but for that you have to know the maximum depth level in advance.

DAX has not been designed to handle such situations. Recursion is not supported. Only a version called sideways recursion is but it does not apply to your case.

PBI/DAX is not Excel.

Best
D
Anonymous
Not applicable

Thank you for your answer @Anonymous

 

How would a solution to this simple example look like in Power Query? I'm trying to avoid having to export the data and process it with other software.

Anonymous
Not applicable

I'm trying to create a PQ solution but it's not going to be for the faint of heart...

Best
D
Anonymous
Not applicable

Please find the PQ solution attached. When you open, please go to Power Query and have a look at the function I created to get the price of a product. The assumption is that products are in the first column and only for them do you have to calculate the total price.

 

Original Data is the table with your original data.

Products with final prices is the table with final prices.

getTotalProductPrice is the function that does the recursion...

 

You have to check this procedure against a representative number of cases because I used your data and made assumptions which might not be true about the full set. You have to TEST.

 

Best

D

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors