Add custom column that finds last reading

abdulbasitb

New Member
Joined
Apr 21, 2015
Messages
17
Note: Need a solution in power query (no VBA).

Please refer to image below. There needs to be a new custom column added like the green one highlighted below, that would automatically find last reading for the relevant ID, even if the columns are not sorted.

JiknX8c.png
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Does this work for you?
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Readings"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Meter Reading", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
    #"Sorted Rows" = Table.Buffer(Table.Sort(#"Added Index",{{"ID", Order.Ascending}, {"Meter Reading", Order.Ascending}})),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"ID"}, {{"Count", each Table.AddIndexColumn(_, "Index2", 1,1),type table}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Meter Reading", "Index", "Index2"}, {"Meter Reading", "Index", "Index2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Count",{{"Meter Reading", Int64.Type}, {"Index", Int64.Type}, {"Index2", Int64.Type}}),
    #"Inserted Addition" = Table.AddColumn(#"Changed Type1", "Addition", each [Index2] + 1, type number),
    #"Merged Queries" = Table.NestedJoin(#"Inserted Addition",{"ID", "Index2"},#"Inserted Addition",{"ID", "Addition"},"Inserted Addition",JoinKind.LeftOuter),
    #"Expanded Inserted Addition" = Table.ExpandTableColumn(#"Merged Queries", "Inserted Addition", {"Meter Reading"}, {"Last Reading"}),
    #"Sorted Rows1" = Table.Sort(#"Expanded Inserted Addition",{{"Index", Order.Ascending}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Sorted Rows1",{"ID", "Meter Reading", "Last Reading"})
in
    #"Removed Other Columns"

Peter
 
Upvote 0
Another way

Code:
let Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Meter reading", Int64.Type}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
#"Sorted Rows" = Table.Sort(#"Added Index",{{"ID", Order.Ascending}, {"Meter reading", Order.Ascending}}),
#"Added Index1" = Table.AddIndexColumn(#"Sorted Rows", "Index.1", 0, 1),
Table3 = Table.AddIndexColumn(#"Added Index1", "Index.2", 1, 1),
#"Merged Queries" = Table.NestedJoin(Table3,{"Index.1", "ID"},Table3,{"Index.2", "ID"},"Table3",JoinKind.LeftOuter),
#"Expanded Table3" = Table.ExpandTableColumn(#"Merged Queries", "Table3", {"Meter reading"}, {"Table3.Meter reading"}),
#"Sorted Rows1" = Table.Sort(#"Expanded Table3",{{"Index", Order.Ascending}}),
#"Removed Columns" = Table.RemoveColumns(#"Sorted Rows1",{"Index", "Index.1", "Index.2"})
in #"Removed Columns"
 
Upvote 0
Does this work for you?
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Readings"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Meter Reading", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
    #"Sorted Rows" = Table.Buffer(Table.Sort(#"Added Index",{{"ID", Order.Ascending}, {"Meter Reading", Order.Ascending}})),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"ID"}, {{"Count", each Table.AddIndexColumn(_, "Index2", 1,1),type table}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows",  "Count", {"Meter Reading", "Index", "Index2"}, {"Meter Reading",  "Index", "Index2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded  Count",{{"Meter Reading", Int64.Type}, {"Index", Int64.Type}, {"Index2",  Int64.Type}}),
    #"Inserted Addition" = Table.AddColumn(#"Changed Type1", "Addition", each [Index2] + 1, type number),
    #"Merged Queries" = Table.NestedJoin(#"Inserted Addition",{"ID",  "Index2"},#"Inserted Addition",{"ID", "Addition"},"Inserted  Addition",JoinKind.LeftOuter),
    #"Expanded Inserted Addition" = Table.ExpandTableColumn(#"Merged  Queries", "Inserted Addition", {"Meter Reading"}, {"Last Reading"}),
    #"Sorted Rows1" = Table.Sort(#"Expanded Inserted Addition",{{"Index", Order.Ascending}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Sorted Rows1",{"ID", "Meter Reading", "Last Reading"})
in
    #"Removed Other Columns"

Peter
I was trying to understand how the line below works as it seems handy for other situations also.
Is this command available through ribbon?
Code:
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"ID"}, {{"Count", each Table.AddIndexColumn(_, "Index2", 1,1),type table}})
 
Last edited:
Upvote 0
I used the technique described here:

https://www.excelguru.ca/blog/2018/06/27/number-rows-by-group-using-power-query/

Note that Table.Buffer has to be placed around the Table.Sort function to preserve the correct sort order. Horseride's method is simpler and easier to understand. I tried that method first myself but confess I messed the join order up so then went for the more complex solution.

Peter
 
Upvote 0

Forum statistics

Threads
1,214,393
Messages
6,119,261
Members
448,880
Latest member
aveternik

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top