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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

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
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.