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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
mryoan04
Frequent Visitor

Determining primary value from column

Hi gurus,

 

I'm preparing a dataset in PBI dataflow. I have an Ingredients column like below and wanted to get the Primary Ingredients column that evaluating the quantity of the ingredients and generate the highest quantity ingredients as primary Ingredients. For example, row 1 generates Orange as primary ingredient as it has 15.

 

Ingredients(Result wanted) Primary Ingredients
13;Apple | 15;Orange | 3;StrawberryOrange
2;Apple | 18;Peach | 3;MelonPeach
27;Blueberry | 7;Blackberry | 1;Lemon | 9;GrapeBlueberry
1;Peach | 11;Banana | 4;Pineapple | 8;Orange | 24;BlackberryBlackberry

 

Could you please help on how to prepare that column in a query?

 

Thank you very much for your help!

2 ACCEPTED SOLUTIONS
MasonMA
Resolver II
Resolver II

Hi @mryoan04 

 

I'd encourage you to use AI to generate M code for this. Below is the code AI shared with me and it works! 

let
    Source = YourTable,
    // Step 1: Split each row into a list of records [Number, Ingredient]
    AddParsedList = Table.AddColumn(Source, "Parsed List", each 
        List.Transform(
            Text.Split([Ingredients], " | "),
            (item) => 
                let 
                    parts = Text.Split(item, ";")
                in 
                    [Number = Number.FromText(parts{0}), Ingredient = parts{1}]
        )
    ),

    // Step 2: Add a column that gets the max number for each row
    AddMaxNumber = Table.AddColumn(AddParsedList, "MaxNumber", each 
        List.Max(List.Transform([Parsed List], each _[Number]))
    ),

    // Step 3: Add a column to extract the ingredient where Number = MaxNumber
    AddPrimaryIngredient = Table.AddColumn(AddMaxNumber, "Primary Ingredient", (row) => 
        let
            records = row[Parsed List],
            maxNum = row[MaxNumber],
            match = List.Select(records, each _[Number] = maxNum)
        in
            if List.Count(match) > 0 then match{0}[Ingredient] else null
    ),

    // Step 4: Remove helper columns
    Cleanup = Table.RemoveColumns(AddPrimaryIngredient, {"Parsed List", "MaxNumber"})
in
    Cleanup

Thanks 

Mason

View solution in original post

speedramps
Community Champion
Community Champion

Click here to download the solution from Onedrive

Click here 

 

 

How it works:-

 

Input this file a csv/text file

13;Apple | 15;Orange | 3;Strawberry
2;Apple | 18;Peach | 3;Melon
27;Blueberry | 7;Blackberry | 1;Lemon | 9;Grape
1;Peach | 11;Banana | 4;Pineapple | 8;Orange | 24;Blackberry

 

Use a custom delimiter = |  and   specify 5 columns

speedramps_0-1749481762888.png

 

Add a product id

speedramps_1-1749481860396.png

 

speedramps_2-1749481903991.png

Click on the product id column.

On the top menu bar select Trasform > Unpivot > Other columns

speedramps_3-1749481981443.png

Remove the empty rows

speedramps_4-1749482023307.png

Extract the Ingredient and Quanity either side of  delimiter

speedramps_5-1749482113971.png

Remove unneed columns and change the Quantity to a number

speedramps_6-1749482179948.png

Create a new table reference the above one
 

speedramps_7-1749482267029.png

Group max quanity by product id

speedramps_8-1749482321406.png

Merge by Product ID and Quantity

 

speedramps_9-1749482362293.png

Expand the merger to get the Ingredient

speedramps_10-1749482416013.png

Well done. You now have a table with just the max ingredient for each product

 

Please click thumbs up me taking the time and effort to try help.
Then click [accept solution] if it works.

Thank you.

 

 

 

 

 

View solution in original post

3 REPLIES 3
p45cal
Super User
Super User

Another, see attached.

In this case I cater for situations when there are equal max ingredients, when all of them are listed.:

 

p45cal_0-1749506146098.png

Stages:

Add an Index:

p45cal_1-1749506261810.png

Split by the pipe symbol into multiple rows:

p45cal_2-1749506331883.png

Split by semicolon into 2 columns:

p45cal_3-1749506396179.png

Trim both ingredients columns:

