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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
tc5pt
Regular Visitor

Remove columns containing a certain value

I have various columns that have a date listed in one row, therefore they contain a "/". None of the other columns I need contain a backslash in any of the rows. I would like to delete the columns containing this backslash.

 

I saw a thread that propose the following formula but am having difficulty applying it to my query. It tells me "PreviousStepName not recognized". I replaced it with the name of the previous step in my query, but that didn't work. Am I supposed to be filling in values for "ColumnNames" and other parts of this formula as well? Or is this formula not accurate for what I am trying to achieve?

 

= let columnsToRemove = List.Select(Table.ColumnNames(PreviousStepName), each Text.StartsWith(_, "Var") or Text.Contains(_, "_prep")) in Table.RemoveColumns(PreviousStepName, columnsToRemove)

 

I also found a thread that suggested transposing my data and then deleting rows, but this was not a good solution for my data.

 

Thank you

1 ACCEPTED SOLUTION
Nolock
Resident Rockstar
Resident Rockstar

Hi @tc5pt,

I've tried to integrate it into the code you mentioned earlier. Just replace all occurrences of "Source" with #"Promoted Headers" and it should work in your Excel file.

 

let
    Source = Excel.Workbook(File.Contents("C: Source"), null, true),
    #"1_Sheet" = Source{[Item="1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(#"1_Sheet", [PromoteAllScalars=true]),

// get columns which contains any slash among values
    ColumnsToRemove = 
        List.Select(
            // get a list of all columns
            Table.ColumnNames(#"Promoted Headers"),
            (columnName) =>
                let
                    // get all values of a columns
                    ColumnValues = Table.Column(#"Promoted Headers", columnName),
                    // go through values and stop when you find the first occurence of a text containing a slash
                    // if there is a value with a slash, return true else false
                    ContainsSlash = List.AnyTrue(List.Transform(ColumnValues, each Text.Contains(_, "/")))
                in
                    ContainsSlash
        ),
    // remove columns
    Result = Table.RemoveColumns(#"Promoted Headers", ColumnsToRemove)
in
    Result

View solution in original post

11 REPLIES 11

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.

Top Solution Authors
Top Kudoed Authors