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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
jmowen
Frequent Visitor

How to create a new column to get the latest record per id prior to the record date

Hello,

 

I'm having a bit of trouble finding a solution to this problem.

 

For every "ID" I would like to state what their "Type" was at the given date for each "Jobtype"

 

I have 2 "jobtype" "S" and "H" when information for "jobtype" "H" is filled out, the "Type" is not recorded. However, that information is recorded in "jobtype "S" 

 

So what I would like to do is for every "Jobtype" that is "H" Find the Latest "Type" for that "ID" in "Jobtype" "S" prior to the "Date" of when "Jobtype" "H" was done.

 

example:
"ID" "1" had a "jobtype" "H" carried out on the 1/02/2022, to return the correct value you would find the latest record of "jobtype" "S" with an "ID" of "1" that is before 1/02/2022 and return the "Type" in this case it would be "A" 

 

Now "ID" "1" also had jobtype" "H" carried out on the 6/05/2022 to return the correct value you would find the latest record before 6/05/2022 which would be 12/03/2022, "Type" "B"

Hopefully I've explained my issue well enough. Below is an image of the current data and what the desired outcome would be.

 

Data

Job IDIDJobtypeDateTypeScore
11S9/05/2021A1050
21S9/05/2021B1050
32S9/05/2021A500
42S10/05/2021B2561
53S10/05/2021A1590
63S10/05/2021B600
74S11/05/2021B2842
85S11/05/2021B1983
91H1/02/2022  
103H2/02/2022  
114H3/02/2022  
121S12/03/2022B237
133S13/03/2022A1020
141H6/05/2022  
153H12/05/2022  

 

Outcome

Job IDIDJobtypeDateTypeScoreType 2
11S9/05/2021A1050 
21S9/05/2021B1050 
32S9/05/2021A500 
42S10/05/2021B2561 
53S10/05/2021A1590 
63S10/05/2021B600 
74S11/05/2021B2842 
85S11/05/2021B1983 
91H1/02/2022  B
103H2/02/2022  A
114H3/02/2022  B
121S12/03/2022B237 
133S13/03/2022A1020 
141H6/05/2022  B
153H12/05/2022  A

 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

Type 2 = if(Data[Jobtype]="S",BLANK(),lookupvalue(Data[Type],Data[Date],CALCULATE(MAX(Data[Date]),FILTER(data,Data[ID]=EARLIER(Data[ID])&&Data[Jobtype]="S"&&Data[Date]<EARLIER(Data[Date]))),Data[ID],Data[ID]))

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

6 REPLIES 6
lbendlin
Super User
Super User

 

 

Type 2 = 
if([Jobtype]="H",
var i = [ID]
var d = [Date]
var a = SUMMARIZE(FILTER('Data','Data'[ID]=i && 'Data'[Date] < d && 'Data'[Jobtype]="S"),'Data'[Job ID],'Data'[Score],'Data'[Type])
var b = maxx(a,[Score])
var c = filter(a,[Score]=b)
var e = TOPN(1,c,[Job ID],DESC)
return concatenatex(e,[Type])
)

 

lbendlin_0-1655686824938.png

 

or if you insist on using EARLIER

Type 2 = 
if([Jobtype]="H",
var a = SUMMARIZE(FILTER('Data','Data'[ID]=EARLIER(Data[ID]) && 'Data'[Date] < EARLIER(Data[Date]) && 'Data'[Jobtype]="S"),'Data'[Job ID],'Data'[Score],'Data'[Type])
return concatenatex(TOPN(1,filter(a,[Score]=maxx(a,[Score])),[Job ID],DESC),[Type])
)

Thanks for this it works great!

Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

Type 2 = if(Data[Jobtype]="S",BLANK(),lookupvalue(Data[Type],Data[Date],CALCULATE(MAX(Data[Date]),FILTER(data,Data[ID]=EARLIER(Data[ID])&&Data[Jobtype]="S"&&Data[Date]<EARLIER(Data[Date]))),Data[ID],Data[ID]))

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks! works greats. I'll accept this solution as it did solve my intinal problem.
I did run into an problem when using my real data however and it just comes down having multiple values returned.  I came up with a way to decide what value to keep based on some additional. If you don't mind taking a look? or would it be better to open another disscussion? 


The outcome I would like is if there are multiple Types returned it will return the Type that has the Highest score and if the score is the same it will return the Type with the highest Job ID


I've replicated the issue in the data below. 


Data

Job IDIDJobtypeDateTypeScore
11S9/05/2021A1050
21S9/05/2021B1050
32S9/05/2021A500
42S10/05/2021B2561
53S10/05/2021A1590
63S10/05/2021B600
74S11/05/2021B2842
85S11/05/2021B1983
91H1/02/2022  
103H2/02/2022  
114H3/02/2022  
121S12/03/2022B237
133S13/03/2022A1020
141H6/05/2022  
153H12/05/2022  

 

Outcome

Job IDIDJobtypeDateTypeScoreType 2
11S9/05/2021A1050 
21S9/05/2021B1050 
32S9/05/2021A500 
42S10/05/2021B2561 
53S10/05/2021A1590 
63S10/05/2021B600 
74S11/05/2021B2842 
85S11/05/2021B1983 
91H1/02/2022  B
103H2/02/2022  A
114H3/02/2022  B
121S12/03/2022B237 
133S13/03/2022A1020 
141H6/05/2022  B
153H12/05/2022  A

 

You are welcome.  If my previous reply helped, please mark that reply as Answer.  Try these calculated column formulas

To consider = if(Data[Jobtype]="S",if(CALCULATE(countrows(Data),filter(Data,Data[ID]=earlier(Data[ID])&&Data[Jobtype]="S"))=1,1,if(CALCULATE(COUNTROWS(Data),FILTER(Data,Data[ID]=EARLIER(Data[ID])&&Data[Date]<EARLIER(Data[Date]))),1,if(CALCULATE(COUNTROWS(Data),FILTER(Data,Data[ID]=EARLIER(Data[ID])&&Data[Date]=EARLIER(Data[Date])&&Data[Score]<EARLIER(Data[Score]))),1,if(CALCULATE(COUNTROWS(Data),FILTER(Data,Data[ID]=EARLIER(Data[ID])&&Data[Date]=EARLIER(Data[Date])&&Data[Score]=EARLIER(Data[Score])&&Data[Job ID]<EARLIER(Data[Job ID]))),1)))),BLANK())
Type 2 = if(Data[Jobtype]="S",BLANK(),lookupvalue(Data[Type],Data[To consider],1,Data[ID],Data[ID]))

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
lbendlin
Super User
Super User

Please provide sample data in usable format, not as a screenshot.  Screenshot is ok for expected outcome.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.