Forum Discussion

Paul_W_W's avatar
Paul_W_W
Regular Visitor
5 years ago
Solved

Changing the value in a list

Hello All   I want to create a two dimension List array (eg Lookup = {{1..10},{1..10}} and then be able to change a specific entry.   I have tried Lookup{1}{4} = 99 but PQ does not like it. I can...
  • Jimmy801's avatar
    5 years ago

    Hello Paul_W_W 

     

    as List.PositionOf is not working, because if yuo have double entries, it won't work. So the only way is to use List.Positions and List.Zip to add a position number to every entry and then use List.Tranform to change your required entry. Here a function that does that for you. This function takes a list, firstlevel, second level and a new entry. Check it out and give feedback

    //fxChangeEntry2LevelList
    (List as list, Level1 as number, Level2 as number, NewEntry as any)=>
    let
    
    
        Source = List,
        TransformFirstLevel = List.Positions(Source),
        TransformSecondLevel = List.Transform(List.Buffer(Source),each List.Zip({_, List.Positions(_)})),
        Zip = List.Zip({TransformSecondLevel,TransformFirstLevel}), 
        Transform = List.Transform
        (
            Zip,
            (listlevel1)=> if listlevel1{1}= Level1 then 
            List.Transform
            (
                listlevel1{0},
                (listlevel2)=> if listlevel2{1}= Level2 then NewEntry else listlevel2{0}
               
            )
    
            else
            List.Transform
            (
                listlevel1{0},
                each _{0}
            )
        )
    in
        Transform

     

    Copy paste this code to the advanced editor in a new blank query. Give the function a name and try to apply ti

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy