Forum Discussion
Is it possible to Ungruoup one column into 3 columns using get & transfrom
- Oct 07, 2017
Hi Joana,
Perhaps there is more clever solution, but i did that in straigforward way - split Article on levels and fill each level by it names. Steps are in attached file, generated code is
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], DuplicateArticle = Table.AddColumn(Source, "Artcl", each [Article]), NullValueToZero = Table.ReplaceValue(DuplicateArticle,null,0,Replacer.ReplaceValue,{"Value"}), SplitArticleOnLevels = Table.SplitColumn(NullValueToZero, "Article", Splitter.SplitTextByDelimiter(".", QuoteStyle.Csv), {"Article.1", "Article.2", "Article.3"}), EmptyToNull = Table.ReplaceValue(SplitArticleOnLevels,"",null,Replacer.ReplaceValue,{"Article.2", "Article.3"}), AddFirstLevel = Table.AddColumn(EmptyToNull, "Custom1", each if [Article.2] = null then [Description] else null), FilledItDown = Table.FillDown(AddFirstLevel,{"Custom1"}), AddSecondLevel = Table.AddColumn(FilledItDown, "Custom2", each if [Article.3] = null and [Article.2] <> null then [Description] else null), NotRequired = Table.AddColumn(AddSecondLevel, "Custom22", each if [Article.2] = null then "" else [Custom2]), FillSecond = Table.FillDown(NotRequired,{"Custom22"}), RemoveNotRequired = Table.RemoveColumns(FillSecond,{"Custom2"}), AddThirdLevel = Table.AddColumn(RemoveNotRequired, "Custom3", each if [Article.2] <> null and [Article.3] <> null then [Description] else ""), RenameColumns = Table.RenameColumns(AddThirdLevel,{{"Artcl", "Article"}, {"Custom1", "Group"}, {"Custom22", "sub-group"}}), RemoveTempColumns = Table.SelectColumns(RenameColumns,{"Value", "Article", "Group", "sub-group", "Custom3"}), RenameFinally = Table.RenameColumns(RemoveTempColumns,{{"Custom3", "description"}}), Reordered = Table.ReorderColumns(RenameFinally,{"Article", "Group", "sub-group", "description", "Value"}), ProperType = Table.TransformColumnTypes(Reordered,{{"Article", type text}, {"Group", type text}, {"sub-group", type text}, {"description", type text}, {"Value", type number}}) in ProperType
This is the script I am using to try to do what you suggest above with the 5 chapters I have. Add Custom Column = [#"CAP1"]*10000 + [#"CAP2"] * 1000 +[#"CAP3"] * 100 + [#"CAP4"] * 10 + [#"CAP5"] but it's giving an error.
I already sent you one dummy file and here goes another and the script I am using to combine these 2 files from a folder is:
let
Source = Folder.Files("C:\Users\Joana\Documents\Orçamentos"),
#"Invoke Custom Function1" = Table.AddColumn(Source, "Transform File from Orçamentos", each #"Transform File from Orçamentos"([Content])),
#"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
#"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File from Orçamentos"}),
#"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File from Orçamentos", Table.ColumnNames(#"Transform File from Orçamentos"(#"Sample File"))),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded Table Column1",{{"Source.Name", type text}, {"Index", type any}, {"Letra", type any}, {"CAP1", type any}, {"CAP2", type any}, {"CAP3", type any}, {"CAP4", type any}, {"CAP5", type any}, {"Artigo", type any}, {"Group", type any}, {"Sub-Group", type any}, {"DESIGNAÇÃO", type any}, {"QT", type any}, {"UN", type any}, {"Fator", type any}, {"Fornecedor", type any}, {"Preço/Unitário", type any}, {"Parcial", type any}, {"Total Custo", type any}, {"Observações", type any}})
in
#"Changed Type"
All my files are in the c:\documents\orçamentos folder.
Thanks a lot for all your help, I hope I can be of some help to you sometime.
Joanna,
Let do step by step. Here is the script and sorting file based on your initial sample. Initially I tried to avoid any coding and use only UI to generate the script, but very short manual combining of formulas splitting and sorting through the list makes things much easier - we don't depend on number of dots in Articles.
This part is commented:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
DefaultTypes = Table.TransformColumnTypes(Source,{{"Article", type text}, {"Description", type text}, {"Value", Int64.Type}}),
DuplicateArticles = Table.DuplicateColumn(DefaultTypes, "Article", "ArticleCopy"),
SplitArticles = Table.SplitColumn(DuplicateArticles, "ArticleCopy",
Splitter.SplitTextByDelimiter(".", QuoteStyle.Csv), {"Article.1", "Article.2", "Article.3"}),
LevelsToNumbers = Table.TransformColumnTypes(SplitArticles,
{{"Article.1", Int64.Type}, {"Article.2", Int64.Type}, {"Article.3", Int64.Type}}),
AddLevel1 = Table.AddColumn(LevelsToNumbers, "Custom1", each if [Article.2] = null then [Description] else null ),
FillLevel1 = Table.FillDown(AddLevel1,{"Custom1"}),
AddLevel2 = Table.AddColumn(FillLevel1,
"Custom2", each if [Article.3] = null then
if [Article.2] <> null then [Description] else ""
else null),
FillLevel2 = Table.FillDown(AddLevel2,{"Custom2"}),
AddLevel3 = Table.AddColumn(FillLevel2, "Custom3", each if [Article.3] = null then "" else [Description] ),
RemoveTempColumns = Table.SelectColumns(AddLevel3,{"Value", "Article", "Custom1", "Custom2", "Custom3"}),
ReorderColumns = Table.ReorderColumns(RemoveTempColumns,{"Article", "Custom1", "Custom2", "Custom3", "Value"}),
RenameColumns = Table.RenameColumns(ReorderColumns,{
{"Custom1", "Group"},
{"Custom2", "sub-group"},
{"Custom3", "Description"}}),
ProperColumnsType = Table.TransformColumnTypes(RenameColumns,{
{"Article", type text},
{"Group", type text},
{"sub-group", type text},
{"Description", type text}
}),
// Here we convert each Article into the list, like
// "3.2.11" => {"3","2","11"}
ArticelToList = Table.AddColumn(ProperColumnsType, "Custom", each Text.Split([Article],".")),
// Next by Text.PadStart add zeroes to the start of each list element to have totally 4 symbols
// and transform it into one text string, like
// {"3","2","11"} => {"0003","0002","0011"} => "000300020011"
ListToSortString = Table.TransformColumns(ArticelToList,
{"Custom", each Text.Combine(List.Transform(_, each Text.PadStart(_,4,"0")), ""), type text}),
// Sort by this column and remove it then
SortAscending = Table.Sort(ListToSortString,{{"Custom", Order.Ascending}}),
AndRemoveIt = Table.RemoveColumns(SortAscending,{"Custom"})
in
AndRemoveItFile is attached.
Please not your resulting table is sorted by PowerQuery on each refresh if only you set External Table properties (right click on it and check menu) as this
- SergeiBaklanNov 13, 2017Diamond Contributor
Something like attached
- SergeiBaklanNov 13, 2017Diamond Contributor
Hi Joana,
Raw idea is to create separate query with subtotals, merge it with main one before sorting (generating proper columns to merge), sort after that.
I could play with that in couple of days, on business trip now.
Regards,
Sergei
- Joana Villas-BoasNov 12, 2017Iron Contributor
Hi Sergei,
My model is almos finished. You helped me a lot with creating a list in the format I needed. After that I used Get&Transform to append the total number of rows 4 times, so each item could be attributed to 1,2, 3 or 4 suppliers. So I added a column to the result of our query that says supplier with a Data Validation that gets a list from all suppliers. (I know that's not a very good Idea because if the query gets refreshed the nr of rows can change and the suppliers can get all mixed up)
The only manual work the user has to do is to select the suppliers for each item and then a Macro creates one sheet by supplier that will be protected and sent to each supplier for them to fill prices and quantities. It's working great.
Now my client ask me if it is possible to have a subtotal row in our query bellow each item that repeats 4 times. 3.1.1 for exemple will apear 4 times in our result query and they want a subtotal before that. If we use group on the query editor we will loose the detail refight? I only know how to do sub-totals in the result pivot table, but not the query.
Thank you in advance for your help and patience with all my questions.
Regards, Joana
- Joana Villas-BoasNov 06, 2017Iron Contributor
Thank you so much Sergei, that was exactly the issue. I didn't know it was important to have the same name on every table! Fantastic!!! I dont know how to thank you for so much help!
- SergeiBaklanNov 05, 2017Diamond Contributor
Joana, with this method you shall same name for the tables in all files you combine (or same name for sheets if you combine by sheets).
When you combine files you have the prompt "Select the object to be extracted from each file", take it literally.
Script takes the table, let say "MyTable", from each file in the list and combines them. Or "MySheet" if you combine by sheets. No other tables/sheets. If it doesn't find such you have an error.
You shall use same names or another logic to combine.
- Joana Villas-BoasNov 05, 2017Iron Contributor
Fantastic, it worked! Many thanks. When you figure out why the combine files from folder is not working, please let me know. I lost a hole day today trying to figure it out and no luck. Thanks a lot. You've been a great help!