Apr 01 2022 08:10 AM
I have a .txt file that I am running through Power Query and into Excel. The txt file is a simple format that has an item ID on one line and an error message on the next, eg:
Item ABC
Error Msg
Item DEF
Error Msg
Item GHI
Error Msg
...
Ultimately, what I want is one column with the Item ID, and one with the error message. Simple enough. So after I pull the txt file in, I add a custom column :
if Text.Start([Column1],4) = "Item" then "ITEM ID" else "ERROR MSG"
This then give me alternating rows of "ITEM ID" and "ERROR MSG". Then I pivot on the new column using the original column as the Values Column and "Don't Aggregate" for the function, expecting to get my final result. However all I get is a table with two error cells and the error is "There weren't enough elements in the enumeration to complete the operation."
How can I fix this or is there another way to accomplish this simple task in Power Query?
Apr 01 2022 08:54 AM
Apr 01 2022 09:27 AM
@adinezzaYour method probably works also if just add an index column.
Apr 30 2024 02:10 AM
@Riny_van_Eekelen Hi Riny, I have a similar issue, see attached. Similar data, but one column with IDs, then a bunch of numbers, I want all the numbers related to ID under it as a column header. Any help appreciated!
Apr 30 2024 03:31 AM
@davidmaddock54 Thought of a quick-and-dirty way to fix it, though believe there should be a better way to do it.
Apr 30 2024 09:31 AM
You can use group and transpose:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
ChangeType = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Number", Int64.Type}}),
grouped = Table.Group(ChangeType, {"ID"}, {{"NumbersGroup", each _, type nullable table[ID = nullable text, Number = nullable Int64.Type]}}),
TransposeGroups = Table.AddColumn(grouped, "TransposedTable", each Table.Transpose([NumbersGroup])),
KeepNumbers = Table.AddColumn(TransposeGroups, "TransposedNumbers", each Table.LastN([TransposedTable], 1)),
combined = Table.Combine(KeepNumbers[TransposedNumbers]),
TransposeCombined = Table.Transpose(combined),
zipped = List.Zip({Table.ColumnNames(TransposeCombined),grouped[ID]}),
renamed = Table.RenameColumns(TransposeCombined, zipped)
in
renamed
May 01 2024 09:22 AM
I didn't dig your query, but it looks like result is not correct
@rachel , we may simplify it to
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
GroupedID = Table.Group(Source, {"ID"}, {{"Columns", each [Number]}}),
Result = Table.FromColumns( GroupedID[Columns], GroupedID[ID] )
in
Result
@davidmaddock54 , if order of numbers in the source doesn't matter, perhaps it's better to sort them in columns as in next query in attached attached
May 01 2024 11:11 AM
@SergeiBaklan Oops! Must have been confused.
May 01 2024 10:25 PM
Thank you! the code is so straightforward and self explanatory!
By comparison, the code generated by pressing "Group By" button now looks like a mess!