Forum Discussion
Convert table that is using #lf (line feed)
- 10 months ago
Some similarity with DataNinja777 's but split/zip/convert to table in one line.
See attached Excel workbook.
Code:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], // Keep note of original column headers and their order: ColmHdrs = Table.ColumnNames(Source), AddedCustom = Table.AddColumn(Source, "Custom", each Table.FromRows(List.Zip({Text.Split([Unit], "#(lf)"),Text.Split([qty], "#(lf)"),Text.Split([Year], "#(lf)")}),{"Unit","qty","Year"})), RemovedColumns = Table.RemoveColumns(AddedCustom,{"Unit", "qty", "Year"}), ExpandedCustom = Table.ExpandTableColumn(RemovedColumns, "Custom", {"Unit", "qty", "Year"}, {"Unit", "qty", "Year"}), // use the original column order from th ColmHdrs step: ReorderedColumns = Table.ReorderColumns(ExpandedCustom,ColmHdrs) in ReorderedColumns - 10 months ago
You can add a step like:
ReplacedValue = Table.ReplaceValue(ExpandedCustom,each [qty],each try Number.From([qty]) otherwise [qty] ,Replacer.ReplaceValue,{"qty"})
This leaves the column as type Any but the individual values will be type number or type text depending on content, and so they'll appear in Excel as numbers/text in cells (justified right if numbers and justified left if text).
For Year you could do something similar. They could be combined in a single step but that would require (I think) an inline function but I've been lazy.
Some similarity with DataNinja777 's but split/zip/convert to table in one line.
See attached Excel workbook.
Code:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
// Keep note of original column headers and their order:
ColmHdrs = Table.ColumnNames(Source),
AddedCustom = Table.AddColumn(Source, "Custom", each Table.FromRows(List.Zip({Text.Split([Unit], "#(lf)"),Text.Split([qty], "#(lf)"),Text.Split([Year], "#(lf)")}),{"Unit","qty","Year"})),
RemovedColumns = Table.RemoveColumns(AddedCustom,{"Unit", "qty", "Year"}),
ExpandedCustom = Table.ExpandTableColumn(RemovedColumns, "Custom", {"Unit", "qty", "Year"}, {"Unit", "qty", "Year"}),
// use the original column order from th ColmHdrs step:
ReorderedColumns = Table.ReorderColumns(ExpandedCustom,ColmHdrs)
in
ReorderedColumns
Hello p45cal , I have another simple question.
When the table is expanded, the type is set to text.
My column "qty" contain numbers and text (N/A).
I tried to set the column as "type any" but the number remains as a text.
How would I change the query to make the output in the "qty" column to be a number when it is one and text for the rest?
- p45cal10 months agoSolution Supplier
You can add a step like:
ReplacedValue = Table.ReplaceValue(ExpandedCustom,each [qty],each try Number.From([qty]) otherwise [qty] ,Replacer.ReplaceValue,{"qty"})
This leaves the column as type Any but the individual values will be type number or type text depending on content, and so they'll appear in Excel as numbers/text in cells (justified right if numbers and justified left if text).
For Year you could do something similar. They could be combined in a single step but that would require (I think) an inline function but I've been lazy.
- DanFromMontreal10 months agoHelper IV
Wow... simple. Just what I wanted.
Thank you dufoq3
- dufoq310 months agoCommunity Champion
DanFromMontreal Did you mean me? You've replied to p45cal