Forum Discussion
Create flattened table from logtable
- 2 years ago
Your spelling is all over the place - not good in Power Query as that is case sensitive.
let fx = (A) => let #"Added Custom" = Table.AddColumn(A, "First Color", each List.First(A[Color]), type text), #"Added Custom1" = Table.AddColumn(#"Added Custom", "Last Color", each List.Last(A[Color])), #"Added Custom2" = Table.AddColumn( #"Added Custom1", "Green Present", each if List.Contains(A[Color], "Green") then 1 else 0, Int64.Type ), #"Added Custom3" = Table.AddColumn( #"Added Custom2", "Green Step", each if [Green Present] = 1 then Table.SelectRows(A, each [Color] = "Green"){0}[Step] else 0, Int64.Type ), #"Added Custom4" = Table.AddColumn( #"Added Custom3", "Walk after Sit", each try let s = Table.SelectRows(A, each [Action] = "Sit"){0}[Step], w = Table.SelectRows(A, each [Step] = s + 1){0}[Action] in if w = "Walk" then 1 else 0 otherwise 0, Int64.Type ), #"Removed Other Columns" = Table.SelectColumns( #"Added Custom4", {"ID", "First Color", "Last Color", "Green Present", "Green Step", "Walk after Sit"} ), #"Removed Duplicates" = Table.Distinct(#"Removed Other Columns", {"ID"}) in #"Removed Duplicates", Source = Table.FromRows( Json.Document( Binary.Decompress( Binary.FromText( "i45WclTSUTIEYqec0lQgFZ6Yk60UqwMRNwLiyNScnPxyICM4swQuYQzE7kWpqXnoOkxAAhmZJRhGmQJxQGlRQQ6KjBPUcrglAUX5yanFxZl56XB5IwLyxkhWwtzoBHUKVn2xAA==", BinaryEncoding.Base64 ), Compression.Deflate ) ), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Step = _t, Color = _t, Action = _t] ), #"Changed Type" = Table.TransformColumnTypes( Source, {{"ID", type text}, {"Step", Int64.Type}, {"Color", type text}, {"Action", type text}} ), #"Grouped Rows" = Table.Group( #"Changed Type", {"ID"}, { { "Rows", each _, type table [ ID = nullable text, Step = nullable number, Color = nullable text, Action = nullable text ] } } ), #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each fx([Rows])), #"Expanded Custom" = Table.ExpandTableColumn( #"Added Custom", "Custom", {"First Color", "Last Color", "Green Present", "Green Step", "Walk after Sit"}, {"First Color", "Last Color", "Green Present", "Green Step", "Walk after Sit"} ), #"Removed Other Columns" = Table.SelectColumns( #"Expanded Custom", {"ID", "First Color", "Last Color", "Green Present", "Green Step", "Walk after Sit"} ) in #"Removed Other Columns"How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.
Hi, i have been able to transform your code and got it working. It looks really nice and i can add multiple columns into your function.
I do have 1 more question, which i havent stated in my problem. Where do i perform a sort ASC by 'step'?
Thank you.
what for? You should avoid sorting in Power Query as much as possible, it is very expensive.
- PQ-Noob6751372 years agoFrequent Visitor
Alright, in my example table the step is automaticly sorted asc.
im unsure if this is the case with the actual data.