Forum Discussion

ljx0648's avatar
ljx0648
Icon for Helper III rankHelper III
2 years ago
Solved

Return All Records with certain status in another column

Hi guys,

 

I have some data and it came from a manual entry CRM so it has alot of human errors. (which you will see in the Customer Stage, the status will be marked as XXX Error)

 

The first step that I have to do is to filter out the Customer profile which is at the latest stage. 

 

Customer Stage ranking: Lead OR Lead Error -> Prospect OR Prospect Error-> Profile OR Profile Error (final stage)

 

Below is a piece of sample data I have:

 

Customer Profile StatusAsset
ALead 10
AProspect20
BLead 30
BProspect30
BProfile50
CLead 40
CProfile60
CProfile60
DLead 15
DProspect20
DProspect Error20
EProfile 5
EProfile Error5

 

The result I am looking for as per below:

 

Customer Profile StatusAsset
AProspect20
BProfile50
CProfile60
CProfile60
DProspect20
DProspect Error20
EProfile5
EProfile Error5

 

You will probably realize that there are still duplicates after the exercise, however, my goal here is not trying to de-dup but picking up the profile at their latest stages.

 

Lastly, if this can be done in power editor that would be great as I will need to join this table with other tables afterwards. However, Measure is good too if there is no possible way in the editor.

 

I tried approached this by grouping the customer 1st , creating orders for the status and then select the Max order, but this approach only return ONE record (which I willl need duplicate if it is in the same stage).

 

May I know if someone can provide a way of doing this?

 

Any tips are apprecaited.

 

Thank you

  • I believe, you've already posted this but updated the data.  The code I gave you wouldn't remove any duplicates but as mentioned, you need to update the condition for Profile Status to include those with error.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUfJJTUxRANKGBkqxOhChgKL84oLU5BIg0wgi6oSk0BghhKQQVTQtMycVyDKFCDoj6TZBCCHUmeERdEF2oylcCNONyKIKrkVF+UUIOVeEySBzTDEEYeqBMrEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, #"Profile Status" = _t, Asset = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Profile Status", type text}, {"Asset", Int64.Type}}),
        #"Trimmed Text" = Table.TransformColumns(#"Changed Type",{{"Profile Status", Text.Trim, type text}}),
        #"Added Custom" = Table.AddColumn(#"Trimmed Text", "Profile Status Order", each if [Profile Status] = "Lead" then 1 else if [Profile Status] = "Prospect" then 2 else if [Profile Status] = "Prospect Error" then 2 else if [Profile Status] = "Profile Error" then 3 else if [Profile Status] = "Profile" then 3 else 3),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Max", each let customer = [Customer]
    in
    List.Max(
    Table.SelectRows(#"Added Custom", each [Customer ] = customer)[Profile Status Order])),
        #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Filter", each [Profile Status Order]=[Max]),
        #"Filtered Rows" = Table.SelectRows(#"Added Custom2", each [Filter] = true),
        #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"Customer", "Profile Status", "Asset"}),
        #"Sorted Rows1" = Table.Sort(#"Removed Other Columns",{{"Customer", Order.Ascending}})
    in
        #"Sorted Rows1"

     

     

1 Reply

  • I believe, you've already posted this but updated the data.  The code I gave you wouldn't remove any duplicates but as mentioned, you need to update the condition for Profile Status to include those with error.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUfJJTUxRANKGBkqxOhChgKL84oLU5BIg0wgi6oSk0BghhKQQVTQtMycVyDKFCDoj6TZBCCHUmeERdEF2oylcCNONyKIKrkVF+UUIOVeEySBzTDEEYeqBMrEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, #"Profile Status" = _t, Asset = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Profile Status", type text}, {"Asset", Int64.Type}}),
        #"Trimmed Text" = Table.TransformColumns(#"Changed Type",{{"Profile Status", Text.Trim, type text}}),
        #"Added Custom" = Table.AddColumn(#"Trimmed Text", "Profile Status Order", each if [Profile Status] = "Lead" then 1 else if [Profile Status] = "Prospect" then 2 else if [Profile Status] = "Prospect Error" then 2 else if [Profile Status] = "Profile Error" then 3 else if [Profile Status] = "Profile" then 3 else 3),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Max", each let customer = [Customer]
    in
    List.Max(
    Table.SelectRows(#"Added Custom", each [Customer ] = customer)[Profile Status Order])),
        #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Filter", each [Profile Status Order]=[Max]),
        #"Filtered Rows" = Table.SelectRows(#"Added Custom2", each [Filter] = true),
        #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"Customer", "Profile Status", "Asset"}),
        #"Sorted Rows1" = Table.Sort(#"Removed Other Columns",{{"Customer", Order.Ascending}})
    in
        #"Sorted Rows1"