Forum Discussion

Varan_15's avatar
Varan_15
Icon for Helper III rankHelper III
2 years ago
Solved

row to column in power query

Hi All, I have data like same worksteps (input, Pending) with entry and exit date time i want to convert into All the worksteps with entry - exit column level in power query.   Please refer below ...
  • Ashish_Mathur's avatar
    2 years ago

    Hi,

    This M code works

    let
        Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
        #"Uppercased Text" = Table.TransformColumns(Source,{{"Workstep", Text.Upper, type text}}),
        #"Grouped Rows" = Table.Group(#"Uppercased Text", {"Process ID", "Workstep"}, {{"Count", each Table.AddIndexColumn(_,"Index",1,1)}},GroupKind.Local),
        #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Process datetime", "status", "Index"}, {"Process datetime", "status", "Index"}),
        #"Added Index" = Table.AddIndexColumn(#"Expanded Count", "Order", 1, 1, Int64.Type),
        #"Grouped Rows1" = Table.Group(#"Added Index", {"Process ID", "Index"}, {{"Count", each Table.AddIndexColumn(_,"Index1",1,1)}}),
        #"Expanded Count1" = Table.ExpandTableColumn(#"Grouped Rows1", "Count", {"Workstep", "Process datetime", "status", "Order", "Index1"}, {"Workstep", "Process datetime", "status", "Order", "Index1"}),
        #"Sorted Rows" = Table.Sort(#"Expanded Count1",{{"Order", Order.Ascending}}),
        #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Order", "Index"}),
        #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Process datetime", type datetime}}),
        #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[status]), "status", "Process datetime"),
        #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Index1"}),
        #"Sorted Rows1" = Table.Sort(#"Removed Columns1",{{"Process ID", Order.Ascending}, {"Entry", Order.Ascending}})
    in
        #"Sorted Rows1"

    Hope this helps.

     

  • PavanLalwani's avatar
    2 years ago

    To transform the data from row to column in Power Query and achieve either **Result 1** or **Result 2**, follow these steps:

    ### Steps in Power Query:

    1. **Sort the Data**: Ensure the data is sorted by `Process ID`, `Workstep`, and `Process datetime`.

    2. **Group the Data**:
    - Use **Group By** on `Process ID` and `Workstep`.
    - Inside **Group By**, aggregate the **Entry** and **Exit** times:
    - For **Entry**, choose **Min**.
    - For **Exit**, choose **Max** (filter for "Exit" status).

    3. **Conditional Column**: If needed, add a counter or index column to differentiate repeated worksteps for **Result 2**.

    This will reshape the data as you need.