Forum Discussion

PQ-Noob675137's avatar
PQ-Noob675137
Frequent Visitor
2 years ago
Solved

Create flattened table from logtable

Hi all,   I have some questions regarding Power query. I have a log table see first table below   Table 2 shows what im trying to achieve, with some example columnms.  What i currently hav...
  • lbendlin's avatar
    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.