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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
yve214
Helper III
Helper III

Urgent Help with Creating a Flag column

Problem:

Please, I am trying to check if my recent admission location is the same as my recent discharge location and create a flag to help me with my other measures. How to approach this.

 

check...

1. The patient's most recent admission location

2. The patient's most recent discharge location

3. Create a flag to see if they have the same location then 1, else 0

 

Table:

patient_namepatient_Idadmission_datedischarge_daterecord_IdLocation
Mark11/1/20183/15/20191001A
John26/1/20196/3/20201002B
Tom31/1/20208/7/20201003A
Tom38/7/20209/7/20201004C
Tom39/7/202010/3/20201005A
Sarah47/2/20152/1/20181006C
Kim53/1/20167/6/20171007E
Mark13/16/20194/7/20191008A
Tom32/1/2021 1009C
Steve63/2/2019 1010 
John23/4/20225/10/20221011A

 

Expected Results:

patient_namepatient_Idlocation_flag//Explanation
Mark11most recent discharge location and new admission location is the same so flag is 1
John21recent discharge location is same as recent admission
Tom30different locations for  their recent admission and discharge record
Sarah40has only one record
Kim50has only one record
Steve60has only one record, no discharge record

 

Thank you very much in advance.

 

1 ACCEPTED SOLUTION
tamerj1
Community Champion
Community Champion

Hi @yve214 

please try

Flag =
VAR CurrentIDTable =
    CALCULATETABLE ( Table1, ALLEXCEPT ( Table1, Table1[patient_Id] ) )
VAR LastAdmissionLocation =
    MAXX ( TOPN ( 1, CurrentIDTable, Table1[admission_date] ), Table1[Location] )
VAR LastDischargeLocation =
    MAXX ( TOPN ( 1, CurrentIDTable, Table1[discharge_date] ), Table1[Location] )
RETURN
    IF ( LastAdmissionLocation = LastDischargeLocation, 1 )

View solution in original post

2 REPLIES 2
tamerj1
Community Champion
Community Champion

Hi @yve214 

please try

Flag =
VAR CurrentIDTable =
    CALCULATETABLE ( Table1, ALLEXCEPT ( Table1, Table1[patient_Id] ) )
VAR LastAdmissionLocation =
    MAXX ( TOPN ( 1, CurrentIDTable, Table1[admission_date] ), Table1[Location] )
VAR LastDischargeLocation =
    MAXX ( TOPN ( 1, CurrentIDTable, Table1[discharge_date] ), Table1[Location] )
RETURN
    IF ( LastAdmissionLocation = LastDischargeLocation, 1 )

@tamerj1 ,

 

you are a life safer. YES worked PERFECTLY. Immense thanks for your quick and effective response.

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 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.