Problem Facing while learning Custom Functions in Power Query

Kartick0075

New Member
Joined
Jan 9, 2020
Messages
18
Office Version
  1. 2019
Platform
  1. Windows
Dear Respected All,
I'm learning Power Query from ExcelIsFun on YouTube. I've learnt a lot of things from Mike Sir. He is truly a genius. I've come to know about this Forum from him.

But, I got stuck while following 2 videos from this channel. (Link 001 :
and Link 002 :
). I failed to understand even after trying lots of times, why did he use "(x) =>" in "Add Column" Dialog Box and delete "each" Word before "MAT"? Please help me. I'm attaching the screenshots below.....
 

Attachments

  • IMG_20210804_224415.jpg
    IMG_20210804_224415.jpg
    183.8 KB · Views: 10
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
When you use the keyword 'each' you can refer to that variable with the shorthand character _. You can omit each by naming the variable like in your example as (x)=>. If you have nested formulas that use the _ character you get stuck in the scope of the current function you're in and can't refer to the outer function. Naming your variables gets around this problem and makes the code clearer and more understandable.
 
Upvote 0
When you use the keyword 'each' you can refer to that variable with the shorthand character _. You can omit each by naming the variable like in your example as (x)=>. If you have nested formulas that use the _ character you get stuck in the scope of the current function you're in and can't refer to the outer function. Naming your variables gets around this problem and makes the code clearer and more understandable.
Thank you so much Sir for your information. But, Would you kindly explain what do you mean by "Scope"?
 
Upvote 0
RankFunction = (tabletorank as table) as table =>
let
SortRows = Table.Sort(tabletorank,{{"Sales", Order.Descending}}),
AddIndex = Table.AddColumn(SortRows, "newcolumn", each table[column name]+1)
in
AddIndex,

see above how the first function declares the variable as 'table' then below we use the each _. If we used each _ instead of table =>, then there would be no way to reference the table. Because you'd be trying to use _ for both. But _ only refers to the current function you're writing. That's what I mean by scope.

Here's all you need to really know about it. There is really no difference between the two methods. Using each _ is fine when you're using 1 function per line of code, or step. Otherwise, if you have nested functions, i.e. a function within a function, you might need to ditch the each _ and use variable names.
 
Upvote 0
Solution
RankFunction = (tabletorank as table) as table =>
let
SortRows = Table.Sort(tabletorank,{{"Sales", Order.Descending}}),
AddIndex = Table.AddColumn(SortRows, "newcolumn", each table[column name]+1)
in
AddIndex,

see above how the first function declares the variable as 'table' then below we use the each _. If we used each _ instead of table =>, then there would be no way to reference the table. Because you'd be trying to use _ for both. But _ only refers to the current function you're writing. That's what I mean by scope.

Here's all you need to really know about it. There is really no difference between the two methods. Using each _ is fine when you're using 1 function per line of code, or step. Otherwise, if you have nested functions, i.e. a function within a function, you might need to ditch the each _ and use variable names.
Thank you Sir. You helped me understanding the concept finally. May God bless you!
 
Upvote 0
Hope it helped. It was harder to explain than I thought it would be. If you have any questions in the future, let me know and I'll try to help.
 
Upvote 0
Hope it helped. It was harder to explain than I thought it would be. If you have any questions in the future, let me know and I'll try to help.
Sir, I've another question, is it possible to do Partial Match Lookup accurately in Power Query? In Excel, it's very easy.

Suppose, you have a Source Table with Company-wise Monthly Revenue. Now in the Destination or Lookup Table, you have to extract the Revenue from the Source Table based on the Partial Company Names in Lookup Table.
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,961
Members
452,539
Latest member
delvey

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