Forum Discussion
row to column in power query
- 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.
- 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.
Try 3:
End DateTime =
VAR StartDateTime = MAX(YourTable[StartDateTime]) -- The start date and time for the current row context
VAR HoursToAdd = [Additional Hours] -- Reference the dynamic measure for additional hours
VAR WorkingStartHour = 8
VAR WorkingEndHour = 16
VAR WorkingHoursPerDay = WorkingEndHour - WorkingStartHour
-- Filter the calendar table to get only future working days starting from the start date
VAR FilteredCalendar =
FILTER(
Calendar,
Calendar[Date] >= DATEVALUE(StartDateTime) &&
Calendar[WorkingDay] = 1
)
-- Calculate the number of complete working days required
VAR FullDaysRequired = INT(HoursToAdd / WorkingHoursPerDay)
VAR RemainingHours = MOD(HoursToAdd, WorkingHoursPerDay)
-- Find the date after adding the required number of working days
VAR EndDate =
MINX(
FILTER(
ADDCOLUMNS(
FilteredCalendar,
"CumulativeDays",
RANKX(
FilteredCalendar,
Calendar[Date],
,
ASC,
DENSE
)
),
[CumulativeDays] > FullDaysRequired
),
Calendar[Date]
)
-- Calculate the final time based on the remaining hours
VAR StartHour =
IF(
HOUR(StartDateTime) < WorkingStartHour,
WorkingStartHour,
MAX(HOUR(StartDateTime), WorkingStartHour)
)
VAR EndTime =
TIME(StartHour + RemainingHours, MINUTE(StartDateTime), SECOND(StartDateTime))
RETURN
EndDate + EndTime