Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Dicken
Responsive Resident
Responsive Resident

Power Query conditional column selection

Hi , I wanted to select columns that contained a certain value and came up with this , but has anyone any ideas
for a different approach ? here just selectign columns  that contian "X".  BTW, should I put code in tags or something? 
I've tried using the html button but then cant type any text ? 

 

let
Source = #table(
type table [A = text, B = text, C = text, D = text],
{{"a,b", "X,c", "c,c", "a,d"}, {"c,c", "a,a", "a,X", "d,c"}}
),
truefalse = List.Transform(
Table.ToColumns(Source),
(x) => List.Transform(x, (y) => List.Contains(Splitter.SplitTextByDelimiter(",")(y), "X"))
),
pos = List.PositionOf(List.Transform(truefalse, (x) => List.AnyTrue(x)), true, Occurrence.All),
headers = List.Transform(pos, (x) => Table.ColumnNames(Source){x}),
result = Table.SelectColumns(Source, headers)
in
result

1 ACCEPTED SOLUTION
PwerQueryKees
Super User
Super User

Your table = Source.

You can use this anywhere you would normall use SelectColumns (or RemoveColumns)

 

= Table.SelectColumns(Source,List.Select(Table.ColumnNames(Source), each Text.Contains(_, "X", Comparer.OrdinalIgnoreCase)))

 

I made the match case INsenistive, remove the Comparare.OrdinalIgnoreCase if you don't want that.

View solution in original post

8 REPLIES 8
Dicken
Responsive Resident
Responsive Resident

Just to add you can skip the List.AnyTrue which does give a flexible for All / Any ; but this also works; 
= List.Transform(  Table.ToColumns( Source ), (x)=>List.ContainsAny( x, {"X"}, (x,y)=>Text.Contains(x,y) ) )

Dicken
Responsive Resident
Responsive Resident

Thanks for all the replies, havve nto gone through each step off all, but will

Thanks for taking the time to repond.

Omid_Motamedise
Super User
Super User

Hi @Dicken 

Table.UnpivotOtherColumn is really useful for this question, so use the below code

let
Source = #table(
type table [A = text, B = text, C = text, D = text],
{{"a,b", "X,c", "c,c", "a,d"}, {"c,c", "a,a", "a,X", "d,c"}}
),
    Custom1 = Table.UnpivotOtherColumns(Source,{},"Columns","Value"),
    #"Filtered Rows" = Table.SelectRows(Custom1, each Text.Contains([Value], "X"))
in
    #"Filtered Rows"
If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
PwerQueryKees
Super User
Super User

Your table = Source.

You can use this anywhere you would normall use SelectColumns (or RemoveColumns)

 

= Table.SelectColumns(Source,List.Select(Table.ColumnNames(Source), each Text.Contains(_, "X", Comparer.OrdinalIgnoreCase)))

 

I made the match case INsenistive, remove the Comparare.OrdinalIgnoreCase if you don't want that.

Not sure it works, or Im missing something, I just got an empty 2 row table not the columns where and "X"is contained. 
let
Source = let
Source = #table(
type table [A = text, B = text, C = text, D = text],
{{"a,b", "X,c", "c,c", "a,d"}, {"c,c", "a,a", "a,X", "d,c"}} ) in Source,
Custom1 = Table.SelectColumns(Source,List.Select(Table.ColumnNames(Source), each Text.Contains(_, "X", Comparer.OrdinalIgnoreCase)))
in
Custom1

slorin
Super User
Super User

Hi @Dicken 

 

let
Source = YourSource,
DemoteHeaders = Table.DemoteHeaders(Source),
Transpose1 = Table.Transpose(DemoteHeaders),
SelectRows_with_X = Table.SelectRows(Transpose1, each not List.IsEmpty(List.FindText(List.Skip(Record.ToList(_)),"X"))),
Transpose2 = Table.Transpose(SelectRows_with_X),
PromoteHeaders = Table.PromoteHeaders(Transpose2, [PromoteAllScalars=true])
in
PromoteHeaders

 Stéphane

Dicken
Responsive Resident
Responsive Resident

Interesting, I'll leave it open see if anyone else repsonds,  

Richard. 

Hi @Dicken 

Table.SelectColumns(
    Source, 
    List.Select(
        Table.ColumnNames(Source), 
        each Text.Contains(Text.Combine(Table.Column(Source, _), "|"), "X")
    )
)

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.