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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
michaelu1
Advocate II
Advocate II

Getting prior record

I'm trying to get the prior LeaseID in power query for each record on my table.

 

You can see how the LeaseIDs don't neccesarily start at 1 and are also not consecutive.

 

For each unit and lease, and I'm trying to get the prior lease.

 

Here is the data:

 

michaelu1_0-1707920708659.png

 

Here is the expected result:

 

michaelu1_1-1707920825309.png

 

(FYI the actual data has 200k+ records)

 

Here is a sample file:

https://drive.google.com/file/d/1yil8zqNSnEwYT5P5mGZeSnah8PyNjehA/view?usp=sharing

1 ACCEPTED SOLUTION
ronrsnfld
Super User
Super User

Group by UnitID, then add a shifted column to each subtable.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQw0HU0VNJRMlSK1UHiGqFyjVG5JnCuE6peJ1TFYK4pKtcMwTVCVWyEarIRpmILpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [UnitID = _t, LeaseID = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"UnitID", type text}, {"LeaseID", Int64.Type}}),

//Group by UnitID
// then add Shifted Column to each sub table and re-expand
    #"Group UnitID" = Table.Group(#"Changed Type",{"UnitID"}, {
        {"Shifted Column", (t)=>
            Table.FromColumns(
                Table.ToColumns(t)
                    & {{null} & List.RemoveLastN(t[LeaseID],1)},{"UnitID","LeaseID","Prior Lease"}),
                type table[UnitID=text,LeaseID=Int64.Type, Prior Lease = Int64.Type]}
    }),
    #"Expanded Shifted Column" = Table.ExpandTableColumn(#"Group UnitID", "Shifted Column", {"LeaseID", "Prior Lease"})
in
    #"Expanded Shifted Column"

 

Results

ronrsnfld_0-1707938208013.png

 

 

 

View solution in original post

6 REPLIES 6
ronrsnfld
Super User
Super User

Group by UnitID, then add a shifted column to each subtable.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQw0HU0VNJRMlSK1UHiGqFyjVG5JnCuE6peJ1TFYK4pKtcMwTVCVWyEarIRpmILpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [UnitID = _t, LeaseID = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"UnitID", type text}, {"LeaseID", Int64.Type}}),

//Group by UnitID
// then add Shifted Column to each sub table and re-expand
    #"Group UnitID" = Table.Group(#"Changed Type",{"UnitID"}, {
        {"Shifted Column", (t)=>
            Table.FromColumns(
                Table.ToColumns(t)
                    & {{null} & List.RemoveLastN(t[LeaseID],1)},{"UnitID","LeaseID","Prior Lease"}),
                type table[UnitID=text,LeaseID=Int64.Type, Prior Lease = Int64.Type]}
    }),
    #"Expanded Shifted Column" = Table.ExpandTableColumn(#"Group UnitID", "Shifted Column", {"LeaseID", "Prior Lease"})
in
    #"Expanded Shifted Column"

 

Results

ronrsnfld_0-1707938208013.png

 

 

 

@ronrsnfld I know that my data is not sorted correctly but that should be easy to rectify, I think this is what I was looking for.

 

Thank you!

@ronrsnfld Thank you again, this solution has been working amazing!

 

Are you able to help me again by adding another column to this for the reverse? Meaning showing me the next lease?

Actually, I just played around some more and figured it out:

 

#"Grouped Rows" = Table.Group(#"Removed Columns1", {"UnitID"}, {
        {"Shifted Column", (t)=>
            Table.FromColumns(
                Table.ToColumns(t)
                    & {{null} & List.RemoveLastN(t[RMLEASE],1)} & {List.RemoveFirstN(t[RMLEASE],1) & {null}},{"UnitID","RMLEASE","Prior Lease", "Next Lease"}),
                type table[UnitID=text,RMLEASE=Int64.Type, Prior Lease = Int64.Type, Next Lease = Int64.Type]}       
    }),

 

michaelu1
Advocate II
Advocate II

@HotChilli 

Thats an interesting solution which should work. I'm a little hesitant though, as the the SQL query to generate the list of tenants is a rather large query so I would like to avoid having to load it twice.

 

Is there any way to accomplish this within 1 query?

HotChilli
Community Champion
Community Champion

You could start with 2 versions of the table.

In version 1, add an Index column (from 0)

In version 2, add an Index column (from 1).

Merge the 2 tables (on Index AND UnitID) using Left Outer join with Version 1 as first table.

Expand to bring back Lease ID

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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 Solution Authors