Forum Discussion

lcmbany's avatar
lcmbany
New Member
2 years ago

How to replace some NULL values by text values from another column in the same rows

Hello

In the case of two columns, one "Product Name" containing non-zero values and the other "Category Name" containing 70% of values named "null", example on 5 lines: "Product Name" line 1 = Levi's 501 jeans, "Category Name" line 1 = Apparel; "Product Name" line 2 = Toyota Corolla, "Category Name" line 2 = Automotive; "Product Name" line 3 = iPhone 12, "Category Name" line 3 = Electronics; "Product Name" line 4 = Chevrolet Cruze, "Category Name" line 4 = null; "Product Name" line 5 = iPhone 14, "Category Name" line 5 = null,
How do I replace all null values with the non-null values in the Product Name column in the same row as the null values in the Category Name column?

5 Replies

  • You can also use this formula

    let
    Source = Table.FromRows({
    {"Levi's 501 jeans", "Apparel"},
    {"Toyota Corolla", "Automotive"},
    {"iPhone 12", "Electronics"},
    {"Chevrolet Cruze", null},
    {"iPhone 14", null}
    }
    ),
    Replaced = Table.ReplaceValue(Source,null,each _[Column1],(a,b,c)=>a??c,{"Column2"})
    in
    Replaced

    • lcmbany's avatar
      lcmbany
      New Member

      Hello Omid,
      Maybe I wasn't clear. Let me tell you what I need :

      IDProduct NameCategory NameCustomer Name
      1Pepsi ColaBeveragesYannick
      2Honda CivicAutomotiveOliver
      3Zara clothingApparelKurt
      4Rolex watchesAccessories/JewelryJoel
      5Bose headphonesElectronicsIsabelle
      6Levi's 501 jeansnullEmerode
      7Toyota CorollanullNoah
      8Pepsi ColanullAlbert
      9PlayStation 5nullSisca
      10Honda CivicnullJessy
      11Starbucks CoffeenullMetthew

      I need for each "null" value takes a valid and good category name.

      How to do this?

      For example :

      Row 1 Pepsi Cola = Berevages. But for the same product at the Row 8 = null?

      Row 2 Honda Civic = Automobile. But at the Row 7, Toyota Corolla = null?

      Row 9, we know that the PlayStation 5 element is on the Electronics category, same as Bose headphones. But on this row, we see null.

      I'm wondering how to make sure that each "null" value is replaced with the correct category of each product in the "null" row?

      • Omid_Motamedise's avatar
        Omid_Motamedise
        Super User

        Use this formula, Source is your table

        let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Grouped Rows" = Table.Group(Source, {"Product Name"}, {{"Count", each Table.FillUp(Table.FillDown(_,{"Category Name"}),{"Category Name"})}}),
        #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"ID", "Category Name", "Customer Name"}, {"ID", "Category Name", "Customer Name"})
        in
        #"Expanded Count"

  • Hi lcmbany 

    If I've understood correctly, you want to replace each null Category Name value with the Product Name value in the same row.

     

    Here's an example:

     

    let
      Source = #table(
        type table [Product Name = text, Category Name = text],
        {
          {"Levi's 501 jeans", "Apparel"},
          {"Toyota Corolla", "Automotive"},
          {"iPhone 12", "Electronics"},
          {"Chevrolet Cruze", null},
          {"iPhone 14", null}
        }
      ),
      #"Replace null Category Names" = Table.ReplaceValue(
        Source,
        null,
        (_) as text => [Product Name],
        Replacer.ReplaceValue,
        {"Category Name"}
      )
    in
      #"Replace null Category Names"

     

    Does this work as expected?

     

    • lcmbany's avatar
      lcmbany
      New Member

      Hi OwenAuger 

      Sorry for my silent, was very busy at work!

      I tried your code, but got result error.

      Same as my firt answer to Omid_Motamedise, maybe I wasn't clear. Let me tell you what I need :

      IDProduct NameCategory NameCustomer Name
      1Pepsi ColaBeveragesYannick
      2Honda CivicAutomotiveOliver
      3Zara clothingApparelKurt
      4Rolex watchesAccessories/JewelryJoel
      5Bose headphonesElectronicsIsabelle
      6Levi's 501 jeansnullEmerode
      7Toyota CorollanullNoah
      8Pepsi ColanullAlbert
      9PlayStation 5nullSisca
      10Honda CivicnullJessy
      11Starbucks CoffeenullMetthew

      I need for each "null" value takes a valid and good category name.

      How to do this?

      For example :

      Row 1 Pepsi Cola = Berevages. But for the same product at the Row 8 = null?

      Row 2 Honda Civic = Automobile. But at the Row 7, Toyota Corolla = null?

      Row 9, we know that the PlayStation 5 element is on the Electronics category, same as Bose headphones. But on this row, we see null.

      I'm wondering how to make sure that each "null" value is replaced with the correct category of each product in the "null" row?