Power Query - Remove row(s) question

scott_86_

New Member
Joined
Sep 27, 2018
Messages
40
Office Version
  1. 365
Platform
  1. Windows
Hi.

Using Microsoft 365.

Below is a snip example of a much larger dataset I am using.

In the snip below, 5/7 names (green) have 3 training types (Height, Rescue & PFA). 2/7 names (red) have PFA only.

Is there a way in Power Query to remove all rows (names) that have PFA only with no other training types? Thanks.

1715753027815.png
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You can do this in a single query but the below requires minimal M code changes
• Import the initial table
• Use Duplicate to create a second copy of the query
• in the 2nd copy with name seletected - Transform > Group > Count rows
• go back to the first query Home > Merge Queries > Left join on name to the 2nd (grouped query)
• Expand the merged table to show count
• Add a conditional column with Count = 1 then Y
• Go into the M code of this and add the part in blue
if [Count] = 1 and [Training] = "PFA" then "Y" else null
• Filter on custom = null
• Remove the columns Count and Custom
• Close and Load

1715759589996.png


XL2BB in case anyone else want to give you a more streamlined version:

20240515 PQ Remove rows single value scott_86_.xlsx
ABCDE
1OriginalResult
2
3NameTrainingNameTraining
4ScottHeightScottHeight
5ScottRescueScottRescue
6ScottPFAScottPFA
7DavidPFAChrisHeight
8ChrisHeightChrisRescue
9ChrisRescueChrisPFA
10ChrisPFATimHeight
11TimHeightTimRescue
12TimRescueTimPFA
13TimPFARickHeight
14RickHeightRickRescue
15RickRescueRickPFA
16RickPFAFrankHeight
17JimPFAFrankRescue
18FrankHeightFrankPFA
19FrankRescueMaryHeight
20FrankPFA
21MaryHeight
22
Sheet1
 
Upvote 0
Here the short version

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    grp = Table.Group(Source, {"Name"}, {{"Group", each if Table.RowCount(_)=1 and [Training]{0} = "PFA" then Table.RemoveRows(_,0,1) else _  } }),
    result = Table.Combine(grp[Group])
in
    result
 
Upvote 0
Here the short version

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    grp = Table.Group(Source, {"Name"}, {{"Group", each if Table.RowCount(_)=1 and [Training]{0} = "PFA" then Table.RemoveRows(_,0,1) else _  } }),
    result = Table.Combine(grp[Group])
in
    result
Thanks, this works when I use the basic table from my original post.

I applied it to the large dataset I have; however, it returned an error. I believe the error is because my larger dataset has 10 columns instead of 2 in my example.

Would it be possible to amend the code based off the following column headers I have in my larger dataset?

  • User - User full name (This is where the user's name is eg. Scott)
  • User - Position
  • Certifications - Certification title (This is where the training items are, eg. Provide First Aid - which was PFA in my example)
  • Initial Completion Date
  • Certification Expiry Date
  • User - Location
  • User - Manager
  • User - Business Unit
  • Certifications - Certification user status
  • Certifications - Certification period is current
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top