Forum Discussion
Dynamically select column based on part of Header
- 4 years ago
Hi Anonymous - try this.
First of all, here is the file.Here is the "table" I am working with. Blue is your source data, green is what I am returning.
Here is the code to generate the columns to keep:
let Source = let varDate = DateTime.Date(DateTime.LocalNow()), varYear = if Date.Month(varDate) > 3 then Date.Year(varDate) else Date.Year(varDate) - 1 in {Text.From(varYear)}, FixedTypes = {"PTD", "YTD", "Approved Amount"}, Custom1 = List.Combine({Source, FixedTypes}), #"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), {"Types"}, null, ExtraValues.Error), #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Types", type text}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Categories", each Categories), #"Expanded Categories" = Table.ExpandTableColumn(#"Added Custom", "Categories", {"Categories"}, {"Categories.1"}), #"Merged Columns" = Table.CombineColumns(#"Expanded Categories",{"Types", "Categories.1"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"New Columns")[New Columns] in #"Merged Columns"I am determining if I should pull 2022 or 2023 in the Source line. Then I convert that to text
The FixedTypes step are the hardcoded types you always want.
Then I convert it a table, and add a new column that pulls in ALL of the category table, which looks like this:
So added to a column the way I did, which was to just type = Categories in the Add Custom Column box (it looks like 'each Categories' in the code above) returns a table like this:
Then I expanded the Categories column. That gives me a cross join of all possible columns
I then merge those two columns with a space as a separator, then convert it to a list. This list is called ColumnsToKeepThen I use that list in my actual table.
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Removed Other Columns" = Table.SelectColumns(Source,ColumnsToKeep, MissingField.Ignore) in #"Removed Other Columns"For columns generated that do not exist are simply skipped.
You can modify the date logic and add whatever you want to the fixed list. All of that can even be tables in Excel or SharePoint list or wherever to generate this data.
If you need more help, please post data. I wasted 10min converting your typing above into something usable and had to deal with web garbage of unichar 160 - the nonbreaking space.
How to get good help fast. Help us help you.
How To Ask A Technical Question If you Really Want An Answer
How to Get Your Question Answered Quickly - Give us a good and concise explanation
How to provide sample data in the Power BI Forum - Provide data in a table format per the link, or share an Excel/CSV file via OneDrive, Dropbox, etc.. Provide expected output using a screenshot of Excel or other image. Do not provide a screenshot of the source data. I cannot paste an image into Power BI tables.
Hi Anonymous - try this.
First of all, here is the file.
Here is the "table" I am working with. Blue is your source data, green is what I am returning.
Here is the code to generate the columns to keep:
let
Source =
let
varDate = DateTime.Date(DateTime.LocalNow()),
varYear = if Date.Month(varDate) > 3 then Date.Year(varDate) else Date.Year(varDate) - 1
in
{Text.From(varYear)},
FixedTypes = {"PTD", "YTD", "Approved Amount"},
Custom1 = List.Combine({Source, FixedTypes}),
#"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), {"Types"}, null, ExtraValues.Error),
#"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Types", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Categories", each Categories),
#"Expanded Categories" = Table.ExpandTableColumn(#"Added Custom", "Categories", {"Categories"}, {"Categories.1"}),
#"Merged Columns" = Table.CombineColumns(#"Expanded Categories",{"Types", "Categories.1"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"New Columns")[New Columns]
in
#"Merged Columns"
I am determining if I should pull 2022 or 2023 in the Source line. Then I convert that to text
The FixedTypes step are the hardcoded types you always want.
Then I convert it a table, and add a new column that pulls in ALL of the category table, which looks like this:
So added to a column the way I did, which was to just type = Categories in the Add Custom Column box (it looks like 'each Categories' in the code above) returns a table like this:
Then I expanded the Categories column. That gives me a cross join of all possible columns
I then merge those two columns with a space as a separator, then convert it to a list. This list is called ColumnsToKeep
Then I use that list in my actual table.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Removed Other Columns" = Table.SelectColumns(Source,ColumnsToKeep, MissingField.Ignore)
in
#"Removed Other Columns"
For columns generated that do not exist are simply skipped.
You can modify the date logic and add whatever you want to the fixed list. All of that can even be tables in Excel or SharePoint list or wherever to generate this data.
If you need more help, please post data. I wasted 10min converting your typing above into something usable and had to deal with web garbage of unichar 160 - the nonbreaking space.
How to get good help fast. Help us help you.
How To Ask A Technical Question If you Really Want An Answer
How to Get Your Question Answered Quickly - Give us a good and concise explanation
How to provide sample data in the Power BI Forum - Provide data in a table format per the link, or share an Excel/CSV file via OneDrive, Dropbox, etc.. Provide expected output using a screenshot of Excel or other image. Do not provide a screenshot of the source data. I cannot paste an image into Power BI tables.
- AlexisOlson4 years agoSuper User
You can also compute a Cartesian product (cross join) using List.Transform.
For example, you could define ColumnsToKeep like this:
let Year = Number.ToText(Date.Year(Date.AddMonths(DateTime.LocalNow(), -3))), FixedTypes = { "Approved Amount", Year, "PTD", "YTD" }, FixedCategories = { "Foundations", "Concrete" }, Other = { "Project Name", "Project Number", "Project Category" }, TypesAndCategories = List.Transform( FixedTypes, (T) => List.Transform(FixedCategories, (C) => T & " " & C) ), Combined = List.Combine(TypesAndCategories) & Other in Combined