Forum Discussion
Richard_Halsall
Helper IV
1 year agoExpand grouped data but apply a custom column value only to the last group record
Hi I am attempting to apply a custom column value only to the last group record, [I have a column named 'SiteDate' which could be used to determine the last record] before expanding my data view ...
- 1 year ago
Hi Richard_Halsall Could you try this please
- Group the data by "Technician" and include all rows in a column named "AllData".
- Add an index column to each group to track row order.
- Add a column to identify the last record in each group by comparing the index to the total row count.
- Add a custom column that assigns MinimumHours only to the last record in each group; otherwise, assign null .
- Expand the grouped table back into rows with the added columns.
- Try this code please replace according to your columns
let GroupedData = Table.Group(Source, {"Technician"}, {{"AllData", each _, type table [SiteDate=date, MinimumHours=number]}}), // Add Index to Each Group AddIndex = Table.TransformColumns(GroupedData, {"AllData", each Table.AddIndexColumn(_, "Index", 1, 1, Int64.Type)}), // Identify Last Record AddIsLastRecord = Table.TransformColumns(AddIndex, {"AllData", each Table.AddColumn(_, "IsLastRecord", (row) => row[Index] = Table.RowCount(_))}), // Apply Custom Column for Last Record AddCustomColumn = Table.TransformColumns(AddIsLastRecord, {"AllData", each Table.AddColumn(_, "CustomValue", (row) => if row[IsLastRecord] then row[MinimumHours] else null)}), // Expand Back Data ExpandedData = Table.ExpandTableColumn(AddCustomColumn, "AllData", {"SiteDate", "MinimumHours", "CustomValue"}) in ExpandedData
Akash_Varuna
Super User
1 year agoHi Richard_Halsall Could you try this please
- Group the data by "Technician" and include all rows in a column named "AllData".
- Add an index column to each group to track row order.
- Add a column to identify the last record in each group by comparing the index to the total row count.
- Add a custom column that assigns MinimumHours only to the last record in each group; otherwise, assign null .
- Expand the grouped table back into rows with the added columns.
- Try this code please replace according to your columns
let
GroupedData = Table.Group(Source, {"Technician"}, {{"AllData", each _, type table [SiteDate=date, MinimumHours=number]}}),
// Add Index to Each Group
AddIndex = Table.TransformColumns(GroupedData, {"AllData", each Table.AddIndexColumn(_, "Index", 1, 1, Int64.Type)}),
// Identify Last Record
AddIsLastRecord = Table.TransformColumns(AddIndex, {"AllData", each Table.AddColumn(_, "IsLastRecord", (row) => row[Index] = Table.RowCount(_))}),
// Apply Custom Column for Last Record
AddCustomColumn = Table.TransformColumns(AddIsLastRecord, {"AllData", each Table.AddColumn(_, "CustomValue", (row) => if row[IsLastRecord] then row[MinimumHours] else null)}),
// Expand Back Data
ExpandedData = Table.ExpandTableColumn(AddCustomColumn, "AllData", {"SiteDate", "MinimumHours", "CustomValue"})
in
ExpandedData
Richard_Halsall
Helper IV
1 year agoAkash_Varuna Perfect thanks very much