Bringing previous record in power query

ahmedismailfourtex

Board Regular
Joined
Apr 28, 2015
Messages
124
Hello,
I am working with a table like this:
M/C​
Date​
Record​
Needed Column​
1​
17/01/2020​
x​
y​
1​
15/01/2020​
y​
y​
1​
14/01/2020​
y​
x​
1​
10/01/2020​
x​
and I need to bring the previous record for the same M/C in a (Needed Column), in power query.
Thanks in advance :).
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
at first look...

M/CDateRecordRecord.1
117/01/2020xy
115/01/2020yy
114/01/2020yx
110/01/2020x

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    Index = Table.AddIndexColumn(Type, "Index", 0, 1),
    Index1 = Table.AddIndexColumn(Index, "Index.1", 1, 1),
    Join = Table.NestedJoin(Index1,{"Index.1"},Index,{"Index"},"Table",JoinKind.LeftOuter),
    Expand = Table.ExpandTableColumn(Join, "Table", {"Record"}, {"Record.1"}),
    Sort = Table.Sort(Expand,{{"Index", Order.Ascending}}),
    RC = Table.RemoveColumns(Sort,{"Index", "Index.1"})
in
    RC
 
Upvote 0
Solution
at first look...

M/CDateRecordRecord.1
117/01/2020xy
115/01/2020yy
114/01/2020yx
110/01/2020x

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    Index = Table.AddIndexColumn(Type, "Index", 0, 1),
    Index1 = Table.AddIndexColumn(Index, "Index.1", 1, 1),
    Join = Table.NestedJoin(Index1,{"Index.1"},Index,{"Index"},"Table",JoinKind.LeftOuter),
    Expand = Table.ExpandTableColumn(Join, "Table", {"Record"}, {"Record.1"}),
    Sort = Table.Sort(Expand,{{"Index", Order.Ascending}}),
    RC = Table.RemoveColumns(Sort,{"Index", "Index.1"})
in
    RC
Thank you for your reply, but it doesn't work for different values in column "M/C"
if we added M/C no 2 it will come to M/C no 1. :unsure:
 
Upvote 0
that's what I did with Join
thanks
have a nice day

You can change the solution post by clicking on another one that helped you better.
And you can always Like the posts that you like or helps you
 
Upvote 0

Forum statistics

Threads
1,223,757
Messages
6,174,331
Members
452,555
Latest member
colc007

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