Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Repeat sequence number in power query

Kindly suggest how to get the below repeated sequence in power query, kindly help.   Type Repeate Sequence A 1 A 2 A 3 B 1 B 2 B 3 B 4 C 1 C 2 D 1
  • tackytechtom's avatar
    3 years ago

    Hi Anonymous ,

     

    Here a solution:

     

     

    Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclSK1UElnXCQzkiki1JsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Type = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Type", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Type"}, {{"Grouping", each _, type table [Type=nullable text]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn ( [Grouping], "Index", 1 )),
        #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Type", "Index"}, {"Type", "Index"})
    in
        #"Expanded Custom"

     

    I used the steps described in here:

    https://www.tackytech.blog/how-to-swiftly-take-over-power-query/#3_Create_ranks_and_indexes

     

    Let me know if this helps 🙂

     

    /Tom
    https://www.tackytech.blog/
    https://www.instagram.com/tackytechtom/

  • AlienSx's avatar
    3 years ago

    Hi, Anonymous 

    let
        f = (ch, num) => let a = {{ch, num}} in (if num = 1 then a else a & @f(ch, num - 1)),
        out = 
            Table.Sort(
                Table.FromRows(
                    f("A", 3) & f("B", 4) & f("C", 2) & f("D", 1),
                    {"Type", "Repeat Sequence"}
                ),
                {"Type", {"Repeat Sequence", Order.Ascending}}
            )
    in
        out