Power query custom functions

dicken

Active Member
Joined
Feb 12, 2022
Messages
292
Office Version
  1. 365
Platform
  1. Windows
Hi,
This is just about general advice rather than a specific case, but when writing custom function,
when do I need to use the variables first and then let and when within the the let statement;

l
Excel Formula:
let
   RunningTotal = (Avlaue, Aindex) =>
   List.Sum(
       List.FirstN( Avlaue, Aindex )
   )
    in RunningTotal

as opposed to somthing like this which pomotes the first two row to headers;

Excel Formula:
(Rname as table )=>
let
    Custom1 = List.Zip( {Table.ColumnNames(Rname) ,
List.Transform( Table.ToColumns( Table.FirstN(  Rname,2)),(_)=> Text.Combine(_," ")) } ),
    Custom2 = Table.RenameColumns(  Table.RemoveFirstN(Rname,2),Custom1 )
in
    Custom2

I'm looking more for general advice on how to write functions, a lot of videos show how to do a specific thing, so
if anyone knows of a good source of information, please pass it on.

Richard.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
There is no "declaring" variables in M Code. However, a variable must be assigned a value before it's used. While it does matter where the variable is declared in that a variable can be created after a second or subsequent let, but is then only available to that and subsequent let sections as far as I know. At least that's how other languages like it would work.
Power Query:
You can post Power Query M Code clicking the M icon two icons to the right of the XLS icon you used.
 
Upvote 0
There is no "declaring" variables in M Code. However, a variable must be assigned a value before it's used. While it does matter where the variable is declared in that a variable can be created after a second or subsequent let, but is then only available to that and subsequent let sections as far as I know. At least that's how other languages like it would work.
Power Query:
You can post Power Query M Code clicking the M icon two icons to the right of the XLS icon you used.
Fair enough, but if you don't use the term variable, what do you use, so in the example I posted;

Excel Formula:
(Rname as table )=>
let
    Custom1 = List.Zip( {Table.ColumnNames(Rname) ,
List.Transform( Table.ToColumns( Table.FirstN(  Rname,2)),(_)=> Text.Combine(_," ")) } ),
    Custom2 = Table.RenameColumns(  Table.RemoveFirstN(Rname,2),Custom1 )
in
    Custom2

Custom1 is used in Custom2, what do you call it?

RD
 
Upvote 0
It's a matter of preference. These three custom functions are interchangeable:

Power Query:
(a as number, b as number) as number => a + b
Power Query:
(a as number, b as number) as number=>
let
    Result = a + b
in
    Result
Power Query:
let
    AddItems = (a as number, b as number) as number => a + b
in
    AddItems

For very simple functions like the examples above, I like the short one liner format with no need for the let/in keywords. For more complicated functions I use the second format. and for defining and using a custom function in the same query I use the third format like this:

Power Query:
let
    Source = Table.FromColumns({{1,2,3},{4,5,6}}),
    AddItems = (a as number, b as number) as number => a + b,
    Result = Table.AddColumn(Source, "Total", each AddItems([Column1], [Column2]))
in
    Result
 
Upvote 1
Fair enough, but if you don't use the term variable, what do you use, so in the example I posted;

Excel Formula:
(Rname as table )=>
let
    Custom1 = List.Zip( {Table.ColumnNames(Rname) ,
List.Transform( Table.ToColumns( Table.FirstN(  Rname,2)),(_)=> Text.Combine(_," ")) } ),
    Custom2 = Table.RenameColumns(  Table.RemoveFirstN(Rname,2),Custom1 )
in
    Custom2

Custom1 is used in Custom2, what do you call it?

RD
In your code Custom1 and Custom2 are the RESULTS of the functions in those lines. Typically those are tables. To create a variable just assign it a value such as
Power Query:
let
    Var1 = "This is Variable 1",
    Var2 = 1234
in
    Var2
This would simply display 1234
If the code was this:
Power Query:
let
    Var1 = "This is Variable 1",
    Var2 = 1234
in
    Var1
it would display
This is Variable 1
Get it?
 
Upvote 0
Solution

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top