Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Custom Function in Query

I Can do this task using modelling and DAX.

 

But i want it using "invoke Custom Functions".

 

Requirement is i have key column as below

 

Key

Id-1

Id-10

Id-100

 

So i want to display only number and remove "ID-" from column

 

Thanks,

Pravin

  • Anonymous's avatar
    Anonymous
    6 years ago

    It works for me. You have to create a function in Power Query and then change its body accordingly.

     

    Best

    D

40 Replies

  • Hi. Sure. You can go to "Edit Queries" or "Transform Data". In that screen you can add columns or do transformations like this one.

     

    Click on the ID column, go to "transform" tab and select Extract->After delimiter. Then write "-" and accept. Then  you have your result.

    If you want to learn some M this is the function to do it:

    = Table.TransformColumns(#"LastStep", {{"ColumnName", each Text.AfterDelimiter(Text.From(_), "-"), type text}})

    Replace LastStep and ColumnName with your data.

     

    Regards, 

    • Anonymous's avatar
      Anonymous
      Not applicable

      This functions works perfect !!!!

       

      So the thing is 

      let say i have 5 tables all have key1,..key5.

       

      actual requirement is  like i need to create one function that removes "Id -" part. every time i just need to click on invoke function  and select function name and Column name.

      How can we do it?

      I don't know M code or how to invoke function

       

      Thanks,

      Pravin

      • ibarrau's avatar
        ibarrau
        Super User

        Custom functions are for having in one step multiple transformations. In your case, you want a simple transformation that is in one step. Then it is unnecessary to do it that way. If you use this code in one column it will refresh always for that column. If you need it for another column table you can just add it again.

         

        I guess I'm not sure what you need now. Did my suggestion solve your issue?

        Regards,

  • v-lionel-msft's avatar
    v-lionel-msft
    Community Support

    Hi Anonymous ,

     

    Even if you use "invoke Custom Functions", still create a function for each table, Unless you merge all your tables into one table.

    You can refer to the code:

    let
        Source = () => let
            Source = Excel.Workbook(File.Contents("C:\Users\xxx\Desktop\x4.xlsx"), null, true),
            Sheet6_Sheet = Source{[Item="Sheet6",Kind="Sheet"]}[Data],
            #"Changed Type" = Table.TransformColumnTypes(Sheet6_Sheet,{{"Column1", type text}}),
            #"Extracted Text After Delimiter" = Table.TransformColumns(#"Changed Type", {{"Column1", each Text.AfterDelimiter(_, "-"), type text}})
        in
           #"Extracted Text After Delimiter"
    in
        Source

     

    So, the most effective way is to refer to ibarrau 's answer.

     

    Best regards,
    Lionel Chen

    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 v-lionel-msft , ibarrau , amitchandak 

       

      Thanks For your solutions.

       

      I don't know this functionality is there or not. If not then microsoft need to add some functionality like this which is mostly available in programing languages. we just need to create one funtion for all to avoid many steps in each table.

       

       

      Let say, i have done some transformation like removing delimeters then taking right 2 character from text and then again i have done some 2 3 steps on this.

      We have 4 5 tables and each table we have same requirement. So its better to have one function for all and you need to just call function with parameter to avoild repeating steps in each query.

       

      Thanks & regards,
      Pravin Wattamwar
      www.linkedin.com/in/pravin-p-wattamwar

      If I resolve your problem Mark it as a solution and give kudos.

       

       

      • Anonymous's avatar
        Anonymous
        Not applicable
        If you have several tables with the same structure, you can always create a UNION of the tables, apply all the transformations you want to the big table and then split the table into its constituents again. This is how you reuse logic.

        Best
        D