Forum Discussion

Varan_15's avatar
Varan_15
Helper III
3 years ago
Solved

convert row to columns

Hi All,

I have data single ID contains mutiple rows how to convert into columns example input file like :

 

 can we use summarize functions ? Expected Result:

Thanks

  • Hi Varan_15 ,
    sure, please paste the following code into the advanced editor of a new blank query and follow the steps:


    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQ0MDRU0lHKzCsoLQHShvqW+kZGCoYmVqYWYK6hAYhvaWVqqhSrg1BflJqVmgzRYATWYGBlbATmmoG5xlYmBigaEBYgKUBwgfaZKcXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Process = _t, IN = _t, OUT = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Process", type text}, {"IN", type datetime}, {"OUT", type datetime}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Process"}, {{"Partition", each _, type table [ID=nullable number, Process=nullable text, IN=nullable datetime, OUT=nullable datetime]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Partition],"Index", 0,1)),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Partition"}),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"ID", "IN", "OUT", "Index"}, {"ID", "IN", "OUT", "Index"}),
        #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Expanded Custom", {"IN", "OUT"}, "Attribute", "Value"),
        #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Unpivoted Only Selected Columns", {{"Index", type text}}, "en-US"),{"Process", "Attribute", "Index"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Header"),
        #"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Header]), "Header", "Value")
    in
        #"Pivoted Column"

9 Replies

    • Varan_15's avatar
      Varan_15
      Helper III

      Hi Mahesh0016 ,

       

      i have tried pivot column/ group by/transapose  only one input column appearing but as per logic i need input 1 and input 2 (2 columns should be created as per respective date).

       

      whatever available in process column all should be created with separate columns. example

    • Varan_15's avatar
      Varan_15
      Helper III

      Hi ImkeF ,

       

      Thanks for the update , I'm facing error because of i have 2 inputs entry IN/OUT so i need both entries when we tried your steps it's considered only one input IN/OUT 

      couldn't get second input entries.

       

      please suggest how to get all the process rows dates as mentioned expected output columns.

      Thanks,

  • ImkeF's avatar
    ImkeF
    Community Champion

    Hi Varan_15 ,
    before merging the column to retrieve the column names, group your data into chunks and add an index column to those chunked columns. Then expand the tables again, include the index column in the merge and pivot.

    • Varan_15's avatar
      Varan_15
      Helper III

      Hi ImkeF ,

       

      quite confused your steps could you please create sample and assist with your steps.

       

      RAW data:

      IDProcessINOUT
      11011input09/Jan/2022 14:58:0110/Jan/2022 09:55:02
      11011reject02/Jan/2022 10:32:1206/Jan/2022 13:40:49
      11011input06/Jan/2022 13:40:5806/Jan/2022 14:56:42
  • ImkeF's avatar
    ImkeF
    Community Champion

    Hi Varan_15 ,
    sure, please paste the following code into the advanced editor of a new blank query and follow the steps:


    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQ0MDRU0lHKzCsoLQHShvqW+kZGCoYmVqYWYK6hAYhvaWVqqhSrg1BflJqVmgzRYATWYGBlbATmmoG5xlYmBigaEBYgKUBwgfaZKcXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Process = _t, IN = _t, OUT = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Process", type text}, {"IN", type datetime}, {"OUT", type datetime}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Process"}, {{"Partition", each _, type table [ID=nullable number, Process=nullable text, IN=nullable datetime, OUT=nullable datetime]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Partition],"Index", 0,1)),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Partition"}),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"ID", "IN", "OUT", "Index"}, {"ID", "IN", "OUT", "Index"}),
        #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Expanded Custom", {"IN", "OUT"}, "Attribute", "Value"),
        #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Unpivoted Only Selected Columns", {{"Index", type text}}, "en-US"),{"Process", "Attribute", "Index"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Header"),
        #"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Header]), "Header", "Value")
    in
        #"Pivoted Column"