PQ Keep Rows where any column contains a certain value

cr731

Well-known Member
Joined
Sep 17, 2010
Messages
611
I am trying to isolate rows (Customers) in my data set with "invalid" values in any one of four columns. My table looks like this,

CustomerAttribute 1Attribute 2Attribute 3Attribute 4
Customer1xxxxxxInvalidAxxx
Customer1xxxxxxInvalidBxxx
Customer3InvalidCxxxxxxInvalidD
Customer4xxxxxxxxxxxx

<tbody>
</tbody>

Where xxx is valid data. So based on above, I want to keep rows for Customers 1 and 3.

In each of the Attribute columns, the "invalid" value could be something different (like above, Attribute 1's invalid value is InvalidC, while Attribute 3's invalid values are InvalidA and InvalidB). So my approach was to create a table listing the invalid values by attribute.

AttributeInvalid Value
Attribute 1InvalidC
Attribute 3InvalidA
Attribute 3InvalidB
Attribute 4InvalidD

<tbody>
</tbody>

I then unpivoted my data set, and merged with an inner join to this table of invalid values to keep all records of invalid values. Problem is, now I'm left with a table like this,

CustomerAttributeValue
Customer 1Attribute 3InvalidA
Customer 1Attribute 3InvalidB
Customer 3Attribute 1InvalidC
Customer 3Attribute 4InvalidD

<tbody>
</tbody>


And I want my resulting output table to be the same format as the original (where there is a column for Attributes 1, 2, 3, 4).

If I try to re-pivot the last table above, I get an error that there were too many elements in the enumeration because Customer 1 has two records for Attribute 3. Is there a way to get the last table re-pivoted to look like the first, or a better way to do this altogether?

Thanks
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi
Try with
Code:
let    source = Excel.CurrentWorkbook(){[Name="Customers"]}[Content],
    grouped = Table.Group(source, {"Customer"},
    {
        {"needed", (sub) =>
            let
                attrOnly = Table.RemoveColumns(sub, {"Customer"}),
                AttrTableList = List.Transform(Table.ToRecords(attrOnly), each Record.ToTable(_)),
                transposed = Table.Combine(AttrTableList),
                return = Table.SelectRows(transposed, each Text.StartsWith([Value], "Invalid"))
            in
                return
        }
    }),
    expanded = Table.ExpandTableColumn(grouped, "needed", {"Name", "Value"})
in
    Table.SelectRows(expanded, each [Value] <> null)
Regards,
 
Upvote 0

Forum statistics

Threads
1,216,309
Messages
6,130,000
Members
449,551
Latest member
MJS_53

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