Forum Discussion
Remove Column/s which contain only one specitic value (no other one value is there)
- 3 years ago
Once in Power Query, you have to make an assumption in order to map the column names to the column letter in Excel.
In the code below I am assuming that the first column of your PQ table is Column B in Excel. If that is not the case, you will need to edit the code that looks for the zero's and no's to look at the correct columns.
Read the code and the comments to understand the algorithm.
let //edit next line to reflect actual data source Source = Excel.CurrentWorkbook(){[Name="Table9"]}[Content], //edit next line to reflect actual column headers // or set data types using a dynamic method #"Changed Type" = Table.TransformColumnTypes(Source, {{"20220308", Int64.Type}, {"20220309", Int64.Type}, {"20220310", Int64.Type}, {"its possible 1", type text}, {"its possible 2", type text}, {"Its possible3", type text}, {"no remove", type text}}), //ASSUMES table starts with column B (adjust list ranges if that is not the case) // Check for 0's will be columns 1,2,3 // Check for "NO"'s will be columns 4,5,6 #"Check for Zero" = List.Range(Table.ColumnNames(#"Changed Type"),0,3), #"Check for NO" = List.Range(Table.ColumnNames(#"Changed Type"),3,3), #"All Zero" = List.Accumulate(#"Check for Zero",{}, (state, current)=> if List.MatchesAll(Table.Column(#"Changed Type",current), each _ = 0) then state & {current} else state), #"All NO" = List.Accumulate(#"Check for NO",{}, (state, current)=> if List.MatchesAll(Table.Column(#"Changed Type",current), each _ = "NO") then state & {current} else state), //Delete the relevant columns #"Delete Columns" = Table.RemoveColumns(#"Changed Type", #"All Zero" & #"All NO") in #"Delete Columns"
Sorry I forgot mention that I can determinate which Value in which Column I seek.
In this example I determinate:
If in column range B:D is Column which has only 0 value, this will be removed.
If in column range E:G is Column which has only "NO" value, this will be removed.
Jan
Once in Power Query, you have to make an assumption in order to map the column names to the column letter in Excel.
In the code below I am assuming that the first column of your PQ table is Column B in Excel. If that is not the case, you will need to edit the code that looks for the zero's and no's to look at the correct columns.
Read the code and the comments to understand the algorithm.
let
//edit next line to reflect actual data source
Source = Excel.CurrentWorkbook(){[Name="Table9"]}[Content],
//edit next line to reflect actual column headers
// or set data types using a dynamic method
#"Changed Type" = Table.TransformColumnTypes(Source,
{{"20220308", Int64.Type}, {"20220309", Int64.Type}, {"20220310", Int64.Type}, {"its possible 1", type text}, {"its possible 2", type text}, {"Its possible3", type text}, {"no remove", type text}}),
//ASSUMES table starts with column B (adjust list ranges if that is not the case)
// Check for 0's will be columns 1,2,3
// Check for "NO"'s will be columns 4,5,6
#"Check for Zero" = List.Range(Table.ColumnNames(#"Changed Type"),0,3),
#"Check for NO" = List.Range(Table.ColumnNames(#"Changed Type"),3,3),
#"All Zero" = List.Accumulate(#"Check for Zero",{}, (state, current)=>
if List.MatchesAll(Table.Column(#"Changed Type",current), each _ = 0) then state & {current} else state),
#"All NO" = List.Accumulate(#"Check for NO",{}, (state, current)=>
if List.MatchesAll(Table.Column(#"Changed Type",current), each _ = "NO") then state & {current} else state),
//Delete the relevant columns
#"Delete Columns" = Table.RemoveColumns(#"Changed Type", #"All Zero" & #"All NO")
in
#"Delete Columns"
- Kopec3 years ago
Helper I
Thank you, it works great:).
Jan