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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Hyuna_8000
Frequent Visitor

Conditionally fill blank cells based on another column in Power Query

Hi,

 

I have a table that has two columns (Username and User Email). I was wondering how can I fill in the blank cells in "User Email" based on the following conditions:
1. If the same username appears in duplicated/multiple rows, one of them has email and the other has email as blank, fill the blank cells in "User Email" column with the existing emails for the same username
2. If the user name only appear in one row and its "User Email" is blank, then "User Email" as blank

I've tried to use group by rows, Fill down but I haven't been able to figure out correctly in Power Query. Any tips would be appreciated. Thank you.

Original table:

UsernameUser Email
Amy Shaneamy.shane@123.com
Brat Pittbrad.pitt@123.com
Katy Perrykaty.perry@123.com
Tom Bradytom.brady@123.com
Vince Lawrancev.lawrance@123.com
Tom Brady 
Amy Shane 
Jay Chou 

 

Expected Output:

UsernameUser Email
Amy Shaneamy.shane@123.com
Brat Pittbrad.pitt@123.com
Katy Perrykaty.perry@123.com
Tom Bradytom.brady@123.com
Vince Lawrancev.lawrance@123.com
Tom Bradytom.brady@123.com
Amy Shaneamy.shane@123.com
Jay Chou 



1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @Hyuna_8000 

Here's an example using a self-join.

Create blank query and paste into advanced editor:

 

let
  Source = #table(
    type table [Username = text, User Email = text],
    {
      {"Amy Shane", "amy.shane@123.com"},
      {"Brat Pitt", "brad.pitt@123.com"},
      {"Katy Perry", "katy.perry@123.com"},
      {"Tom Brady", "tom.brady@123.com"},
      {"Vince Lawrance", "v.lawrance@123.com"},
      {"Tom Brady", null},
      {"Amy Shane", null},
      {"Jay Chou", null}
    }
  ),
  #"Self-join" = Table.NestedJoin(
    Source,
    {"Username"},
    Source,
    {"Username"},
    "SelfJoin",
    JoinKind.LeftOuter
  ),
  #"Aggregated SelfJoin" = Table.AggregateTableColumn(
    #"Self-join",
    "SelfJoin",
    {{"User Email", List.Min, "User Email 2", type text}}
  ),
  #"Use non-null email" = Table.CombineColumns(
    #"Aggregated SelfJoin",
    {"User Email", "User Email 2"},
    // Custom Combiner function that returns first non-null value of the two columns
    (Columns) as nullable text => Columns{0} ?? Columns{1},
    "User Email"
  )
in
    #"Use non-null email"

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

1 REPLY 1
OwenAuger
Super User
Super User

Hi @Hyuna_8000 

Here's an example using a self-join.

Create blank query and paste into advanced editor:

 

let
  Source = #table(
    type table [Username = text, User Email = text],
    {
      {"Amy Shane", "amy.shane@123.com"},
      {"Brat Pitt", "brad.pitt@123.com"},
      {"Katy Perry", "katy.perry@123.com"},
      {"Tom Brady", "tom.brady@123.com"},
      {"Vince Lawrance", "v.lawrance@123.com"},
      {"Tom Brady", null},
      {"Amy Shane", null},
      {"Jay Chou", null}
    }
  ),
  #"Self-join" = Table.NestedJoin(
    Source,
    {"Username"},
    Source,
    {"Username"},
    "SelfJoin",
    JoinKind.LeftOuter
  ),
  #"Aggregated SelfJoin" = Table.AggregateTableColumn(
    #"Self-join",
    "SelfJoin",
    {{"User Email", List.Min, "User Email 2", type text}}
  ),
  #"Use non-null email" = Table.CombineColumns(
    #"Aggregated SelfJoin",
    {"User Email", "User Email 2"},
    // Custom Combiner function that returns first non-null value of the two columns
    (Columns) as nullable text => Columns{0} ?? Columns{1},
    "User Email"
  )
in
    #"Use non-null email"

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors