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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
tgjones43
Helper IV
Helper IV

Filter out certain rows based on values in 2 columns

Hi all

 

I'm hoping someone can suggest some M code that can do the following.

 

For each unique value in the [Number] column, apply the following rules:

 

If all values in the [Value] column are "A" only, keep all rows

If all values in the [Value] column are "B" only, keep all rows

If the values in the [Value] column are a mix of "A" and "B", keep all rows of "A" only

 

I have tried to show how this would work in the table below, with the final column being the desired outcome.

 

NumberValueKeep
1BYes
1BYes
2AYes
2BNo
3A

Yes

3AYes
3BNo
4AYes
5BYes

 

Thank you so much!

1 ACCEPTED SOLUTION
DataInsights
Super User
Super User

@tgjones43,

 

Try this solution in Power Query. It uses Group By and a custom column.

 

DataInsights_0-1629728191414.png

M code:

 

let
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText("i45WMlTSUXJSitVBZhkBWY5wFkTMGC6GyoLImsDFTCFisQA=", BinaryEncoding.Base64),
        Compression.Deflate
      )
    ),
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [Number = _t, Value = _t]
  ),
  ChangeType = Table.TransformColumnTypes(Source, {{"Number", Int64.Type}}),
  GroupRows = Table.Group(
    ChangeType,
    {"Number"},
    {
      {"Distinct Count", each Table.RowCount(Table.Distinct(_)), Int64.Type},
      {"All", each _, type table [Number = nullable number, Value = nullable text]}
    }
  ),
  ExpandAll = Table.ExpandTableColumn(GroupRows, "All", {"Value"}, {"Value"}),
  AddColumn = Table.AddColumn(
    ExpandAll,
    "Keep Column",
    each
      if [Distinct Count] = 1 then
        "Y"
      else if [Distinct Count] = 2 and [Value] = "A" then
        "Y"
      else
        "N"
  )
in
  AddColumn

 

Result:

 

DataInsights_1-1629728252995.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

2 REPLIES 2
DataInsights
Super User
Super User

@tgjones43,

 

Try this solution in Power Query. It uses Group By and a custom column.

 

DataInsights_0-1629728191414.png

M code:

 

let
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText("i45WMlTSUXJSitVBZhkBWY5wFkTMGC6GyoLImsDFTCFisQA=", BinaryEncoding.Base64),
        Compression.Deflate
      )
    ),
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [Number = _t, Value = _t]
  ),
  ChangeType = Table.TransformColumnTypes(Source, {{"Number", Int64.Type}}),
  GroupRows = Table.Group(
    ChangeType,
    {"Number"},
    {
      {"Distinct Count", each Table.RowCount(Table.Distinct(_)), Int64.Type},
      {"All", each _, type table [Number = nullable number, Value = nullable text]}
    }
  ),
  ExpandAll = Table.ExpandTableColumn(GroupRows, "All", {"Value"}, {"Value"}),
  AddColumn = Table.AddColumn(
    ExpandAll,
    "Keep Column",
    each
      if [Distinct Count] = 1 then
        "Y"
      else if [Distinct Count] = 2 and [Value] = "A" then
        "Y"
      else
        "N"
  )
in
  AddColumn

 

Result:

 

DataInsights_1-1629728252995.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Thank you so much @DataInsights, that is a great solution!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.