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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Thomas_123
New Member

Replace fixed column names in a function

Hello

 

I try to replace fixed column names in a function to reuse the function in other queries.

Unfortunately I can't get any further with two replacements.

 

The function calculates an average based on two given columns (YearColumn, ValueColumn) and a number of years looking backwards (Years_back).

 

In lines 10 to 12 I was able to replace the original fixed column names.

The issues are in row 13 "row [YearColumn]" and row 14 "[ValueColumn]". If I use the original fixed names than it is working.

 

 

 

let

    MyFunction = (Table as table, NewColumn as text, YearColumn as text, ValueColumn as text, Years_back as number) =>
    
    Table.AddColumn(Table,NewColumn,
                            (x) => List.Average(
                                                Table.SelectRows( Table,
                                                                 (row) => List.Contains( 
                                                                                        List.MaxN( 
                                                                                                List.Select( Table.Column( Table, YearColumn ), 
                                                                                                                (y) => y <= x {YearColumn} ), 
                                                                                                                Years_back), 
                                                                            row [YearColumn]  )
                                                                ) [ValueColumn]
                                                )
                    )

in 

MyFunction

 

 

Here comes the original embedded function.

    Custom = Table.AddColumn(Prev_Step,"AVG",
                                (x) => List.Average(
                                                    Table.SelectRows( Prev_Step,
                                                                    (row) => List.Contains( 
                                                                                        List.MaxN( 
                                                                                                    List.Select(Prev_Step[Year], 
                                                                                                    (y) => y <= x[Year]),
                                                                                                    5),
                                                                                row[Year])
                                                                    )[#"Values"]
                                                    )
                                        )

 

Best regards

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @Thomas_123 

You can update those pieces of code using

Table.Column( ... , ValueColumn )

and

Record.Field ( row, YearColumn) 

 

The full M code (reformatted slightly) would be:

let
  MyFunction = ( Table as table, NewColumn as text, YearColumn as text, ValueColumn as text, Years_back as number ) =>
    Table.AddColumn(
      Table,
      NewColumn,
      (x) =>
        List.Average(
          Table.Column(
            Table.SelectRows(
              Table,
              (row) =>
                List.Contains(
                  List.MaxN(
                    List.Select(Table.Column(Table, YearColumn), (y) => y <= x{YearColumn}),
                    Years_back
                  ),
                  Record.Field(row, YearColumn)
                )
            ),
            ValueColumn
          )
        )
    )
in
  MyFunction

Does this work for you?


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

2 REPLIES 2
OwenAuger
Super User
Super User

Hi @Thomas_123 

You can update those pieces of code using

Table.Column( ... , ValueColumn )

and

Record.Field ( row, YearColumn) 

 

The full M code (reformatted slightly) would be:

let
  MyFunction = ( Table as table, NewColumn as text, YearColumn as text, ValueColumn as text, Years_back as number ) =>
    Table.AddColumn(
      Table,
      NewColumn,
      (x) =>
        List.Average(
          Table.Column(
            Table.SelectRows(
              Table,
              (row) =>
                List.Contains(
                  List.MaxN(
                    List.Select(Table.Column(Table, YearColumn), (y) => y <= x{YearColumn}),
                    Years_back
                  ),
                  Record.Field(row, YearColumn)
                )
            ),
            ValueColumn
          )
        )
    )
in
  MyFunction

Does this work for you?


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Hello Owen

 

Great! It is working. You saved my Sunday.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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