Forum Discussion

Daniel_Jesus's avatar
Daniel_Jesus
Frequent Visitor
6 years ago
Solved

How to remove specific rows based on conditions?

Hey guys,

I have a data like in the example below, with thousands of rows following the same ideia. I'm trying to remove all rows that contains a company who only did purchase operation and not a sale, but I couldn't handle with it.

Data example:

COMPANYOPERATION
Company ASale
Company APurchase
Company BPurchase
Company CSale
Company DPurchase
Company DSale

 

Reinforcing what I intend to do: company A did a sale and a purchase, it stays in my table; company C only did a sale and stays in my table, but company B only did a purchase, so I need to remove that from my data.

 

I'm intending to do something like:

COMPANYOPERATION
Company ASale
Company APurchase
Company CSale
Company DPurchase
Company DSale

 

Many thanks in advance 😃

  • Hi,

    This M code works

    let
        Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
        #"Grouped Rows" = Table.Group(Source, {"COMPANY"}, {{"All Operations", each Text.Combine(List.Distinct([OPERATION]), ", "), type text}}),
        Joined = Table.Join(Source, "COMPANY", #"Grouped Rows", "COMPANY"),
        #"Uppercased Text" = Table.TransformColumns(Joined,{{"All Operations", Text.Upper, type text}}),
        #"Added Custom" = Table.AddColumn(#"Uppercased Text", "Custom", each if [All Operations]="PURCHASE" then "Ignore" else "Consider"),
        #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = "Consider")),
        #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"All Operations", "Custom"})
    in
        #"Removed Columns"

    Hope this helps.

10 Replies