Forum Discussion
Combine columns if not null or empty
- 9 years ago
Good catch Marcel but I have other columns :)
Thanks for your help, you pointed me to the right way !
here the solution :
= Table.AddColumn(#"Reordered Columns", "Personnalisé", each Text.Combine(List.Select(Record.FieldValues(Record.FromList({[Col1],[Col2],[Col3]}, type [Col1 = text,Col2 = text,Col3 = text])), each _<> "" and _ <> null)," & "))
Thanks again
- 9 years ago
Pleased you solved your own issue.
I'd rather had shortened the code a bit:
= Text.Combine(List.Select({[Col1],[Col2],[Col3],[Col4]}, each _<> "" and _ <> null)," & ")
I have run yor M code and it works, but when I analyzed the code I realized you took advantange that every cell with data in my table has lenght just one (it just for a brief example) and you split column by position. In this case position "1" always works.
Imagine that in the cells of a certain row were just a word like "Luke", now the split position would be the length of the word "Luke". In the other rows could be another different length...
I suppose that an algorithm following your idea would be: calculate the maximum of the lenghts of each items on each row, maybe you have to use a dummy column with a value to concatenate further.... or maybe just a different approach to solve the problem!
Hi,
There should be a solution to that as well. Please take a practical example and show the expected result.
- Ashish_Mathur8 years agoSuper User
Hi,
Try this M code
let
Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Name1", type text}, {"Name2", type text}, {"Name3", type text}, {"Name4", type text}}),
#"Inserted Merged Column" = Table.AddColumn(#"Changed Type", "Merged", each Text.Combine({[Name1], [Name2], [Name3], [Name4]}, ";"), type text),
#"Trimmed Text" = Table.TransformColumns(#"Inserted Merged Column",{{"Merged", Text.Trim}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Trimmed Text", "Merged", Splitter.SplitTextByEachDelimiter({";"}, QuoteStyle.Csv, false), {"Merged.1", "Merged.2"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Merged.1", type text}, {"Merged.2", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Merged.2"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Merged.1", "Result"}})
in
#"Renamed Columns"Hope this helps.
- Anonymous8 years agoNot applicable
Here you have. It is the same than the previous one but just using words of different lenght in the rows. Before I only used characters of lenght 1...
- Anonymous8 years agoNot applicable
Of course it helps! It works! I shows me the power of the delimeters! Thank you very much!
- Ashish_Mathur8 years agoSuper User
Hi,
You are welcome. Please mark my reply as Answer.
- Anonymous8 years agoNot applicable
I cannot see where to mark your reply as an answer.. In the place where there are 3 points in a row there is not such option in the scroll down menu... maybe it's only allowed just one "solved" per post (thread)