SOLVED

Power Query: select first column for function Table.SelectRows

Brass Contributor

Dear coleagues,

I get a table like this

IDNameCity
1APrague
2BStuttgart
3CCardiff
COUNT:3 

I want to filter out the row COUNT in the first column of the table:

= Table.SelectRows(Source, each ([ID] <> "COUNT:"))

The problem is, that the first column name can be different:

NameCity
APrague
BStuttgart
CCardiff
COUNT:3

How to replace the hard coded column name in the SelectRows formula?

I tried to get first column as a text or as a table, but it does not work for the "each" construction in the SelectRows formula

let
Source = Excel.CurrentWorkbook(){[Name="Tabulka1"]}[Content],
GetColumnNames = Table.ColumnNames(Source),
GetFirstColumn = List.First(GetColumnNames),
FirstColAsTable = #table(1, {{GetFirstColumn}}),
#"Filtered Rows" = Table.SelectRows(Source, each (FirstColAsTable <> "COUNT:"))
in
#"Filtered Rows"

 

Thank You

Zdenek Moravec

Cesky Krumlov, Czech Republic

5 Replies
best response confirmed by Zdenek_Moravec (Brass Contributor)
Solution

@Zdenek_Moravec See attached workbook. It contains two tables based on your example where the row with "Count:" in whichever the first column is called in a table, is filtered out.

@Zdenek_Moravec 

How about simply this?

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    FirstColumn = Table.ColumnNames ( Source ){0},    
    FilteredRows = Table.SelectRows(Source, each (Record.Field (_, FirstColumn) <> "Count:"))
in
    FilteredRows
Hello @Riny_van_Eekelen,
Record.Field (_, Columnname) was the magic keyword. Then the shrinked step is
= Table.SelectRows(Source, each (Record.Field (_, List.First(Table.ColumnNames(Source))) <> "COUNT:"))
works prefect, thank You!
Zdenek

@Zdenek_Moravec 

As variant

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    FilteredRows = Table.SelectRows(Source, each (Record.FieldValues(_){0} <> "Count:"))
in
    FilteredRows
1 best response

Accepted Solutions
best response confirmed by Zdenek_Moravec (Brass Contributor)
Solution

@Zdenek_Moravec See attached workbook. It contains two tables based on your example where the row with "Count:" in whichever the first column is called in a table, is filtered out.

View solution in original post