Hello people, who knows how to split data by different delimiters BUT function must look through all rows. For example Splitter.SplitTextByAnyDelimiter() splits data and it takes as pattern data in the first row, if there will be 5 columns after splitting data in the first row then power query keep only 5 columns, even though second or third rows can produce 6 or 7 columns, data after 5th column from these rows actually is lost.
Best regards!
You cannot split each row into separate columns.
Add a column with Text.Split , you can pass a different separator to each row. The result will be a list, with List.FirstN you can keep only 5 items.
Then, you can combine the list back with a unique separator. Finally, split this column by the unique separator you specified.
All in one formula for a new column:
try Text.Combine(List.FirstN(Text.Split([Column1]," "),5),"|") otherwise null
Of course I can't and I don't try, I mean I have this function and three rows = Table.SplitColumn(#"Table Name", "Column Name", Splitter.SplitTextByAnyDelimiter({" ", ", ", "-"}, QuoteStyle.Csv)).
1. USA State, City-County-Province
2. France-City-County-Province
3. United Kingdom-Great Britain-London-Europe, Example
As you know Splitter function will produce only 5 columns even though third row has more values and word Europe & London will not be shown in splitted version of data.
Hard to understand how you need to have the result, I'll have to guess.
I'll assume that you want to split into 5 columns AND keep whatever is after 5 elements into another column. Or you want 4 items in first 4 columns, and the rest in column 5?
Please provide clear descriptions next time, to save our time and get a faster and accurate solution, that's why a sample file with before and after examples is the best way to go.
Whenever I have a challenge, I go to the list of available power query functions searching for one that might fit my scenario.
In this case, if you want to have more than 1 delimiter add a new column with Text.SplitAny instead of Text.Split:
Text.SplitAny([Column1],"- ,")
Note that consecutive delimiters will create a blank item, you might want to replace in the entire column ", " with ","
In the new column, you will have each entry as a list of items, you can apply List functions to this column.
You can: add a column with Text.Combine(List.FirstN([Custom],5),"|")
You can split this column by the new delimiter, you will get 5 columns.
https://docs.microsoft.com/en-us/powerquery-m/list-firstn
Add now another column with: Text.Combine(List.RemoveFirstN([Custom],5),", ")
https://docs.microsoft.com/en-us/powerquery-m/list-removefirstn
This last column will have all items after 5 items.
Thank you for response. I added the file to explain what I mean.
The main point is to split data by the longest string and with new added data in source to keep it updated. Spliitter functions gives hard coded columns names, how retrieve them and make them dynamic if they only occurs during this step?
I'm not able to open your pbix file, it's incompatible with my PBI version (Version: 2.84.981.0 64-bit (August 2020))
Can you provide the example of the desired result in excel?
Yes, of course. I added. In the query that it is called AtemptToAutomate everything works until I add word in front of the longest string of text. For example, in third row if I add new words at the end of cell in the source, number of columns increases but if I add new word in front of UnitedKingdom the query doesn't show proper number of columns. It looks like that it is limitation of Text.SplitAny function.
Really don't understand what you mean.
I tried your query, with both scenarios:
-adding "/aaa" at the end of the third value,
-adding "aaa/" at the beginning of the third value.
The result is correct, I get 8 columns.
How can I replicate what you're saying?
Are you adding a delimiter NOT specified in Text.SplitAny? Please provide all details, don't let me guess what you did.
Here is a file, with problem. List.Max doesn't work properly, I can't see why.
List.Max cannot be used on text values, will apply an alphabetical text sorting, in your example list.max will return the text that starts with USA...
You have to transform from text to a count, use this step instead of what you have:
Table.SplitColumn(#"Added Custom", "Custom", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), List.Max(List.Transform(#"Added Custom"[Custom],each List.Count(Text.Split(_,";")
))))
Or, in 2 steps:
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom", each List.Count(Text.SplitAny([Data]," /-"))),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each Text.Combine(Text.SplitAny([Data]," /-"),";")),
#"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom1", "Custom.1", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), List.Max(#"Added Custom1"[Custom]))
in
#"Split Column by Delimiter"
Or:
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom", each Text.SplitAny([Data]," /-")),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each Text.Combine(Text.SplitAny([Data]," /-"),";")),
#"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom1", "Custom.1", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), List.Max(List.Transform(#"Added Custom1"[Custom],each List.Count(_))))
in
#"Split Column by Delimiter"
Thank you very much! I've understood the problem with my approach.
Best regards!