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

    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