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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Anonymous
Not applicable

If and query: if ID is in one table and based on a date before, and other criteria

Hi everyone

 

this is my first app i have done from scratch - please bear with me.

I have two datasets in it, one current and one historic. these are waitlist tables of patients.

 

i want to add a flag to the current table if a patient (ID field) was not in the previous snapshot from the historic table and didnt start their pathway on the current so i would call this a 'pop in', ie. they have popped onto the waitlist from nowhere.

 

i have been searching around this site and think im nearly there but its still not right:

 

popin = if(max(Historic[SnapShotDate]) <today() 
&& (not( PTL[RTTPathwayID] ) IN DISTINCT  (Historic[RTTPathwayID]) )
&& PTL[RTTStartDate] < max(Historic[SnapShotDate])
, "y" ,"n")
 
logically i thought this would work - any help appreciated 😉
thanks Liz
1 ACCEPTED SOLUTION

 

i also dont want to include them as a pop in if their RTT start date is in the last 2 weeks

 

Please define what you mean by "last two weeks".

 

Apart from that here is the basic approach:

Table Snapshots:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvIKMjYwNzEwMFPSUTKw0Dcw0TcyMDIEcszNgYSpsSmQNDQACYQG+fv4u0cCWQE5iXl5qSkKzolFqVikjcz0DSxBxhgpxergtQJkuCmYSaIVhkbEWmECssLQhHQrDEyJtcIYZIWBORkBZakPNAliRSwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [RTTPathwayID = _t, RTTStartDate = _t, WeeksWaited = _t, RTTWaitDays = _t, SpecialtyCode = _t, Specialty = _t, Division = _t, RTTSpecialtyCode = _t, RTTSpecialty = _t, SnapShotDate = _t]),
    #"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"SnapShotDate", type date}}, "en-GB")
in
    #"Changed Type with Locale"

 

Table Current:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pc9NCgIxDAXgq8i4FfqaNOl0Oaj4gzpSx4Wox/D+Nl0IsxlQN4+25H2k93uT95nhRRI1i8aL8+QIhHLRkEomtnf4ktfcH/rNrZxIHZLNWWl+cl3zXFSKkgTyaiPBoVLWlNiWbCvlzR5ydz12s8cLIJ31edj252692i0vUzojCNRAX0b4o0vVzfV18/VpmGZiAGxJtA7hw7CUjPGb/zIkcau1MqL4J0oRrMIjyn4U9TtKOYD+o55v", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [RTTPathwayID = _t, RTTStartDate = _t, RTTWaitDays = _t, Weeks = _t, RTTSpecialtyCode = _t, RTTSpecialty = _t, LastUpdate = _t, #"in" = _t]),
    #"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"RTTStartDate", type date},{"LastUpdate", type date}}, "en-GB"),
    #"Added Custom" = Table.AddColumn(#"Changed Type with Locale", "Custom", (k)=> Table.FirstN(Table.SelectRows(Snapshots,each [RTTPathwayID]=k[RTTPathwayID] and [SnapShotDate]<k[LastUpdate]),2))
in
    #"Added Custom"

 

Brief explanation: 

#"Added Custom" = Table.AddColumn(#"Changed Type with Locale", "Custom", (k)=> Table.FirstN(Table.SelectRows(Snapshots,each [RTTPathwayID]=k[RTTPathwayID] and [SnapShotDate]<k[LastUpdate]),2))

Map the Snapshots table to the Current Table based on [RTTPathwayID]. Filter for dates that are older than the latest snapshot.  Grab the top two rows.

 

View solution in original post

3 REPLIES 3
lbendlin
Super User
Super User

Please provide sanitized sample data that fully covers your issue.
https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-...
Please show the expected outcome based on the sample data you provided.

https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

Anonymous
Not applicable

Hi thank you; hope I done this right, inserting table:

 

first table is my current PTL.  i want to be able to flag based on RTTPathwayID whether or not they were on the previous 2 snapshots. so i have made a 2nd table showing 1 example that was not on last week but was on the previous week -so this is not a true popin. 

i also dont want to include them as a pop in if their RTT start date is in the last 2 weeks.

 

