Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Lucian
Responsive Resident
Responsive Resident

How to pass column name as parameter into a function

Hello,

I need some help understanding how to pass a column name as parameter into a custom function.

 

I have a table (Accounts) from which I need a specific column (No) using a function that will filter that column.

@wdx223_Daniel was kind enough to help me getting the M code for filtering information in that table into another post: Power Query Select rows between two TEXT values

 

Now I have (partially) failed to create a custom function from his code, because the table name and column name will not be the same every time.

 

The code should be like this:

let
  func = (srcTable as table, srcColumn as text, strStartWith as text, strEndWith as text) as list =>
    let
      Source = srcTable,
      Pas1 = Table.Column(
        Table.SelectRows(
          Source,  
          each
            let
              fx = (txt1, txt2) =>
                List.Count(Text.Split(txt1, ".")) = List.Count(Text.Split(txt2, ".")) 
                and 
                Text.Remove(txt1, ".") <= Text.Remove(txt2, ".")
            in
              fx(strStartWith, srcColumn) and fx(srcColumn, strEndWith)
          ),
        srcColumn
      )
    in
      Pas1
in
  func

 

My problem seems to be on the following line:

fx(strStartWith, srcColumn) and fx(srcColumn, strEndWith)

 

If instead of the parameter srcColum, represeting the source column from the source table (srcTable) that I have to filter & return as list I will put the real name of the column ([No]) as this:

fx(strStartWith, [No]) and fx([No], strEndWith)

...the code will work perfectly, and will return the expected data.

 

So, Is there a way to pass the column name as parameter?

 

Kind Regards,

Lucian

 

1 ACCEPTED SOLUTION
Lucian
Responsive Resident
Responsive Resident

After some more digging, I find the answer => Record.Field(_,srcColumn).

So, changing the following line from:

 

fx(strStartWith, srcColumn) and fx(srcColumn, strEndWith)

 

to

 

fx(strStartWith, Record.Field(_,srcColumn)) and fx(Record.Field(_,srcColumn), strEndWith)

 

Will  make the code work.

Kind Regards,

Lucian

View solution in original post

2 REPLIES 2
Lucian
Responsive Resident
Responsive Resident

After some more digging, I find the answer => Record.Field(_,srcColumn).

So, changing the following line from:

 

fx(strStartWith, srcColumn) and fx(srcColumn, strEndWith)

 

to

 

fx(strStartWith, Record.Field(_,srcColumn)) and fx(Record.Field(_,srcColumn), strEndWith)

 

Will  make the code work.

Kind Regards,

Lucian

Thanks, this solved a problem for me 🙂

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors