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
Daniel_Jesus
Frequent Visitor

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 😃

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

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.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

10 REPLIES 10
v-eqin-msft
Community Support
Community Support

Hi @Daniel_Jesus ,

Did I answer your question? Please mark my reply as solution, thank you~

 

Best regards,

Eyelyn Qin

v-eqin-msft
Community Support
Community Support

Hi @Daniel_Jesus ,

According to my understanding, you want to remove the companies whose OPERATION only do Purchase , right?

 

For my test ,I created a "flag" for the two Operations, then sum it based on each company.

If the sum>0 , it means that the company has Sale operation.

So you could use the following formula:

flag =
IF (
    SELECTEDVALUE ( Purchase[OPERATION] ) = "Purchase",
    0,
    IF ( SELECTEDVALUE ( Purchase[OPERATION] ) = "Sale", 1 )
)
sumFlag =
IF (
    CALCULATE (
        SUMX ( 'Purchase', [flag] ),
        ALLEXCEPT ( 'Purchase', 'Purchase'[COMPANY] )
    ) > 0,
    1,
    0
)

 

After apply sumFlag to filter (set as is 1),My visualization looks like this:

8.25.6.1.PNG

Is the result what you want? If you have any questions, please upload some data samples and expected output.

Please do mask sensitive data before uploading.

 

Best Regards,

Eyelyn Qin

Ashish_Mathur
Super User
Super User

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.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

@Ashish_Mathur well done! You answered just what I needed! I dared to do some changes, but it worked as expected. Thanks a lot and have a nice day, you helped me a lot!

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Greg_Deckler
Super User
Super User

@Daniel_Jesus - Perhaps:

 

Table 4 = FILTER(ADDCOLUMNS(SUMMARIZE('Table (20)',[Company],[Operation]),"Include",COUNTROWS(FILTER('Table (20)',[Company]=EARLIER('Table (20)'[Company])&&[Operation]="Sale"))),[Include]>=1)

PBIX is attached below sig. Table (20) and Table 4

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler In fact what you suggested worked fine, but I was concerned with the performance, once I have thousands of data and by duplicating my table I could cause some delays.  Thanks for supporting me.

@Daniel_Jesus - I didn't realize you wanted a Power Query solution or I would have moved this thread to the Power Query forum.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@amitchandak I read all material you suggested, but I couldn't find a solution or come up with any ideia to solve my problem, but I learned new things, thank you a lot.

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.