Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Richard_Halsall
Helper IV
Helper IV

Expand 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 is as shown below with 'MinimumHours' a custom column

Richard_Halsall_0-1740999604187.png

So if I expand the 'AllData' column the 'MinimumHours' value will be against each record

Richard_Halsall_1-1740999818993.png


However I only require it to show against the last record in the group in this instance the line with a 'SiteDate' of 05/01/2025

 

Can this be achieved in PQ any assistance would be appreciated

 

Thanks

1 ACCEPTED SOLUTION
Akash_Varuna
Super User
Super User

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

 

 

View solution in original post

2 REPLIES 2
Akash_Varuna
Super User
Super User

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 Perfect thanks very much

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors