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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
lanecarrier
Frequent Visitor

Group By with Index and Calculated Column

Hello All,

 

Within Power Query I have a table grouped by item ID (in this case API_NO) and can create a nested index column.

lanecarrier_0-1674580688350.png

My goal is to add a calculated column within this step to calculate 'END_DATE' using the 'Index' and 'PRODUCTION_DAY' columns.  In short, 'PRODUCTION_DATE' is the start date  of the selected record and the end date of the previous record.

 

Here is my attempt at this:  

= Table.Group(#"Sorted Rows3", {"API_NO"}, {{"Count", each Table.AddIndexColumn(_, "Index", 0, 1), type table}, {"Counts", each Table.AddColumn(_, "END_DATE", each "Counts" [PRODUCTION_DAY] {[Index] + 1}), type table}})

 

This creates two lists of tables.

lanecarrier_0-1674581493254.png

 

Any help is greatly appreciated,

Lane

 

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @lanecarrier ,

 

I don't think you need Group By for this - you can just merge the table on itelf with two indexes:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc/LDYAwDAPQXXJGip3Q3yyo+69BQoXogeuTnc91Cc3lEFBhakCVebxopuiJfUf/wawzkHzwLDWQTVG+5MLYgpE4doz6mXXb0LrCE8uDrY9AhzIXoW3ISLZv5sI4nvkRXea8AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [API_NO = _t, PRODUCTION_DAY = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"API_NO", Int64.Type}, {"PRODUCTION_DAY", type date}}),
    sortAPI_DAY = Table.Sort(chgTypes,{{"API_NO", Order.Ascending}, {"PRODUCTION_DAY", Order.Ascending}}),
    addIndex0 = Table.AddIndexColumn(sortAPI_DAY, "Index0", 0, 1, Int64.Type),
    addIndex1 = Table.AddIndexColumn(addIndex0, "Index1", 1, 1, Int64.Type),
    mergeOnSelf = Table.NestedJoin(addIndex1, {"API_NO", "Index1"}, addIndex1, {"API_NO", "Index0"}, "addIndex1", JoinKind.LeftOuter),
    expandPROD_DAY = Table.ExpandTableColumn(mergeOnSelf, "addIndex1", {"PRODUCTION_DAY"}, {"PRODUCTION_DAY.1"}),
    remOthCols = Table.SelectColumns(expandPROD_DAY,{"API_NO", "PRODUCTION_DAY", "PRODUCTION_DAY.1"})
in
    remOthCols

 

Summary:

sortAPI_DAY = Sort original table in order of API Asc, then DAY Asc.

addIndex0 / addIndex1 = Create two indox columns, one starting from zero, the other from one.

mergeOnSelf = Merge the table on itself using [API_NO]&[Index1] = [API_NO]&[INDEX0]

expandPROD_DAY = Expand the [PRODUCTION_DAY] column from the nested table to get the next row value.

 

Example output:

BA_Pete_0-1674637614502.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

2 REPLIES 2
BA_Pete
Super User
Super User

Hi @lanecarrier ,

 

I don't think you need Group By for this - you can just merge the table on itelf with two indexes:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc/LDYAwDAPQXXJGip3Q3yyo+69BQoXogeuTnc91Cc3lEFBhakCVebxopuiJfUf/wawzkHzwLDWQTVG+5MLYgpE4doz6mXXb0LrCE8uDrY9AhzIXoW3ISLZv5sI4nvkRXea8AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [API_NO = _t, PRODUCTION_DAY = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"API_NO", Int64.Type}, {"PRODUCTION_DAY", type date}}),
    sortAPI_DAY = Table.Sort(chgTypes,{{"API_NO", Order.Ascending}, {"PRODUCTION_DAY", Order.Ascending}}),
    addIndex0 = Table.AddIndexColumn(sortAPI_DAY, "Index0", 0, 1, Int64.Type),
    addIndex1 = Table.AddIndexColumn(addIndex0, "Index1", 1, 1, Int64.Type),
    mergeOnSelf = Table.NestedJoin(addIndex1, {"API_NO", "Index1"}, addIndex1, {"API_NO", "Index0"}, "addIndex1", JoinKind.LeftOuter),
    expandPROD_DAY = Table.ExpandTableColumn(mergeOnSelf, "addIndex1", {"PRODUCTION_DAY"}, {"PRODUCTION_DAY.1"}),
    remOthCols = Table.SelectColumns(expandPROD_DAY,{"API_NO", "PRODUCTION_DAY", "PRODUCTION_DAY.1"})
in
    remOthCols

 

Summary:

sortAPI_DAY = Sort original table in order of API Asc, then DAY Asc.

addIndex0 / addIndex1 = Create two indox columns, one starting from zero, the other from one.

mergeOnSelf = Merge the table on itself using [API_NO]&[Index1] = [API_NO]&[INDEX0]

expandPROD_DAY = Expand the [PRODUCTION_DAY] column from the nested table to get the next row value.

 

Example output:

BA_Pete_0-1674637614502.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Wow, i got stuck down a rabbit hole for sure there.

 

Thank you!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors