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
mmahomar
Frequent Visitor

Concatenate based on condition with the results of an earlier concatenated value

Hi all,

 

Need help solving how to concatenate into a new column based on a condition and based on the results of an earlier concatenated value. In the table below:

 

For each row, if "type" is equal to "begin_group", add a new "result" column concatenating the value from "name" in the previous row with the value from "name" in the current row. But, if "type" is equal to "end_group", the last concatenated value must be removed. For example:

 

typenameresult
begin_groupGroup AGroup A
begin_groupGroup BGroup A \ Group B
end_group Group A
end_group  

 

If it is the first "begin_group" the result must be the same as "name".

 

Below is the example table:

 

mmahomar_0-1665888762450.png

 

This is the expected result:

 

mmahomar_2-1665890697743.png

 

This is the code for the first table:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkksSVXSUSouSSwqUYrVgQuk5qWAuUmp6Zl58elF+aUFQFF3EK3giFPGCaeMM1gGaCpcHKdSF5wyrtgNIcpUNwr0umNXWpJaUQJku4KCKhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"type" = _t, name = _t])
in
    Source

 

 

Thank you, 

 

Munir

 

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Hi @mmahomar ,
Please check out the following code. It isn't exactly what you've described, but should give you a hint on where the logic of the hierarchy must be further specified/corrected:

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText(
                    "i45WSkksSVXSUSouSSwqUYrVgQuk5qWAuUmp6Zl58elF+aUFQFF3EK3giFPGCaeMM1gGaCpcHKdSF5wyrtgNIcpUNwr0umNXWpJaUQJku4KCKhYA", 
                    BinaryEncoding.Base64
                ), 
                Compression.Deflate
            )
        ), 
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [#"type" = _t, name = _t]
    ), 
    Custom1 = Source, 
    Custom2 = Table.ToRecords(Custom1), 
    Custom3 = List.Accumulate(
        Custom2, 
        {}, 
        (state, current) =>
            state
                & {
                    Record.AddField(
                        current, 
                        "result", 
                        if current[type] = "date" or current[type] = "text" then
                            current[name]
                        else if current[#"type"]
                            = "begin_group"
                                and (
                                    List.Last(state)[type]
                                        <> "begin_group" and List.Last(state)[type]
                                        <> "end_group"
                                )
                        then
                            current[name]
                        else if current[#"type"]
                            = "begin_group"
                                and (
                                    List.Last(state)[type]
                                        = "begin_group" or List.Last(state)[type]
                                        = "end_group"
                                )
                        then
                            List.Last(state)[result] & "\" & current[name]
                        else
                            Text.BeforeDelimiter(
                                List.Last(state)[result], 
                                "\", 
                                {0, RelativePosition.FromEnd}
                            )
                    )
                }
    ), 
    #"Converted to Table" = Table.FromList(
        Custom3, 
        Splitter.SplitByNothing(), 
        null, 
        null, 
        ExtraValues.Error
    ), 
    #"Expanded Column1" = Table.ExpandRecordColumn(
        #"Converted to Table", 
        "Column1", 
        {"type", "name", "result"}, 
        {"type", "name", "result"}
    )
in
    #"Expanded Column1"

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

5 REPLIES 5
ImkeF
Super User
Super User

Hi @mmahomar ,
Please check out the following code. It isn't exactly what you've described, but should give you a hint on where the logic of the hierarchy must be further specified/corrected:

let
    Source = Table.FromRows(
        Json.Document(
            Binary.Decompress(
                Binary.FromText(
                    "i45WSkksSVXSUSouSSwqUYrVgQuk5qWAuUmp6Zl58elF+aUFQFF3EK3giFPGCaeMM1gGaCpcHKdSF5wyrtgNIcpUNwr0umNXWpJaUQJku4KCKhYA", 
                    BinaryEncoding.Base64
                ), 
                Compression.Deflate
            )
        ), 
        let
            _t = ((type nullable text) meta [Serialized.Text = true])
        in
            type table [#"type" = _t, name = _t]
    ), 
    Custom1 = Source, 
    Custom2 = Table.ToRecords(Custom1), 
    Custom3 = List.Accumulate(
        Custom2, 
        {}, 
        (state, current) =>
            state
                & {
                    Record.AddField(
                        current, 
                        "result", 
                        if current[type] = "date" or current[type] = "text" then
                            current[name]
                        else if current[#"type"]
                            = "begin_group"
                                and (
                                    List.Last(state)[type]
                                        <> "begin_group" and List.Last(state)[type]
                                        <> "end_group"
                                )
                        then
                            current[name]
                        else if current[#"type"]
                            = "begin_group"
                                and (
                                    List.Last(state)[type]
                                        = "begin_group" or List.Last(state)[type]
                                        = "end_group"
                                )
                        then
                            List.Last(state)[result] & "\" & current[name]
                        else
                            Text.BeforeDelimiter(
                                List.Last(state)[result], 
                                "\", 
                                {0, RelativePosition.FromEnd}
                            )
                    )
                }
    ), 
    #"Converted to Table" = Table.FromList(
        Custom3, 
        Splitter.SplitByNothing(), 
        null, 
        null, 
        ExtraValues.Error
    ), 
    #"Expanded Column1" = Table.ExpandRecordColumn(
        #"Converted to Table", 
        "Column1", 
        {"type", "name", "result"}, 
        {"type", "name", "result"}
    )
in
    #"Expanded Column1"

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

@ImkeF OMG this is amazing! exactly what I was looking for, like magic 🙂 - thank you!

mmahomar
Frequent Visitor

Hi Imke,

You are correct! My picture is wrong. I will update it, but Group D should be included in that row.

ImkeF
Super User
Super User

Hi @mmahomar
I am wondering why GroupD is missing in the row below:
2022-10-16_12-02-36.jpgBut if it is like in your first picture, please describe the logic on how those hierarchies are built.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi Imke, this is the corrected picture:

 

mmahomar_0-1665919753689.png

Thank you for looking into this!

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