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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
SaaM
Helper II
Helper II

Concatenate column with condition Recursive way or any other option?

Hi All,

I need your help solving a problem.

 

I am trying to calculate a new column "Concatenated Serial Numbers" with concatenating other values from "Serial Number" with some conditions:

         for each row if column > index I concatenate all the "Serial number" rows for an item to get my column "Concatenated Serial numbers

 

My hope is, that what I want to do if possible in Power Query using M (with a recursive function or not)

 

I have the following Table and I try to generate "Concatenated Serial Numbers":

 

Item  column index     Serial number       Concatenated Serial numbers

A32                            11111-22222-33333
A43 11111                           11111-22222-33333
A5422222                            22222-33333
A553333333333
B411410 2-3
B654341122-3
B6543654333
C10101CCC1CCC

Kinds regards,

Saam

2 ACCEPTED SOLUTIONS
Jimmy801
Community Champion
Community Champion

Hello @SaaM 

 

not to 100% clear how your column is calculated. I hope I guessed right.

Try to add a new column with this formula. The variable PreviousStep has to refer to your prior step

if [column]>[index] then Text.Combine(Table.SelectRows(PreviousStep, (sel)=> sel[Item] = [Item] and sel[column]>= [column])[Serial number], "-")
 else [Serial number]

here a complete example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIGYiMgVorVgQiYQAUNQeDQAri4KVTOCARQREHYGATAok4gdYaGYNIAZjBI0MzUxBguZ4QuDKUgZjiD7DeAE87OzkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t, column = _t, index = _t, #"Serial number" = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,Table.ColumnNames(Source)),
    PreviousStep = Table.TransformColumnTypes(#"Replaced Value",{{"Item", type text}, {"column", Int64.Type}, {"index", Int64.Type}, {"Serial number", type text}}),
    AddConcatenateSerial = Table.AddColumn(PreviousStep, "Concatenate Serial Numbers", each if [column]>[index] then Text.Combine(Table.SelectRows(PreviousStep, (sel)=> sel[Item] = [Item] and sel[column]>= [column])[Serial number], "-")
 else [Serial number])
in
    AddConcatenateSerial

this transforms this

Jimmy801_0-1613735326874.png

 

into this

Jimmy801_1-1613735337392.png

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

stevedep
Memorable Member
Memorable Member

Hi,

 

This should do the trick:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIGYiMgVlCK1YGImEBFFQxBACFuCpUzAgEUURA2BgGwqBNInaEhmDSAmwwSNTM1MYZLGqELQymIIc5AFlg3hHB2dlaKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t, column = _t, index = _t, #"Serial number" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", type text}, {"column", Int64.Type}, {"index", Int64.Type}, {"Serial number", type text}}),
    fn = (tbl as table,colval as text) => Text.Combine( Table.SelectRows(tbl, each [Item] = colval)[Serial number], "-"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [column] > [index] then fn(#"Changed Type", [Item]) else 0
)
in
    #"Added Custom"

stevedep_0-1613735855260.png

Kind regards, Steve

View solution in original post

2 REPLIES 2
stevedep
Memorable Member
Memorable Member

Hi,

 

This should do the trick:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIGYiMgVlCK1YGImEBFFQxBACFuCpUzAgEUURA2BgGwqBNInaEhmDSAmwwSNTM1MYZLGqELQymIIc5AFlg3hHB2dlaKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t, column = _t, index = _t, #"Serial number" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", type text}, {"column", Int64.Type}, {"index", Int64.Type}, {"Serial number", type text}}),
    fn = (tbl as table,colval as text) => Text.Combine( Table.SelectRows(tbl, each [Item] = colval)[Serial number], "-"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [column] > [index] then fn(#"Changed Type", [Item]) else 0
)
in
    #"Added Custom"

stevedep_0-1613735855260.png

Kind regards, Steve

Jimmy801
Community Champion
Community Champion

Hello @SaaM 

 

not to 100% clear how your column is calculated. I hope I guessed right.

Try to add a new column with this formula. The variable PreviousStep has to refer to your prior step

if [column]>[index] then Text.Combine(Table.SelectRows(PreviousStep, (sel)=> sel[Item] = [Item] and sel[column]>= [column])[Serial number], "-")
 else [Serial number]

here a complete example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIGYiMgVorVgQiYQAUNQeDQAri4KVTOCARQREHYGATAok4gdYaGYNIAZjBI0MzUxBguZ4QuDKUgZjiD7DeAE87OzkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t, column = _t, index = _t, #"Serial number" = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,Table.ColumnNames(Source)),
    PreviousStep = Table.TransformColumnTypes(#"Replaced Value",{{"Item", type text}, {"column", Int64.Type}, {"index", Int64.Type}, {"Serial number", type text}}),
    AddConcatenateSerial = Table.AddColumn(PreviousStep, "Concatenate Serial Numbers", each if [column]>[index] then Text.Combine(Table.SelectRows(PreviousStep, (sel)=> sel[Item] = [Item] and sel[column]>= [column])[Serial number], "-")
 else [Serial number])
in
    AddConcatenateSerial

this transforms this

Jimmy801_0-1613735326874.png

 

into this

Jimmy801_1-1613735337392.png

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 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.

Top Kudoed Authors