Forum Discussion

ogend's avatar
ogend
Icon for Helper II rankHelper II
4 years ago
Solved

power query- generate a list for each record using "offset down" logic

Hi Power Query Gurus,    Is it possible to generate a list of records with the following pattern? I have 2 columns: "Text" and "Number".  "Number" provides a count of records I need in the list, a...
  • ronrsnfld's avatar
    4 years ago

    First add an Index column (with a starting index of 0).

    Then add a custom column with the formula:

     

    = if [number]=0 then null 
        else List.Range(#"Added Index"[text],[Index]+1,[number])

     

     

    Finally, remove that Index column

    The M Code to reproduce this, which you can paste into a blank query:

     

     

    let
    
    //the data
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VY5LCoAwDESvIlm7EPU2xUWUoMX0Q2wt3t6iWHX5ZjKTUQrQeyaooYGhVuAJpXqJo8nQXWDQzi5Te1va0j+aMJAYYmeLtAXBNJLIUXJzxB3LwciRHr+/FCf5zVvKZD59q076O3VaCqEXPblw83AC", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [text = _t, number = _t]),
    
    //set the data types
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"text", type text}, {"number", Int64.Type}}),
    
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    
    //add custom column
        #"Added Custom" = Table.AddColumn(#"Added Index", "List", 
            each if [number]=0 then null 
                    else List.Range(#"Added Index"[text],[Index]+1,[number])),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
    in
        #"Removed Columns"