Forum Discussion

JoMont's avatar
JoMont
Frequent Visitor
10 months ago
Solved

How to replace text using a lookup table without duplicating text

I have the following table

 

Medical Registration NumberDiscipline TypeFacility TypeStart DateEnd DateDiscipline Group
MED1043Primary Health
ACCHS
Primary Health4/05/202321/06/2023Community
ACCHS
MED1047Primary HealthSeveral Primary Health clinics2/11/20234/02/2024Community
MED1048Obstetrics and GynaecologyHospital8/01/202321/01/2023Obstetrics and Gynaecology and Obstetrics and Gynaecology and Gynaecology
MED1052ObstetricsHospital21/08/202329/10/2023Obstetrics and Gynaecology
MED1054Primary HealthSeveral Primary Health clinics5/02/20241/05/2024Community

 

The Discipline Group column is generated from a lookup table with the following entries (among others)

 

Detailed DisciplineDiscipline Category
Primary HealthCommunity
Primary Health ACCHSCommunity
Rural GPCommunity
GynaecologyObstetrics and Gynaecology
Gynaecology (only)Obstetrics and Gynaecology
Obstetrics & GynaecologyObstetrics and Gynaecology
Obstetrics (only)Obstetrics and Gynaecology
Obstetrics and GynaecologyObstetrics and Gynaecology

 

I have the following code that adds the Discipline Group column, based on the value in the Discipline Type column:

#"Add Discipline Group" = Table.AddColumn(#"Removed Columns", "Discipline Group", each List.Accumulate(
Table.ToRecords(Discipline_Type_Lookup), [Discipline Type],( valueToReplace, replaceOldNewRecord ) => Text.Replace( valueToReplace, replaceOldNewRecord[Detailed Discipline], replaceOldNewRecord[Discipline Category])
))
in
#"Add Discipline Group"

 

How do I get an exact match, so that "Obstetrics and Gynaecology" in the Discipline Type column does not become "Obstetrics and Gynaecology and Obstetrics and Gynaecology and Gynaecology" in the Discipline Group column, and is instead just "Obstetrics and Gynaecology"? This is also happening with "Primary Health" and many more entries in my list ("Surgery", "General Surgery" etc).

 

There are currently 141 entries in the Detailed Discipline column, so I definitely need a list function rather than manual replacements.

 

Thanks

 

  • Hi JoMont ,
    You can use the List.PositionOf function which shall give you the exact match according to the LookUps. I'll attach the imageşof the output and the file link below. Let me know if I have understood your query correctly and in case of any doubts as well.


    Thanks,

3 Replies

  • JoMont's avatar
    JoMont
    Frequent Visitor

    Thank you, this has worked perfectly! Really appreciate your help.

  • Hi JoMont ,
    You can use the List.PositionOf function which shall give you the exact match according to the LookUps. I'll attach the imageşof the output and the file link below. Let me know if I have understood your query correctly and in case of any doubts as well.


    Thanks,

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    #"Add Discipline Group" = let a=List.Buffer(Table.ToRows(Table.Group(Discipline_Type_Lookup,"Discipline Category",{"n",each [Detailed Discipline]}))) in Table.AddColumn(#"Removed Columns", "Discipline Group", each List.Select(a,(x)=> List.Contains(x{1},[Discipline Type])){0}?{0}?)
    in
    #"Add Discipline Group"