cancel
Showing results for 
Search instead for 
Did you mean: 
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
Memorable Member
Memorable Member

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
Memorable Member
Memorable Member

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
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

MPPC 2023 PBI Carousel

Power Platform Conference-Power BI and Fabric Sessions

Join us Oct 1 - 6 in Las Vegas for the Microsoft Power Platform Conference.

Top Solution Authors
Top Kudoed Authors