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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Tooba_kazmi
Helper I
Helper I

How to identify rows with same ID and return a count of "1" in a separate column only once?

Hi All, 

 

In Table 1, a customer gets referred for three different products namely, motor, home & landlord insurance known as product referrals.The field, LEAD TYPE ID identifies a unique customer.  

 

What's the best way to search for unique Customer Referrals identified by Lead Type ID as shown in Table 2?

 

For example, Search rows having same Lead Type ID and return 1 in the Customer Referral Count field only once?  

 

Table 1:

LEAD_TYPELEAD_TYPE_IDCUSTOMER_EMAILPRODUCT
Rf88f271b-4a99-4c2d-9b6c-5a58c239082fsbb@bigpond.comPERSONAL_MOTOR
Rf88f271b-4a99-4c2d-9b6c-5a58c239082fsbb@bigpond.comPERSONAL_HOME
Rf88f271b-4a99-4c2d-9b6c-5a58c239082fsbb@bigpond.comPERSONAL_LANDLORD
Rc97e9b0c-1bed-48a9-af36-d2057bccf4c5thnbills@gmail.comPERSONAL_MOTOR
Rc97e9b0c-1bed-48a9-af36-d2057bccf4c5thnbills@gmail.comPERSONAL_HOME

 

Table 2:

LEAD_TYPELEAD_TYPE_IDCUSTOMER_EMAILPRODUCTCustomer Referral Count
Rf88f271b-4a99-4c2d-9b6c-5a58c239082fsbb@bigpond.comPERSONAL_MOTOR1
Rf88f271b-4a99-4c2d-9b6c-5a58c239082fsbb@bigpond.comPERSONAL_HOME 
Rf88f271b-4a99-4c2d-9b6c-5a58c239082fsbb@bigpond.comPERSONAL_LANDLORD 
Rc97e9b0c-1bed-48a9-af36-d2057bccf4c5thnbills@gmail.comPERSONAL_MOTOR1
Rc97e9b0c-1bed-48a9-af36-d2057bccf4c5thnbills@gmail.comPERSONAL_HOME 
1 ACCEPTED SOLUTION
v-xinruzhu-msft
Community Support
Community Support

Hi @Tooba_kazmi 

You can put the following code to advanced editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rc89C4MwFIXh/5LZWzQmmmwVFDqoKbabSMm9MVbwo2D/PxUKHTu5Hl4eOG3LGhYwr5TnaYQgrNYgiDvQmBBIKxXxWIeK+z3bEM84Dq91cSda5325Fs3N1Fn5qMzdNKwLjvIupioO5MqszkvT5D+SdNprDAki7B0IZTVYHyfgeChTJPKC5J69nwuO07Sdh9mO0//Th5Df390H", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [LEAD_TYPE = _t, LEAD_TYPE_ID = _t, CUSTOMER_EMAIL = _t, PRODUCT = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"LEAD_TYPE", type text}, {"LEAD_TYPE_ID", type text}, {"CUSTOMER_EMAIL", type text}, {"PRODUCT", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"LEAD_TYPE", "LEAD_TYPE_ID"}, {{"Count",each Table.AddIndexColumn(_, "Index", 1, 1), type table }}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"CUSTOMER_EMAIL", "PRODUCT", "Index"}, {"CUSTOMER_EMAIL", "PRODUCT", "Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Count", "Count", each if [Index] = 1 then 1 else null),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Count", Int64.Type}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Index"})
in
    #"Removed Columns"

Output

vxinruzhumsft_0-1694745757227.png

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-xinruzhu-msft
Community Support
Community Support

Hi @Tooba_kazmi 

You can put the following code to advanced editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rc89C4MwFIXh/5LZWzQmmmwVFDqoKbabSMm9MVbwo2D/PxUKHTu5Hl4eOG3LGhYwr5TnaYQgrNYgiDvQmBBIKxXxWIeK+z3bEM84Dq91cSda5325Fs3N1Fn5qMzdNKwLjvIupioO5MqszkvT5D+SdNprDAki7B0IZTVYHyfgeChTJPKC5J69nwuO07Sdh9mO0//Th5Df390H", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [LEAD_TYPE = _t, LEAD_TYPE_ID = _t, CUSTOMER_EMAIL = _t, PRODUCT = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"LEAD_TYPE", type text}, {"LEAD_TYPE_ID", type text}, {"CUSTOMER_EMAIL", type text}, {"PRODUCT", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"LEAD_TYPE", "LEAD_TYPE_ID"}, {{"Count",each Table.AddIndexColumn(_, "Index", 1, 1), type table }}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"CUSTOMER_EMAIL", "PRODUCT", "Index"}, {"CUSTOMER_EMAIL", "PRODUCT", "Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Count", "Count", each if [Index] = 1 then 1 else null),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Count", Int64.Type}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Index"})
in
    #"Removed Columns"

Output

vxinruzhumsft_0-1694745757227.png

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.