• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Custom Function - choosing any column to filter|Power Query|Excel Forum|My Online Training Hub

You are here: Home / Custom Function - choosing any column to filter|Power Query|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumPower QueryCustom Function - choosing any colu…
sp_PrintTopic sp_TopicIcon
Custom Function - choosing any column to filter
Avatar
El Riz
Member
Members

Financial Modelling
Level 0
Forum Posts: 11
Member Since:
August 30, 2020
sp_UserOfflineSmall Offline
1
August 30, 2020 - 1:29 am
sp_Permalink sp_Print

Hi people! Could you share your way of writing parameter for choosing any column to filter necessary rows in the table inside a custom function.

sp_AnswersTopicSeeAnswer See Answer
Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
August 31, 2020 - 9:10 am
sp_Permalink sp_Print

Hi,

I'm not really sure what you mean by 'your way of writing parameter', maybe you could explain more clearly with an example?

However, if you are asking how to write a function and supply parameters to it, that is, send values from a column to it, see the attached file.

What you need to do is:

1. Write your function

2. Add Column -> Invoke Custom Function -> Select function and input column (parameter)

Regards

Phil

Avatar
El Riz
Member
Members

Financial Modelling
Level 0
Forum Posts: 11
Member Since:
August 30, 2020
sp_UserOfflineSmall Offline
3
September 1, 2020 - 6:14 am
sp_Permalink sp_Print

Hi Philip, thx for reply,

I mean I'm writing custom function and the part of it is selecting necessary rows - = Table.SelectRows(#"Changed Type", each ([Country] = "YourValue")) - however when I provide table the column to be filtered can be different, in this table it is a country, in another table it can be a City etc

for example (sourceTable as table, YourValue as text) => here is two parameters but I'm thinking about the third parameter to make [Country] column name dynamic - it must be any column I choose not only Country  
Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
September 1, 2020 - 9:45 am
sp_Permalink sp_Print

Hi,

It would be much easier if you supplied a detailed example with data in a file.  A couple of lines taken out of context are hard to relate to.

If the column you want to filter can be different each time, how do you know how to refer to that column?  If you don't know how to refer to it, how do you supply that identifier to your function?  You'll need to know either the name or the position of the column.

You could just use

 (sourceTable as table, ColName as text, YourValue as text) => 

I'm also not clear on why you appear to be writing a function to replicate an existing function.  Maybe I'm missing something but as I said at the start, a clear example with data would help me out.

Regards

Phil

Avatar
El Riz
Member
Members

Financial Modelling
Level 0
Forum Posts: 11
Member Since:
August 30, 2020
sp_UserOfflineSmall Offline
5
September 1, 2020 - 11:19 pm
sp_Permalink sp_Print sp_EditHistory

Here is code

(Source as table,Value as text)=>
let
#"Grouped Rows" = Table.Group(Source, {"City"}, {{"Total", each List.Sum([Sales]), type number}}),
#"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([City] = Value)),
Custom1 = #"Filtered Rows"{0}[Total]
in
Custom1

 

This code will run only on tables with City and Sales columns, but what is a way to do it dynamic to group other column if needed , I can type Column as text to put inside Table.Group function inside a list {"City"} but it will not work with each ([City] = Value) part because of square brackets I suppose, how to refer to list in square brackets in custom function?

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
6
September 2, 2020 - 1:18 pm
sp_Permalink sp_Print

Hi,

I'm having trouble understanding what you want to do but I think it is something like this

(Source as table, Col as text, Value as text)=>
let
#"Grouped Rows" = Table.Group(Source, Col, {{"Total", each List.Sum([Sales]), type number}}),
#"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each (Record.Field(_, Col) = Value)),
Custom1 = #"Filtered Rows"{0}[Total]
in
Custom1

Using this you can specify either the City or the Country and the function will work out the % total of sales by City/Country.  See attached.

Regards

Phil

sp_AnswersTopicAnswer
Answers Post
Avatar
El Riz
Member
Members

Financial Modelling
Level 0
Forum Posts: 11
Member Since:
August 30, 2020
sp_UserOfflineSmall Offline
7
September 2, 2020 - 5:14 pm
sp_Permalink sp_Print

Thank you very much, it is what I meant. 

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
8
September 3, 2020 - 8:11 am
sp_Permalink sp_Print

No worries 🙂

Avatar
El Riz
Member
Members

Financial Modelling
Level 0
Forum Posts: 11
Member Since:
August 30, 2020
sp_UserOfflineSmall Offline
9
September 3, 2020 - 10:49 pm
sp_Permalink sp_Print

I also did it more dynamic if other users will search for solution it can be helpful for them:

(Source as table, Column as text, Value as text, ColumnToSum as text )=>
let
#"Grouped Rows" = Table.Group(Source, Column, {{"Total", each List.Sum(Table.Column(_, ColumnToSum)), type number}}),
#"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each (Record.Field(_, Column) = Value)),
Custom1 = #"Filtered Rows"{0}[Total]
in
Custom1

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Richard Mander, Peter Hoffman, Marco Marques, Natasha Smith
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
vexokeb sdfg
John Jack
Malcolm Toy
Ray-Yu Yang
George Shihadeh
Naomi Rumble
Uwe von Gostomski
Jonathan Jones
drsven
Annie Witbrod
Forum Stats:
Groups: 3
Forums: 24
Topics: 6212
Posts: 27236

 

Member Stats:
Guest Posters: 49
Members: 31888
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.