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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
dw700d
Post Patron
Post Patron

Conditional Column

I have two columns, A parent column which gives the name of each parent and a child column that represents the type of child each parent has. I would like to create a new column within the query editor that tells me if a parent has a toddler, an infant or both. For example the new column would  show that John has “both”, Tom has “Toddler”, Mike has “infant” and Bob has “Both”

 

Parent      Child      
JohnToddler
Johninfant
Johninfant
TomToddler
TomToddler
Tom Toddler
Mikeinfant
Mikeinfant
Mikeinfant
BobToddler
Bobinfant
Bobinfant
1 ACCEPTED SOLUTION
jeffshieldsdev
Solution Sage
Solution Sage

This should work:

Note: your third "Tom" has a trailing space. Also, you may be aware Power Query string comparisons are case sensitive by default.

let
    // Your table
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPSUQrJT0nJSS1SitWBC2XmpSXmleAQCcnPRdNFjIhvZnYqqjnEiDjlJ6GZAxHBUAITiAUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Parent = _t, Child = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Parent", type text}, {"Child", type text}}),

    // Group rows by "Parent" into Data column
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Parent"}, {{"Data", each _, type table [Parent=nullable text, Child=nullable text]}}),

    // Determine Child Type by rows in Data column
    #"Added Child Type" = Table.AddColumn(#"Grouped Rows", "Child Type", each 
        if Table.ContainsAll([Data], {[Child = "Toddler"], [Child = "infant"]}) 
            then "both" 
            else if Table.Contains([Data], [Child = "Toddler"]) 
                then "Toddler" 
                else if Table.Contains([Data], [Child = "infant"]) 
                    then "infant"
                    else null,
    type text),

    // Expanded back Data to Child
    #"Expanded Data" = Table.ExpandTableColumn(#"Added Child Type", "Data", {"Child"}, {"Child"})
in
    #"Expanded Data"

 

View solution in original post

2 REPLIES 2
jeffshieldsdev
Solution Sage
Solution Sage

This should work:

Note: your third "Tom" has a trailing space. Also, you may be aware Power Query string comparisons are case sensitive by default.

let
    // Your table
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPSUQrJT0nJSS1SitWBC2XmpSXmleAQCcnPRdNFjIhvZnYqqjnEiDjlJ6GZAxHBUAITiAUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Parent = _t, Child = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Parent", type text}, {"Child", type text}}),

    // Group rows by "Parent" into Data column
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Parent"}, {{"Data", each _, type table [Parent=nullable text, Child=nullable text]}}),

    // Determine Child Type by rows in Data column
    #"Added Child Type" = Table.AddColumn(#"Grouped Rows", "Child Type", each 
        if Table.ContainsAll([Data], {[Child = "Toddler"], [Child = "infant"]}) 
            then "both" 
            else if Table.Contains([Data], [Child = "Toddler"]) 
                then "Toddler" 
                else if Table.Contains([Data], [Child = "infant"]) 
                    then "infant"
                    else null,
    type text),

    // Expanded back Data to Child
    #"Expanded Data" = Table.ExpandTableColumn(#"Added Child Type", "Data", {"Child"}, {"Child"})
in
    #"Expanded Data"

 

jianlong
Resolver I
Resolver I

1, duplicate the table under edit query
2, find one table to groupby "Parent",

3, Merge the two tables together

4, if or switch true create a new column..

jianlong_0-1645927682763.png

jianlong_1-1645927729782.png

jianlong_2-1645927866549.png

 

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

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.

Top Solution Authors