Splitting multiple choice data into separate columns

%3CLINGO-SUB%20id%3D%22lingo-sub-3365924%22%20slang%3D%22en-US%22%3ESplitting%20multiple%20choice%20data%20into%20separate%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3365924%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20big%20data%20set%20and%20one%20of%20the%20columns%20contains%20data%20that%20was%20populated%20from%20a%20multiple%20choice%20answer%20on%20a%20form%20where%20the%20data%20came%20from.%20The%20column%20in%20question%20looks%20something%20like%20below.%20I%20want%20to%20go%20from%20something%20like%20this%3A%3C%2FP%3E%3CTABLE%20width%3D%22233%22%3E%3CTBODY%3E%3CTR%3E%3CTD%20width%3D%22233%22%3E%3CP%3E%3CSTRONG%3E%3CU%3EReferrals%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20width%3D%22233%22%3E%3CP%3EChiropractor%2C%20PCP%2C%20X-Ray%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20width%3D%22233%22%3E%3CP%3EMRI%2C%20Acupuncture%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20width%3D%22233%22%3E%3CP%3EPhysical%20Therapy%2C%20MRI%2C%20Other%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20width%3D%22233%22%3E%3CP%3EInjection%2C%20PCP%2C%20X-Ray%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20end%20up%20with%20something%20like%20this%3A%3C%2FP%3E%3CTABLE%20width%3D%22575%22%3E%3CTBODY%3E%3CTR%3E%3CTD%20width%3D%22103%22%3E%3CP%3E%3CSTRONG%3E%3CU%3EAcupuncture%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2293%22%3E%3CP%3E%3CSTRONG%3E%3CU%3EChiropractic%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2268%22%3E%3CP%3E%3CSTRONG%3E%3CU%3EInjection%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2236%22%3E%3CP%3E%3CSTRONG%3E%3CU%3EMRI%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2251%22%3E%3CP%3E%3CSTRONG%3E%3CU%3EOther%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2247%22%3E%3CP%3E%3CSTRONG%3E%3CU%3EPCP%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%22113%22%3E%3CP%3E%3CSTRONG%3E%3CU%3EPhysical%20Therapy%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2264%22%3E%3CP%3E%3CSTRONG%3E%3CU%3EX-Ray%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20width%3D%22103%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2293%22%3E%3CP%3Ex%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2268%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2236%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2251%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2247%22%3E%3CP%3Ex%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%22113%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2264%22%3E%3CP%3Ex%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20width%3D%22103%22%3E%3CP%3Ex%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2293%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2268%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2236%22%3E%3CP%3Ex%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2251%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2247%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%22113%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2264%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20width%3D%22103%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2293%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2268%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2236%22%3E%3CP%3Ex%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2251%22%3E%3CP%3Ex%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2247%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%22113%22%3E%3CP%3Ex%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2264%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20width%3D%22103%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2293%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2268%22%3E%3CP%3Ex%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2236%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2251%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2247%22%3E%3CP%3Ex%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%22113%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FTD%3E%3CTD%20width%3D%2264%22%3E%3CP%3Ex%3C%2FP%3E%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3E%3CBR%20%2F%3EText%20to%20Column%20will%20break%20the%20data%20into%20separate%20columns%2C%20but%20I%20need%20a%20column%20for%20every%20possible%20Referral%20type%20and%20an%20x%20to%20indicate%20their%20selection.%3CBR%20%2F%3EI%E2%80%99m%20just%20not%20sure%20how%20to%20get%20it%20done%20.%20I%E2%80%99ve%20Googled%20so%20many%20of%20videos%20but%20I%20haven%E2%80%99t%20found%20an%20example%20that%20matches%20my%20situation.%20I%20can%20create%20the%20columns%20manually%20and%20through%20formulas%20populate%20the%20rows%2C%20but%20I%20was%20hoping%20to%20find%20a%20solution%20using%20Power%20Query.%26nbsp%3B%20Just%20not%20sure%20how%20to%20do%20it.%3CBR%20%2F%3EAny%20assistance%20you%20could%20provide%20would%20be%20greatly%20appreciated!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3365924%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3366003%22%20slang%3D%22en-US%22%3ERe%3A%20Splitting%20multiple%20choice%20data%20into%20separate%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3366003%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1388577%22%20target%3D%22_blank%22%3E%40Keith_Walworth%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet's%20say%20Referrals%20is%20in%20B1%20and%20the%20headings%20Acupuncture%2C%20Chiropractor%20etc.%20in%20C1%2C%20D1%2C%20...%3C%2FP%3E%0A%3CP%3EEnter%20the%20following%20formula%20in%20C2%3A%3C%2FP%3E%0A%3CP%3E%3DIF(ISNUMBER(SEARCH(C%241%2C%24B2))%2C%22x%22%2C%22%22)%3C%2FP%3E%0A%3CP%3EFill%20to%20the%20right%2C%20then%20down%20(or%20vice%20versa).%3C%2FP%3E%0A%3CP%3ESee%20attached%20sample%20workbook.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3373059%22%20slang%3D%22en-US%22%3ERe%3A%20Splitting%20multiple%20choice%20data%20into%20separate%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3373059%22%20slang%3D%22en-US%22%3EThanks%20Hans.%20I%20created%20a%20similar%20solution%20using%20a%20slightly%20different%20formula%2C%20by%20what%20I'm%20really%20trying%20to%20figure%20out%20is%20whether%20I%20can%20get%20around%20creating%20the%20columns%20manually%20and%20creating%20them%20automatically%20and%20then%20populating%20them%20via%20something%20like%20power%20query.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3373144%22%20slang%3D%22en-US%22%3ERe%3A%20Splitting%20multiple%20choice%20data%20into%20separate%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3373144%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1388577%22%20target%3D%22_blank%22%3E%40Keith_Walworth%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI'll%20leave%20that%20for%20someone%20who%20knows%20PowerQuery.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3374076%22%20slang%3D%22en-US%22%3ERe%3A%20Splitting%20multiple%20choice%20data%20into%20separate%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3374076%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1388577%22%20target%3D%22_blank%22%3E%40Keith_Walworth%3C%2FA%3E%26nbsp%3BPlease%20find%20two%20possible%20PQ%20solutions%20in%20the%20attached%20workbook.%3C%2FP%3E%3CP%3EOne%20is%20not%20dynamic%2C%20as%20it%20will%20split%20the%20referrals%20at%20every%20comma%20based%20on%20what%20it%20found%20when%20I%20created%20the%20script.%20And%20it%20hard-codes%20the%20new%20column%20names.%20If%20the%20number%20of%20items%20on%20any%20of%20the%20rows%20increases%20next%20time%2C%20the%20extra%20items%20will%20be%20ignored%20and%20you%20would%20have%20to%20manually%20adjust%20the%20script.%20But%20most%20of%20the%20script%20was%20created%20by%20just%20clicking%20in%20the%20user%20interface%20(except%20for%20the%20one%20step%20where%20you%20add%20an%20%22X%22%2C%20which%20requires%20some%20typing).%20And%20it's%20not%20very%20complicated%20to%20fix%20the%20step%20that%20makes%20the%20split.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20second%20solution%20is%20dynamic.%20I%20finds%20out%20how%20many%20items%20are%20included%20in%20each%20row%2C%20finds%20the%20highest%20number%20and%20splits%20the%20column%20into%20the%20exact%20number%20of%20columns%20needed.%20But%2C%20it%20requires%20some%20manipulation%20of%20the%20underlying%20M-code.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Riny_van_Eekelen_0-1652506916232.png%22%20style%3D%22width%3A%20549px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F371628i546F81C8A300896F%2Fimage-dimensions%2F549x81%3Fv%3Dv2%22%20width%3D%22549%22%20height%3D%2281%22%20role%3D%22button%22%20title%3D%22Riny_van_Eekelen_0-1652506916232.png%22%20alt%3D%22Riny_van_Eekelen_0-1652506916232.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3375474%22%20slang%3D%22en-US%22%3ERe%3A%20Splitting%20multiple%20choice%20data%20into%20separate%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3375474%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%20target%3D%22_blank%22%3E%40Riny_van_Eekelen%3C%2FA%3E%26nbsp%3B%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1388577%22%20target%3D%22_blank%22%3E%40Keith_Walworth%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20more%20variant%2C%20bit%20more%20coding%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20837px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F371678i4FD76C5D5E1EE635%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EScript%20is%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3Elet%0A%20%20%20%20Source%20%3D%20Excel.CurrentWorkbook()%7B%5BName%3D%22Table1%22%5D%7D%5BContent%5D%2C%0A%20%20%20%20TextToList%20%3D%20Table.AddColumn(%0A%20%20%20%20%20%20%20%20Source%2C%0A%20%20%20%20%20%20%20%20%22Custom%22%2C%0A%20%20%20%20%20%20%20%20each%20List.Transform(Text.Split(%5BReferrals%5D%2C%20%22%2C%22)%2C%20Text.Trim)%20)%2C%0A%20%20%20%20a%20%3D%20List.Sort(%20List.Distinct(%20List.Combine(%20TextToList%5BCustom%5D)%20)%20)%2C%0A%20%20%20%20IfContains%20%3D%20Table.AddColumn(TextToList%2C%0A%20%20%20%20%20%20%20%20%22Flags%22%2C%0A%20%20%20%20%20%20%20%20each%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20b%20%3D%5BCustom%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20c%3DList.Transform(a%2C%20each%20if%20List.Contains(b%2C%20_)%20then%20%22x%22%20else%20%22%22%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%5Bc%5D%20)%2C%0A%20%20%20%20toTable%20%3D%20Table.FromColumns(%20List.Zip(%20IfContains%5BFlags%5D%20)%2C%20a)%0Ain%0A%20%20%20%20toTable%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

I have a big data set and one of the columns contains data that was populated from a multiple choice answer on a form where the data came from. The column in question looks something like below. I want to go from something like this:

Referrals

Chiropractor, PCP, X-Ray

MRI, Acupuncture

Physical Therapy, MRI, Other

Injection, PCP, X-Ray

 

And end up with something like this:

Acupuncture

Chiropractic

Injection

MRI

Other

PCP

Physical Therapy

X-Ray

 

x

 

 

 

x

 

x

x

 

 

x

 

 

 

 

 

 

 

x

x

 

x

 

 

 

x

 

 

x

 

x


Text to Column will break the data into separate columns, but I need a column for every possible Referral type and an x to indicate their selection.
I’m just not sure how to get it done . I’ve Googled so many of videos but I haven’t found an example that matches my situation. I can create the columns manually and through formulas populate the rows, but I was hoping to find a solution using Power Query.  Just not sure how to do it.
Any assistance you could provide would be greatly appreciated!

5 Replies

@Keith_Walworth 

Let's say Referrals is in B1 and the headings Acupuncture, Chiropractor etc. in C1, D1, ...

Enter the following formula in C2:

=IF(ISNUMBER(SEARCH(C$1,$B2)),"x","")

Fill to the right, then down (or vice versa).

See attached sample workbook.

Thanks Hans. I created a similar solution using a slightly different formula, by what I'm really trying to figure out is whether I can get around creating the columns manually and creating them automatically and then populating them via something like power query.

@Keith_Walworth 

I'll leave that for someone who knows PowerQuery.

@Keith_Walworth Please find two possible PQ solutions in the attached workbook.

One is not dynamic, as it will split the referrals at every comma based on what it found when I created the script. And it hard-codes the new column names. If the number of items on any of the rows increases next time, the extra items will be ignored and you would have to manually adjust the script. But most of the script was created by just clicking in the user interface (except for the one step where you add an "X", which requires some typing). And it's not very complicated to fix the step that makes the split.

 

The second solution is dynamic. I finds out how many items are included in each row, finds the highest number and splits the column into the exact number of columns needed. But, it requires some manipulation of the underlying M-code.

Riny_van_Eekelen_0-1652506916232.png

 

 

@Riny_van_Eekelen , @Keith_Walworth 

One more variant, bit more coding

image.png

Script is

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    TextToList = Table.AddColumn(
        Source,
        "Custom",
        each List.Transform(Text.Split([Referrals], ","), Text.Trim) ),
    a = List.Sort( List.Distinct( List.Combine( TextToList[Custom]) ) ),
    IfContains = Table.AddColumn(TextToList,
        "Flags",
        each [
                b =[Custom],
                c=List.Transform(a, each if List.Contains(b, _) then "x" else "" )
             ][c] ),
    toTable = Table.FromColumns( List.Zip( IfContains[Flags] ), a)
in
    toTable