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
OneWithQuestion
Post Prodigy
Post Prodigy

How can I split a text string and reverse the order it splits (so first comes out last)?

I have data as shown in the first column "CurrentInput" what I want to do is split it, but I need to flip around the order first as shown in "Flipped".

I can't find a good way to do this.

I tried doing a Text.Reverse, splitting, then Text.Reverse each column that comes back, and that seems to be an option.

However, it seems like there should be a more elegant way to accomplish this?

Can I do all of it in a single step, Text.Reverse, feed that to the splitter, then Reverse each output column back again???

 

My final output goal is to have the columns which I would name Region, Store, Category, Date.

 

CurrentInputFlippedRegionStoreCategoryDate
2022-01,Sales,Store925,Region1Region1,Store925,Sales,2022-01Region1Store925Sales2022-01
2022-02,Sales,Store854,Region2Region2,Store854,Sales,2022-02Region2Store854Sales2022-02
2022-05,Sales,Store235,Region2Region2,Store235,Sales,2022-05Region2Store235Sales2022-05
2022-01,Sales,Store001,Region3Region3,Store001,Sales,2022-01Region3Store001Sales2022-01

 

 

 

My Advanced Editor text

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI1MNQJTsxJLdYJLskvSrU0MtUJSk3PzM8zVIrVgSkxQlZiYWoCVWKEpMQUWYmRsSkWJSgWGQC5ECXGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CurrentInput = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"CurrentInput", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.Reverse([CurrentInput])),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "Custom", Splitter.SplitTextByDelimiter(",", QuoteStyle.None), {"Custom.1", "Custom.2", "Custom.3", "Custom.4"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Custom.1", type text}, {"Custom.2", type text}, {"Custom.3", type text}, {"Custom.4", type text}})
in
    #"Changed Type1"
2 ACCEPTED SOLUTIONS
AlienSx
Super User
Super User

Hello, @OneWithQuestion 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI1MNQJTsxJLdYJLskvSrU0MtUJSk3PzM8zVIrVgSkxQlZiYWoCVWKEpMQUWYmRsSkWJSgWGQC5ECXGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CurrentInput = _t]),
    column_names = { "Date", "Category", "Store", "Region" },
    output_goal = 
        Table.ReorderColumns(
            Table.FromRows(
                List.Transform(
                    Source[CurrentInput],
                    Splitter.SplitTextByDelimiter(",")
                ),
                column_names
            ),
            List.Reverse(column_names)
            
        )
in
    output_goal

View solution in original post

no. It won't work. To retain all other columns lets transform column items to list >> list reverse >> list to record with known names and finally expand this column of records.  

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI1MNQJTsxJLdYJLskvSrU0MtUJSk3PzM8zVNJRSgTiVKVYHZhSI2SlFqYmUKVGQGVJQJyGpNQUWamRsSmS0mQgTkdSiuIAAyAXotQYqCwFiDOUYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CurrentInput = _t, other_column01 = _t, other_column02 = _t]),
    column_names = { "Region", "Store", "Category", "Date"  },
    output_goal = 
        Table.ExpandRecordColumn(
            Table.TransformColumns(
                Source, 
                {
                    {"CurrentInput",
                    (x) => 
                        Record.FromList(
                            List.Reverse(
                                Splitter.SplitTextByDelimiter(",")(x)
                            ),
                            column_names
                        )
                    }
                }
            ),
            "CurrentInput",
            column_names
        )
in
    output_goal

 

 

View solution in original post

3 REPLIES 3
AlienSx
Super User
Super User

Hello, @OneWithQuestion 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI1MNQJTsxJLdYJLskvSrU0MtUJSk3PzM8zVIrVgSkxQlZiYWoCVWKEpMQUWYmRsSkWJSgWGQC5ECXGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CurrentInput = _t]),
    column_names = { "Date", "Category", "Store", "Region" },
    output_goal = 
        Table.ReorderColumns(
            Table.FromRows(
                List.Transform(
                    Source[CurrentInput],
                    Splitter.SplitTextByDelimiter(",")
                ),
                column_names
            ),
            List.Reverse(column_names)
            
        )
in
    output_goal

Thank you so much!

I'm trying to properly understand this and adapt it.

I'm running into an issue with retaining existing columns in the table.

If in the example I provided, I wanted to only split the first column (which you did) but KEEP those other columns in the table, how does the syntax work for that?

How can you use Table.ReorderColumns to only address specific columns instead of the entire table?

no. It won't work. To retain all other columns lets transform column items to list >> list reverse >> list to record with known names and finally expand this column of records.  

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI1MNQJTsxJLdYJLskvSrU0MtUJSk3PzM8zVNJRSgTiVKVYHZhSI2SlFqYmUKVGQGVJQJyGpNQUWamRsSmS0mQgTkdSiuIAAyAXotQYqCwFiDOUYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CurrentInput = _t, other_column01 = _t, other_column02 = _t]),
    column_names = { "Region", "Store", "Category", "Date"  },
    output_goal = 
        Table.ExpandRecordColumn(
            Table.TransformColumns(
                Source, 
                {
                    {"CurrentInput",
                    (x) => 
                        Record.FromList(
                            List.Reverse(
                                Splitter.SplitTextByDelimiter(",")(x)
                            ),
                            column_names
                        )
                    }
                }
            ),
            "CurrentInput",
            column_names
        )
in
    output_goal

 

 

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