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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Mic1979
Post Partisan
Post Partisan

Dynamic Filter Based on Column Name

Hello,

 

I have the following table:

Mic1979_0-1748709543230.png

and I would like to apply a filter based on the column name, e.g. Distribution.

I was trying to write the following code:

 

SelectDistributionColumn = List.Select (Table.ColumnNames (RemovedColumn), each Text.Contains (_, "Distribution")),

FilteredTable = Table.SelectRows (Previous_Step, each Record.Field(_, SelectDistributionColumn) <>0)

 

but this is not working.

 

Could you help?

 

Thanks

 

1 ACCEPTED SOLUTION

One method is to add a "Filter Column" which will return True or false depending on your logic. Then filter on that column.

 

For example, the following code would add a column name'd "Filter"

#"Add Filter Column" = Table.AddColumn(PreviousStep,"Filter", (r)=> 

    //Use List.AllTrue if you want "and" logic
    //Use List.AnyTrue if you want "or" logic
        List.AllTrue(
            
            List.Transform(
                Record.FieldValues(
                    Record.SelectFields(
                        r,SelectDistributionColumn)), 
            each _ <> 0 and _ <> null)))

 

Then Filter on that column for true or false.

View solution in original post

4 REPLIES 4
ronrsnfld
Super User
Super User

SelectDistributionColumn is a List, but needs to be a text string, as indicated by the error message you most likely received.

 

If there is just a single column, then just use the first element of that list.

If there are multiple columns, then you have to reference each column separately.

The are multiple columns, and I want to reference to the selected column by the column name.

Could you suggest the code?

 

Thanks.

One method is to add a "Filter Column" which will return True or false depending on your logic. Then filter on that column.

 

For example, the following code would add a column name'd "Filter"

#"Add Filter Column" = Table.AddColumn(PreviousStep,"Filter", (r)=> 

    //Use List.AllTrue if you want "and" logic
    //Use List.AnyTrue if you want "or" logic
        List.AllTrue(
            
            List.Transform(
                Record.FieldValues(
                    Record.SelectFields(
                        r,SelectDistributionColumn)), 
            each _ <> 0 and _ <> null)))

 

Then Filter on that column for true or false.

Thanks.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.