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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
SaaM
Helper II
Helper II

Refactor a function

Hello,

 

I am trying to refactor this function that I apply on 10 differents columns.

 

fnConcatenate = (tbl as table) => Table.AddColumn(tbl, "field_1_combined", each if [id]>[linenumber] then Text.Combine(Table.SelectRows(tbl, (sel)=> sel[key] = [key] and sel[id]>= [id])[#"field_1"]) else [#"field_1"]),


fnConcatenate = (tbl as table) => Table.AddColumn(tbl, "field_2_combined", each if [id]>[linenumber] then Text.Combine(Table.SelectRows(tbl, (sel)=> sel[key] = [key] and sel[id]>= [id])[#"field_2"]) else [#"field_2"]),
 
... 

fnConcatenate = (tbl as table) => Table.AddColumn(tbl, "field_10_combined", each if [id]>[linenumber] then Text.Combine(Table.SelectRows(tbl, (sel)=> sel[key] = [key] and sel[id]>= [id])[#"field_10"]) else [#"field_10"]),

 

Is it possible to have something like this

 

fnConcatenate = (tbl as table, field as text) => Table.AddColumn(tbl, "field" + "_combined", each if [id]>[linenumber] then Text.Combine(Table.SelectRows(tbl, (sel)=> sel[key] = [key] and sel[id]>= [id])[#"field"]) else [#"field"]),

?

 

Thanks

 

Kind regards

Saam

1 ACCEPTED SOLUTION
Anonymous
Not applicable

try

 

 

 

fnConcatField = (tbl as table, field as text) => Table.AddColumn(tbl, field & "_combined", each 
                                if [id]>[linenumber] then Text.Combine( Table.Column(Table.SelectRows(tbl, (sel)=> sel[key] = [key] and sel[id]>= [id]), field)) else Record.Field(_, field))

 

 

try something like this (I haven't tested it)  

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

try

 

 

 

fnConcatField = (tbl as table, field as text) => Table.AddColumn(tbl, field & "_combined", each 
                                if [id]>[linenumber] then Text.Combine( Table.Column(Table.SelectRows(tbl, (sel)=> sel[key] = [key] and sel[id]>= [id]), field)) else Record.Field(_, field))

 

 

try something like this (I haven't tested it)  

lbendlin
Super User
Super User

Column names can be created dynamically

 

fnConcatenate = (tbl as table, field as text) => Table.AddColumn(tbl, field + "_combined", 

 

but they cannot be referenced (easily) dynamically. Have you considered using Expression.Evaluate() for this?

I never used Expression.Evaluate()

I will look for this approach

 

Thanks

 

Kind regards,

Saam

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.