March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Hi Team,
I have the following logic to handle and I am not sure how to do it.
In the following table, we have got letters and colors.
Let's say that letter is a group and whenever there is a brown color assigned to the letter regardless of others, take brown.
letter | color | new column |
a | yellow | brown |
a | brown | brown |
a | brown | brown |
a | brown | brown |
b | yellow | brown |
b | yellow | brown |
b | brown | brown |
c | yellow | yellow |
c | yellow | yellow |
d | brown | brown |
d | yellow | brown |
d | yellow | brown |
Can anyone suggest something?
Thanks.
Bart
Solved! Go to Solution.
Hi @bartek_pepper Try below M code using Advance Editor option:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"letter", type text}, {"color", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"letter"}, {{"AllColors", each _, type table [letter=nullable text, color=nullable text, new column=nullable text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "NewColumn", each if List.Contains([AllColors][color], "brown") then "brown" else [AllColors]{0}[color]),
#"Expanded AllColors" = Table.ExpandTableColumn(#"Added Custom", "AllColors", {"color"})
in
#"Expanded AllColors"
Here is the desired output:
Hope this helps!!
If this solved your problem, please accept it as a solution and a kudos!!
Best Regards,
Shahariar Hafiz
Hi @bartek_pepper ,
Try the following:
= (CUSTOM) =>
let
source = (CUSTOM as text) => Text.From([letter]) ,
ColorSelection= Table.SelectRows( #"Changed Type",each [letter] = CUSTOM)
in
ColorSelection
= Table.AddColumn(#"Changed Type", "Custom", each Table.Group (Custom1([letter]), {"letter"}, {"New color", each if List.Contains (_[color] , "brown") = true then "brown" else null} ))
= Table.ReplaceValue(#"Expanded Custom",null,each [color],Replacer.ReplaceValue,{"New color"})
Full code below
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUapMzcnJL1eK1YFwk4ryy/MI8pJQNWLhItQmo0qicVNQ1KZgSsK4sQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [letter = _t, color = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"letter", type text}, {"color", type text}}),
Custom1 = (CUSTOM) =>
let
source = (CUSTOM as text) => Text.From([letter]) ,
ColorSelection= Table.SelectRows( #"Changed Type",each [letter] = CUSTOM)
in
ColorSelection,
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Table.Group (Custom1([letter]), {"letter"}, {"New color", each if List.Contains (_[color] , "brown") = true then "brown" else null} )),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"New color"}, {"New color"}),
#"Replaced Value" = Table.ReplaceValue(#"Expanded Custom",null,each [color],Replacer.ReplaceValue,{"New color"})
in
#"Replaced Value"
Regards
Miguel Félix
Proud to be a Super User!
Check out my blog: Power BI em Portuguêslet
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
brown = Function.Invoke(
Record.FromList,
List.Reverse(
Table.ToColumns(
Table.Distinct(
Table.SelectRows(Source, (x) => x[color] = "brown"),
"letter"
)
)
)
),
new_column = Table.AddColumn(Source, "new column", (x) => Record.FieldOrDefault(brown, x[letter], x[color]))
in
new_column
Thanks @AlienSx @MFelix @shafiz_p for all your solution.
I have done a small try
Create a Custom column by using below code
if Table.Contains([Count],[color ="brown"]) then {"brown"} else List.Distinct([Count][color])
Full code
let
Source = Table2,
#"Grouped Rows" = Table.Group(Source, {"letter"}, {{"Count", each _, type table}}),
Custom2 = #"Grouped Rows",
#"Added Custom" = Table.AddColumn(Custom2, "new Column",
each
if Table.Contains([Count],[color ="brown"]) then {"brown"} else List.Distinct([Count][color])
),
#"Expanded Count" = Table.ExpandTableColumn(#"Added Custom", "Count", {"color"}, {"color"}),
#"Expanded Custom" = Table.ExpandListColumn(#"Expanded Count", "new Column")
in
#"Expanded Custom"
output
Regards
sanalytics
Thank you so much everyone!
Hi @bartek_pepper ,
Thanks for all the replies!
@bartek_pepper , Have you solved your problem? If solved please mark the reply in this post which you think is helpful as a solution to help more others facing the same problem to find a solution quickly, thank you very much!
Best Regards,
Dino Tao
Thanks @AlienSx @MFelix @shafiz_p for all your solution.
I have done a small try
Create a Custom column by using below code
if Table.Contains([Count],[color ="brown"]) then {"brown"} else List.Distinct([Count][color])
Full code
let
Source = Table2,
#"Grouped Rows" = Table.Group(Source, {"letter"}, {{"Count", each _, type table}}),
Custom2 = #"Grouped Rows",
#"Added Custom" = Table.AddColumn(Custom2, "new Column",
each
if Table.Contains([Count],[color ="brown"]) then {"brown"} else List.Distinct([Count][color])
),
#"Expanded Count" = Table.ExpandTableColumn(#"Added Custom", "Count", {"color"}, {"color"}),
#"Expanded Custom" = Table.ExpandListColumn(#"Expanded Count", "new Column")
in
#"Expanded Custom"
output
Regards
sanalytics
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
brown = Function.Invoke(
Record.FromList,
List.Reverse(
Table.ToColumns(
Table.Distinct(
Table.SelectRows(Source, (x) => x[color] = "brown"),
"letter"
)
)
)
),
new_column = Table.AddColumn(Source, "new column", (x) => Record.FieldOrDefault(brown, x[letter], x[color]))
in
new_column
Hi @bartek_pepper ,
Try the following:
= (CUSTOM) =>
let
source = (CUSTOM as text) => Text.From([letter]) ,
ColorSelection= Table.SelectRows( #"Changed Type",each [letter] = CUSTOM)
in
ColorSelection
= Table.AddColumn(#"Changed Type", "Custom", each Table.Group (Custom1([letter]), {"letter"}, {"New color", each if List.Contains (_[color] , "brown") = true then "brown" else null} ))
= Table.ReplaceValue(#"Expanded Custom",null,each [color],Replacer.ReplaceValue,{"New color"})
Full code below
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUapMzcnJL1eK1YFwk4ryy/MI8pJQNWLhItQmo0qicVNQ1KZgSsK4sQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [letter = _t, color = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"letter", type text}, {"color", type text}}),
Custom1 = (CUSTOM) =>
let
source = (CUSTOM as text) => Text.From([letter]) ,
ColorSelection= Table.SelectRows( #"Changed Type",each [letter] = CUSTOM)
in
ColorSelection,
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Table.Group (Custom1([letter]), {"letter"}, {"New color", each if List.Contains (_[color] , "brown") = true then "brown" else null} )),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"New color"}, {"New color"}),
#"Replaced Value" = Table.ReplaceValue(#"Expanded Custom",null,each [color],Replacer.ReplaceValue,{"New color"})
in
#"Replaced Value"
Regards
Miguel Félix
Proud to be a Super User!
Check out my blog: Power BI em PortuguêsHi!
Thank you so much for your input. Indeed, it helped but I have not investigated yet why for some of them this is not working but maybe it is just a matter of data. But the idea in general is really good!
Hi @bartek_pepper Try below M code using Advance Editor option:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"letter", type text}, {"color", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"letter"}, {{"AllColors", each _, type table [letter=nullable text, color=nullable text, new column=nullable text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "NewColumn", each if List.Contains([AllColors][color], "brown") then "brown" else [AllColors]{0}[color]),
#"Expanded AllColors" = Table.ExpandTableColumn(#"Added Custom", "AllColors", {"color"})
in
#"Expanded AllColors"
Here is the desired output:
Hope this helps!!
If this solved your problem, please accept it as a solution and a kudos!!
Best Regards,
Shahariar Hafiz
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.