p45cal_4-1749506461392.png

Change type of Ingredients.1 to decimal, then group by the Index. All the work is done inside the grouping where the max rows are selected and the Ingredients.2 texts combined:

p45cal_5-1749506625326.png

Then they're sorted on Index (because grouping isn't guaranteed to retain the order) and the Index column removed.

 

The code:

let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    AI = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    Split1 = Table.ExpandListColumn(Table.TransformColumns(AI, {{"Ingredients", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Ingredients"),
    Split2 = Table.SplitColumn(Split1, "Ingredients", Splitter.SplitTextByEachDelimiter({";"}, QuoteStyle.Csv, false), {"Ingredients.1", "Ingredients.2"}),
    Trim = Table.TransformColumns(Split2,{{"Ingredients.1", Text.Trim, type text}, {"Ingredients.2", Text.Trim, type text}}),
    CT = Table.TransformColumnTypes(Trim,{{"Ingredients.1", type number}}),
    Group = Table.Group(CT, {"Index"}, {{"Primary Ingredients", each Text.Combine(Table.SelectRows(_,let max = List.Max(_[Ingredients.1]) in each [Ingredients.1]=max)[Ingredients.2],", ")}}),
    Sort = Table.Sort(Group,{{"Index", Order.Ascending}}),
    CleanUp = Table.RemoveColumns(Sort,{"Index"})
in
    CleanUp

 

 

 

speedramps
Community Champion
Community Champion

Click here to download the solution from Onedrive

Click here 

 

 

How it works:-

 

Input this file a csv/text file

13;Apple | 15;Orange | 3;Strawberry
2;Apple | 18;Peach | 3;Melon
27;Blueberry | 7;Blackberry | 1;Lemon | 9;Grape
1;Peach | 11;Banana | 4;Pineapple | 8;Orange | 24;Blackberry

 

Use a custom delimiter = |  and   specify 5 columns

speedramps_0-1749481762888.png

 

Add a product id

speedramps_1-1749481860396.png

 

speedramps_2-1749481903991.png

Click on the product id column.

On the top menu bar select Trasform > Unpivot > Other columns

speedramps_3-1749481981443.png

Remove the empty rows

speedramps_4-1749482023307.png

Extract the Ingredient and Quanity either side of  delimiter

speedramps_5-1749482113971.png

Remove unneed columns and change the Quantity to a number

speedramps_6-1749482179948.png

Create a new table reference the above one
 

speedramps_7-1749482267029.png

Group max quanity by product id

speedramps_8-1749482321406.png

Merge by Product ID and Quantity

 

speedramps_9-1749482362293.png

Expand the merger to get the Ingredient

speedramps_10-1749482416013.png

Well done. You now have a table with just the max ingredient for each product

 

Please click thumbs up me taking the time and effort to try help.
Then click [accept solution] if it works.

Thank you.

 

 

 

 

 

MasonMA
Resolver II
Resolver II

Hi @mryoan04 

 

I'd encourage you to use AI to generate M code for this. Below is the code AI shared with me and it works! 

let
    Source = YourTable,
    // Step 1: Split each row into a list of records [Number, Ingredient]
    AddParsedList = Table.AddColumn(Source, "Parsed List", each 
        List.Transform(
            Text.Split([Ingredients], " | "),
            (item) => 
                let 
                    parts = Text.Split(item, ";")
                in 
                    [Number = Number.FromText(parts{0}), Ingredient = parts{1}]
        )
    ),

    // Step 2: Add a column that gets the max number for each row
    AddMaxNumber = Table.AddColumn(AddParsedList, "MaxNumber", each 
        List.Max(List.Transform([Parsed List], each _[Number]))
    ),

    // Step 3: Add a column to extract the ingredient where Number = MaxNumber
    AddPrimaryIngredient = Table.AddColumn(AddMaxNumber, "Primary Ingredient", (row) => 
        let
            records = row[Parsed List],
            maxNum = row[MaxNumber],
            match = List.Select(records, each _[Number] = maxNum)
        in
            if List.Count(match) > 0 then match{0}[Ingredient] else null
    ),

    // Step 4: Remove helper columns
    Cleanup = Table.RemoveColumns(AddPrimaryIngredient, {"Parsed List", "MaxNumber"})
in
    Cleanup

Thanks 

Mason

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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