Forum Discussion

OneWithQuestion's avatar
OneWithQuestion
Post Prodigy
3 years ago
Solved

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"
  • 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
  • AlienSx's avatar
    AlienSx
    3 years ago

    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

     

     

3 Replies

  • 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
    • OneWithQuestion's avatar
      OneWithQuestion
      Post Prodigy

      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?

      • AlienSx's avatar
        AlienSx
        Super User

        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