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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
jmupton
New Member

Return historic data using PowerBi query

Hi, i'm trying to display historic data based on several filters and struggling to get a search query that will work.

 

For example I have a table with;

WORKNOLOCATIONJOB PLANSTATUSDATEPREVIOUS CLOSED DATE
1234AB-12E1001OPEN01/02/22??
4321AB-12E2001CLOSED01/01/22 
5678AB-12E1001CLOSED01/11/21 
8765AB-12E2001 CLOSED 01/01/21  

 

For WORKNO 1234 I want to search the previously completed WORKNO's and return the DATE the most recent E1001 JOB PLAN was CLOSED, for the above table this should return 01/11/21. I have tried several functions related to last completed dates and max values, but as the most recent E1001 does not fit either of those queries i'm struggling to return the correct value.

 

Any help would be appreciated!

2 REPLIES 2
JW_van_Holst
Resolver IV
Resolver IV

Your test set is somewhat limited, but this should do the trick.

Picture1.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bcxJCoAwEETRq0ivlaTaTFuH7MQILkPufw0bZ8TVh+JRORO4NVRT1zdgaYTWkKYlzhINpVkxU6kzmZbxpnzQYUprHE+MC1vnw8/vG0Mwdhy8s9/n6tbV8y1rKRs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [WORKNO = _t, LOCATION = _t, #"JOB PLAN" = _t, STATUS = _t, DATE = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}}),
    Base = Table.Buffer(Table.Sort(#"Changed Type",{{"DATE", Order.Descending}})),
    #"Grouped Rows" = Table.Group(Base, {"JOB PLAN"}, {{"Data", each _, type table [WORKNO=nullable text, LOCATION=nullable text, JOB PLAN=nullable text, STATUS=nullable text, DATE=nullable date]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Data], "Index", 0, 1)),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
    IndexTable = Table.Buffer(Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"WORKNO", "LOCATION", "JOB PLAN", "STATUS", "DATE", "Index"}, {"WORKNO", "LOCATION", "JOB PLAN", "STATUS", "DATE", "Index"})),
    #"Added Custom1" = Table.AddColumn(IndexTable, "Custom", (outer)=> Table.SelectRows(IndexTable, each ([JOB PLAN]= outer[JOB PLAN]) and ([Index] = outer[Index]+1))[DATE]{0}),
    #"Replaced Errors" = Table.ReplaceErrorValues(#"Added Custom1", {{"Custom", null}})
in
    #"Replaced Errors"

 

Thanks for the response JW, looks like this will work, appreciate the test data set was simplified as couldn't post the real set as over 14mil rows!

I have followed through the steps above with my dataset, when i get to the following step;

 #"Added Custom1" = Table.AddColumn(IndexTable, "Custom", (outer)=> Table.SelectRows(IndexTable, each ([JOB PLAN]= outer[JOB PLAN]) and ([Index] = outer[Index]+1))[DATE]{0}),

I do not get Error or date values as your example, the 'Custom' column just displays 'Table' in every cell, no actual values.

 

The final approach is to run this for multiple locations with the same JOB PLAN, will this approach still work if the dataset looks like the below?

 

WORKNOLOCATIONJOB PLANSTATUSDATEPREVIOUS CLOSED DATE
1234AB-12E1001OPEN01/02/22??
4321AB-12E2001CLOSED01/01/22 
5678AB-12E1001CLOSED01/11/21 
8765AB-12E2001 CLOSED 01/01/21  
2345CD-34E1001OPEN05/06/22??
6789CD-34E1001CLOSED01/05/21 

 

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.