Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Call a function on a table, having only the name of the table

Hi all,

I have this table TableOfTable:

NameOfTableParam1Param2
t1p1p2
t2p3p4

 

I have a function written in Power Query M, myFunction, which is working correctly: the input parameters for the function are:

  1. myTable type table
  2. parameter1 type string
  3. parameter2 type string

I want to write another function in power query who call myFunction for each record of the table above, passing the right parameter. So i wrote this code:

 

let

   i = 0,
   rec = HierarchiesTable{i},
   myTable= Record.Field(rec, "NameOfTable"),
   parameter1 = Record.Field(rec, "Param1"),
   parameter2= Record.Field(rec, "Param2"),
   Output = myFunction(myTable, parameter1 , parameter2)
in Output

 

What is the problem? When i call myFunction passing the first parameter, i'm passing a string with the name of the table, not the table on which i want to apply the function! How can i resolve this? How can i cycle over the record of TableOfTable?

Thank you

  • Anonymous's avatar
    Anonymous
    6 years ago

    #Edited

     

    As first guess, I'd give it a try with this

     

     

    List.Transform(Table.toRecords(tableofTable), each MyFunction(Expressiona.Evaluate([NameOfTable],#shared), [Param1],[Param2]))

     

     

    If you would be more specific, could get more specific answer (peraphs)

     

     

    PS

    After reading better the post, I suggest to give a look at this

     

    using a table anme as parameter 

     

     

     

    let
        conc=(utab,ntab)=>
        let
        lstabs=List.Transform({1..ntab}, each "w"&Text.From(Number.From(Text.End(utab,2))-ntab+_-1)),
        Cws=Table.TransformRows(Expression.Evaluate(utab,#shared), (ru)=>Record.Combine(List.Transform(lstabs, each Expression.Evaluate(_,#shared){[Job=ru[Job]]})&{ru}))
        in
        Table.FromRecords(Cws)
    in
       conc

     

     

3 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    #Edited

     

    As first guess, I'd give it a try with this

     

     

    List.Transform(Table.toRecords(tableofTable), each MyFunction(Expressiona.Evaluate([NameOfTable],#shared), [Param1],[Param2]))

     

     

    If you would be more specific, could get more specific answer (peraphs)

     

     

    PS

    After reading better the post, I suggest to give a look at this

     

    using a table anme as parameter 

     

     

     

    let
        conc=(utab,ntab)=>
        let
        lstabs=List.Transform({1..ntab}, each "w"&Text.From(Number.From(Text.End(utab,2))-ntab+_-1)),
        Cws=Table.TransformRows(Expression.Evaluate(utab,#shared), (ru)=>Record.Combine(List.Transform(lstabs, each Expression.Evaluate(_,#shared){[Job=ru[Job]]})&{ru}))
        in
        Table.FromRecords(Cws)
    in
       conc

     

     

  • Jimmy801's avatar
    Jimmy801
    Icon for Community Champion rankCommunity Champion

    Hello Anonymous 

     

    you have to call your function. So wy not add a new column and passing the three parameters.

    =Table.AddColumn(PreviousStep, "New Column", each YourFuntion([NameOfTable],[Param1],[Param2])

     

    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