So a 'Popin' is any pathwayID that is not on previous 2 snapshots or has an RTT startdate more than 2/3 weeks ago..

thanks

 

 

RTTPathwayIDRTTStartDateRTTWaitDaysWeeksRTTSpecialtyCodeRTTSpecialtyLastUpdatein
RJR301559215/12/202064993101UROLOGY26/09/2022#N/A
RJR295421624/02/202157883110TRAUMA & ORTHOPAEDICS26/09/2022#N/A
RJR304506116/03/202155880120ENT26/09/2022#N/A
RJR307400608/04/202153577101UROLOGY26/09/2022#N/A
RJR305938610/04/202153377101UROLOGY26/09/2022#N/A
RJR305960413/04/202153076101UROLOGY26/09/2022#N/A
RJR306340213/04/202153076101UROLOGY26/09/2022#N/A

 

RTTPathwayIDRTTStartDateWeeksWaitedRTTWaitDaysSpecialtyCodeSpecialtyDivisionRTTSpecialtyCodeRTTSpecialtySnapShotDate
RJR307400608/04/202177535101UROLOGYPlanned Care101UROLOGY26/09/2022
RJR307400608/04/202175521101UROLOGYPlanned Care101UROLOGY12/09/2022
RJR307400608/04/202174514101UROLOGYPlanned Care101UROLOGY05/09/2022
RJR307400608/04/202173507101UROLOGYPlanned Care101UROLOGY29/08/2022

 

 

i also dont want to include them as a pop in if their RTT start date is in the last 2 weeks

 

Please define what you mean by "last two weeks".

 

Apart from that here is the basic approach:

Table Snapshots:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCvIKMjYwNzEwMFPSUTKw0Dcw0TcyMDIEcszNgYSpsSmQNDQACYQG+fv4u0cCWQE5iXl5qSkKzolFqVikjcz0DSxBxhgpxergtQJkuCmYSaIVhkbEWmECssLQhHQrDEyJtcIYZIWBORkBZakPNAliRSwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [RTTPathwayID = _t, RTTStartDate = _t, WeeksWaited = _t, RTTWaitDays = _t, SpecialtyCode = _t, Specialty = _t, Division = _t, RTTSpecialtyCode = _t, RTTSpecialty = _t, SnapShotDate = _t]),
    #"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"SnapShotDate", type date}}, "en-GB")
in
    #"Changed Type with Locale"

 

Table Current:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pc9NCgIxDAXgq8i4FfqaNOl0Oaj4gzpSx4Wox/D+Nl0IsxlQN4+25H2k93uT95nhRRI1i8aL8+QIhHLRkEomtnf4ktfcH/rNrZxIHZLNWWl+cl3zXFSKkgTyaiPBoVLWlNiWbCvlzR5ydz12s8cLIJ31edj252692i0vUzojCNRAX0b4o0vVzfV18/VpmGZiAGxJtA7hw7CUjPGb/zIkcau1MqL4J0oRrMIjyn4U9TtKOYD+o55v", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [RTTPathwayID = _t, RTTStartDate = _t, RTTWaitDays = _t, Weeks = _t, RTTSpecialtyCode = _t, RTTSpecialty = _t, LastUpdate = _t, #"in" = _t]),
    #"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"RTTStartDate", type date},{"LastUpdate", type date}}, "en-GB"),
    #"Added Custom" = Table.AddColumn(#"Changed Type with Locale", "Custom", (k)=> Table.FirstN(Table.SelectRows(Snapshots,each [RTTPathwayID]=k[RTTPathwayID] and [SnapShotDate]<k[LastUpdate]),2))
in
    #"Added Custom"

 

Brief explanation: 

#"Added Custom" = Table.AddColumn(#"Changed Type with Locale", "Custom", (k)=> Table.FirstN(Table.SelectRows(Snapshots,each [RTTPathwayID]=k[RTTPathwayID] and [SnapShotDate]<k[LastUpdate]),2))

Map the Snapshots table to the Current Table based on [RTTPathwayID]. Filter for dates that are older than the latest snapshot.  Grab the top two rows.

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors