Forum Discussion
ogend
4 years agoHelper II
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, and the records would be taken from the "Text" column itslef by offsetting down from the record
| text | number | List |
| apple | 0 | |
| pear | 0 | |
| plum | 3 | list (1) |
| mango | 2 | list (2) |
| pineapple | 0 | |
| watermelon | 0 | |
| strawberry | 2 | list(3) |
| guava | 0 | |
| blueberry | 4 | list(4) |
| orange | 0 | |
| lemon | 0 | |
| kiwi | 0 | |
| peach | 0 | |
| apricot | 0 |
Expected values in each list:
| list (1) |
| mango |
| pineapple |
watermelon |
| list (2) |
| pineapple |
| watermelon |
| list(3) |
| guava |
| blueberry |
| list(4) |
| orange |
| lemon |
| kiwi |
| peach |
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"
2 Replies
- ronrsnfldSuper User
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"