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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Ushkamour
Frequent Visitor

keep rows conundrum

 

Hi, I've been struggling with this and hope esomeone can help. I have a table of several thousand records. In this snip I've removed columns to simplify and anonymise. I want to remove rows to retain only those where there is a variance between consentglobalname for that contactnumber. In this snip, a suitable solution would leave me with only 4 rows, for contactnumber 'CON-383780'. I think the solution will use Group By but I can't figure it out. Any ideas?  Thanks in advance

 

Ushkamour_0-1729791265813.png

 

1 ACCEPTED SOLUTION
jgeddes
Super User
Super User

You can group by py3_contactnumber choosing to aggregate by count distinct rows and keeping all rows. Then amend the resulting code to take the distinct count of only the py3_consentglobalname column. (The code would like this...)

= Table.Group(#"Changed Type", {"py3_contactnumber"}, {{"_nested", each _, type table [py3_consentglobalname=nullable text, py3_contactnumber=nullable text]}, {"Count", each Table.RowCount(Table.Distinct(Table.SelectColumns(_, "py3_consentglobalname"))), Int64.Type}})

You can now filter the resulting contactnumber count column so that only contacts with a count greater than one are kept.

From there you have to decide which rows you are keeping. If you are keeping only rows with consent as "yes" you could use Table.TransformColumns() to filter the nested tables to keep "Yes" rows. Then expand the tables.
Complete code would look like this...

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WikwtVtJRcvb30zWxMDY2sFSK1cEh6JdPpEKEoKmRiaGFAapuGoohu8bSzMwEtyDCLwYm5ugm4hOLBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [py3_consentglobalname = _t, py3_contactnumber = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"py3_consentglobalname", type text}, {"py3_contactnumber", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"py3_contactnumber"}, {{"_nested", each _, type table [py3_consentglobalname=nullable text, py3_contactnumber=nullable text]}, {"Count", each Table.RowCount(Table.Distinct(Table.SelectColumns(_, "py3_consentglobalname"))), Int64.Type}}),
    #"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([Count] = 2)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Count"}),
    Custom1 = Table.TransformColumns(#"Removed Columns", {{"_nested", each Table.SelectRows(_, each [py3_consentglobalname] = "Yes")}}),
    #"Expanded _nested" = Table.ExpandTableColumn(Custom1, "_nested", {"py3_consentglobalname"}, {"py3_consentglobalname"})
in
    #"Expanded _nested"

In this example I started with...

jgeddes_0-1729800475885.png

and ended with...

jgeddes_1-1729800495683.png

Hope this gets you pointed in the right direction.





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

Proud to be a Super User!





View solution in original post

3 REPLIES 3
Ushkamour
Frequent Visitor

thanks @ronrsnfld  and @jgeddes thats exactly what I needed. Much appreciated

ronrsnfld
Super User
Super User

I suggest:

  •  Group by py3_contactnumber
    • All Rows
    • Add custom aggregation to detect if both "No" and "Yes" are in the py3_consentglobalname column
    • Select only the rows that return true
    • Re-expand
let

//Replace Source line with your actual data table
    Source = Table,
    #"Grouped Rows" = Table.Group(Source, {"py3_contactnumber"}, {
        {"All", each _, type table [py3_consentglobalname=nullable text, py3_contactnumber=nullable text]},
        {"Select", each List.ContainsAll([py3_consentglobalname],{"Yes","No"}), type logical}}),
    #"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([Select] = true)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"py3_contactnumber", "Select"}),
    #"Expanded All" = Table.ExpandTableColumn(#"Removed Columns", "All", 
        {"py3_consentglobalname", "py3_contactnumber"})
in
    #"Expanded All"

 

Original Table

ronrsnfld_0-1729818768853.png

 

Result

ronrsnfld_1-1729818794560.png

 

 

jgeddes
Super User
Super User

You can group by py3_contactnumber choosing to aggregate by count distinct rows and keeping all rows. Then amend the resulting code to take the distinct count of only the py3_consentglobalname column. (The code would like this...)

= Table.Group(#"Changed Type", {"py3_contactnumber"}, {{"_nested", each _, type table [py3_consentglobalname=nullable text, py3_contactnumber=nullable text]}, {"Count", each Table.RowCount(Table.Distinct(Table.SelectColumns(_, "py3_consentglobalname"))), Int64.Type}})

You can now filter the resulting contactnumber count column so that only contacts with a count greater than one are kept.

From there you have to decide which rows you are keeping. If you are keeping only rows with consent as "yes" you could use Table.TransformColumns() to filter the nested tables to keep "Yes" rows. Then expand the tables.
Complete code would look like this...

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WikwtVtJRcvb30zWxMDY2sFSK1cEh6JdPpEKEoKmRiaGFAapuGoohu8bSzMwEtyDCLwYm5ugm4hOLBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [py3_consentglobalname = _t, py3_contactnumber = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"py3_consentglobalname", type text}, {"py3_contactnumber", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"py3_contactnumber"}, {{"_nested", each _, type table [py3_consentglobalname=nullable text, py3_contactnumber=nullable text]}, {"Count", each Table.RowCount(Table.Distinct(Table.SelectColumns(_, "py3_consentglobalname"))), Int64.Type}}),
    #"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([Count] = 2)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Count"}),
    Custom1 = Table.TransformColumns(#"Removed Columns", {{"_nested", each Table.SelectRows(_, each [py3_consentglobalname] = "Yes")}}),
    #"Expanded _nested" = Table.ExpandTableColumn(Custom1, "_nested", {"py3_consentglobalname"}, {"py3_consentglobalname"})
in
    #"Expanded _nested"

In this example I started with...

jgeddes_0-1729800475885.png

and ended with...

jgeddes_1-1729800495683.png

Hope this gets you pointed in the right direction.





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

Proud to be a Super User!





Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors