+ Reply to Thread
Results 1 to 2 of 2

Excel Power query Custom function creation and application

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-03-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    777

    Excel Power query Custom function creation and application

    Hello,
    I've started creating custom functions and need some advice regarding a good overview of them an parameters, a lot of tutorials
    show one method or another without explaining different methods of invoking etc.

    I've created a very (very ) simple (find attached ) and two functions whihc do the same thing; (price - cost ) * qty.

    One was created by adding a custom columns and then converting the query to a function (named by step ) the other was written directly into
    the editor ( named Mcode ) They both work but have to be invoked in different ways and I'd like to find a definitive guide to the rules.

    I've also tried using the 'add custom column' and then function (source), which either returns the entire table for one and an error for the other.

    Any guidance greatfuly received.

    Richard.
    Attached Files Attached Files

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Excel Power query Custom function creation and application

    There's no "one size fits all", here - you can create custom functions in pretty much any way you choose, to serve your purpose. Your two functions can achieve the same output, but one performs a calculation when presented with number, the other performs a table transformation.

    If you want to add a column to a table using a custom function, then your "ProfitMCode" function is about right (although I would recommend strongly typing your inputs);

    let
        ProfitMcode = ( Cost as number, Qty as number, Price as number) => (Price - Cost  ) * Qty
    in
       ProfitMcode

    You can then use this function in other queries - for example, adding a profit column:

    let
        Source = Excel.CurrentWorkbook(){[Name="tryone"]}[Content],
        #"Added Profit Column using Function" = Table.AddColumn(Source, "Profit", each ProfitMcode([Cost], [Qty], [Price]), Currency.Type)
    in
        #"Added Profit Column using Function"


    Your "ProfitBySteps" function doesn't really offer any value over simply adding the Add Column step to your 'test' query - unless you wanted to repeat that table function to multiple tables. You can invoke that by passing the entire table:

    let
        Source = Excel.CurrentWorkbook(){[Name="tryone"]}[Content],
        #"Apply Function to Source" = ProfitBySteps(Source)
    in
        #"Apply Function to Source"

    Perhaps, to add more value to your understanding, you can describe more complex use cases for which you want to create functions?
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 11-17-2021, 08:35 AM
  2. [SOLVED] Excel Power Query 'Custom Column"
    By Dicken in forum Office 365
    Replies: 4
    Last Post: 10-26-2021, 09:29 AM
  3. [SOLVED] Power Query/Power Pivot Conditional Column creation & chart
    By mz_h in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-12-2020, 06:07 PM
  4. Sub-Forum for Excel Power Tools (Power Query, Power Pivot & Power BI)
    By chullan88 in forum Suggestions for Improvement
    Replies: 10
    Last Post: 06-28-2018, 02:25 PM
  5. Power Query - Invoke Custom Function
    By leukep in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-12-2017, 02:18 PM
  6. [SOLVED] Formula Creation with Power Query
    By Bhalbert in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-05-2016, 01:11 PM
  7. [SOLVED] Application creation query
    By AndyE in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-14-2013, 03:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1