Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

How to slice a list

In the absence of a direct function for slicing a list, I found several ways, combining various library functions, to get the result, using List.Accumulate, for example, or List.Transform.

but for this last function I encountered a difficulty that I have overcome, but it is not clear to me how.

The signature of the function is

List.TransformMany(list as list, collectionTransform as function, resultTransform as function) as list

but using this form, I get an error message about a value that cannot be converted to a list

 

 

 

let
lst={10..20},
idx={2,3,5},

sel=List.TransformMany(idx, each lst{_},(x,y)=>y)

in

sel

 

 

 

 

instead using this form, you get the desired result:

 

 

 

let
lst={10..20},
idx={2,3,5},

sel=List.TransformMany(idx, each {lst{_}},(x,y)=>y)

in

sel

 

 

 

12
13
15

.

can anyone explain why?

5 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    here another "experiment" with the funcion, to get the result more directly

     

     

     

    let
    lst={10..20},
    idx={2,3,5},
    sel=List.TransformMany({0}, each idx,(x,y)=>lst{y})
    in
    sel

     

     

     

     

     

     

    perhaps the documentation of the function that is provided in   https://docs.microsoft.com/en-us/powerquery-m/list-transformmany  should be revised or it is I who do not understand the explanation well.

     

    You could use any list instead of {0}.

  • dax's avatar
    dax
    Community Support

    Hi Anonymous ,

    I think {lst{_}} seems to convert the value to list, then you could use idx  as  index to get corresponind value in lst List.

    Best Regards,
    Zoe Zhi

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi dax, yes I  know tha {something} convert the "something" as value in "something"  as list of only one element.

       

      Is for that reason that I changed the script from the first form to the second form, as I in the first case get the eror message (something like a "value wich can't be read as list" . For this reason I thought that the function somewhere expected a list and not a value and so I made several attempts until I found the second form).

       

      My question is about the the documentation of the function, which seems to me to be misleading.

      But, I'm not very practical with the English language, so I may have misunderstood the explanation.

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        From this example, it can be understood that the function makes a sort of Cartesian product of the two lists

         

         

        let lst1={1..5}, lst2={-5..-1}
        in Table.FromRecords(List.TransformMany(lst1, each lst2,(x,y)=>[x=x,y=y]))

         

         

        So using this property, choosing appropriate subsets of the Cartesian product to which to apply the appropriate transformations, many things can be done.