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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Yaroslav__D
New Member

Calculate Iterate Levels in M

Hi  ALL! 
I tried to calculate in power query levels for my accounts to calculate all of them later .
I used this code to do it , but is`s don`t work , maybe someone know what could be wrong? 

let
    MappingRules = #"MappingRules",  

    Level1 = Table.AddColumn(
        MappingRules, 
        "Level", 
        each if List.ContainsAll(Balance[FinalAccount], { [SourceAccount] }) then 1 else null, 
        Int64.Type
    ),


    CheckIfCompleted = List.Contains(Level1[Level], null),


    IterateLevels = List.Generate(
        () => [Table = Level1, LevelNum = 1],  
        each CheckIfCompleted,  
        each 
            let
                CurrentTable = _[Table],  
                NextLevelNum = _[LevelNum] + 1,  


                UpdatedTable = Table.TransformColumns(
                    CurrentTable,
                    {"Level", each 
                        if _ <> null then _ 
                        else if List.ContainsAll(CurrentTable[ComponentAccount], 
                                                 Table.SelectRows(CurrentTable, each [Level] = _[LevelNum])[SourceAccount]) 
                             then NextLevelNum 
                             else null, 
                        Int64.Type}
                ),


                UpdatedCheck = List.Contains(UpdatedTable[Level], null)
            in
                [Table = UpdatedTable, LevelNum = NextLevelNum, CheckIfCompleted = UpdatedCheck],  
        each _[Table]  
    ),


    FinalLevelsTable = List.Last(IterateLevels)
in
    FinalLevelsTable

 

9 REPLIES 9
v-priyankata
Community Support
Community Support

Hi @Yaroslav__D 

May I ask if you have resolved this issue? if not Please provide sample data so we can suggest a solution.

Thank you.

Hi , no, this issue not resolved .
yes , sure ,  mappingrules looks literally like this 

TargetAccountSourceAccountOperator
P003099P001010+
P003099P002010+
P003099P002030+
P008999P003099+
P008999P004050+
P013099P013010+
P013099P013020+
P013099P013025+
P013099P013030+
P013099P013035+
P013099P013040+
P013099P013050+
P013099P013062+
P013099P013064+
P013099P013070+
P013099P013080+
P013099P013095+
P014099P014010+


in Balance we have similar list where we find our Source Account , to give level 1 or null

Thanks 

consider using Expression.Evaluate

Hi @Yaroslav__D 

Thank you for reaching out to the Microsoft Fabric Forum Community. sorry for delay.

Idea to use Expression.Evaluate is a really good one or Please try the code below. it may helps you. @lbendlin Thanks for your inputs. 
If the issue persists, feel free to reach out to us. We are happy to assist you.

Table.TransformRows(
currentTable,
(row) =>
if row[Level] <> null then row
else
let
match = Table.SelectRows(DependencyMap, each [TargetAccount] = row[Account]),
sources = if Table.IsEmpty(match) then {} else match{0}[Sources],
allResolved = List.IsEmpty(List.Difference(sources, assignedAccounts))
in
if allResolved then Record.TransformFields(row, {{"Level", each currentLevel + 1}})
else row
)

Hi @Yaroslav__D 

I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions.

Hi @Yaroslav__D 

Hope everything’s going smoothly on your end. We haven’t heard back from you, so I wanted to check if the issue got sorted. If yes, marking the solution would be awesome for others who might run into the same thing.

 

Hi @Yaroslav__D 
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. Please contact the community for any issues you may have. We are happy to assist you.

v-priyankata
Community Support
Community Support

Hi @Yaroslav__D,

We are grateful for your participation in the Microsoft Fabric Community forum.

 

Did your issue get resolved? If not, as @lbendlin suggested, "Please provide sample data so we can suggest a solution. We tried your query but got the error: 'Expression.Error: The name 'MappingRules' wasn't recognized.' Kindly provide more insights on this issue.

Thanks.

lbendlin
Super User
Super User

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
Do not include sensitive information. Do not include anything that is unrelated to the issue or question.
Please show the expected outcome based on the sample data you provided.

Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...
Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June FBC25 Carousel

Fabric Monthly Update - June 2025

Check out the June 2025 Fabric update to learn about new features.