I have an export out of our ERP system of cost centers.
I am trying to transform this from a tabular structure to a horizontal structure based on the number of levels the node has.
I have a mock-up attached. In this file, I have used the TRANSPOSE(UNIQUE) formula to get the number of levels in the node and an IF formula to transform the structure, this is highlighted in yellow. I'm sure this can be done in PQ, which I am decent at, but I need help completing this task.
Ali Enthusiastic self-taught user of MS Excel who's always learning! Don't forget to say "thank you" in your thread to anyone who has offered you help.
You can reward them by clicking on * Add Reputation below their user name on the left, if you wish. Forum Rules (updated August 2023): please read them here.
Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
MS-Off Ver
MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
Posts
80,928
Re: Transform Table Structure
I wish you'd said that before wes started!
I am not the best at PQ, and there will be others better than I, but if I were doing it, I'd split each column conditionally (based on text or number), then fill down the text, then rejoin conditionally, but it would have to be done on each column separately.
It is early evening here and I'm about to log off, but I'll throw up a call for help before I do.
My apologies, I assumed (incorrectly) that I could fill down and get my desired outcome. It was only after I had a working PQ option that I realized that this would not work.
Thank you for all the time you put into this question.
Last edited by AliGW; 06-20-2023 at 01:37 PM.
Reason: Please do NOT quote unnecessarily.
This solution accomplishes what I am trying to do. How would you even go about writing a formula such as this? It works!!
Can it be altered to pull the cost center number into the last column? I have added this formula after I pasted the above formula:
Formula
=LET(z,TEXTBEFORE(A2:A345," (",,,1),c,SEQUENCE(,6),d,VSTACK("|- ","< ",UNICHAR(12288)),y,IFNA(TEXTSPLIT(TEXTAFTER(z,d,c),d),"|"),
SUBSTITUTE(TRANSPOSE(SCAN("",TRANSPOSE(y),LAMBDA(a,v,IF(v="",a,v)))),"|",))
Power Query
PHP Code:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], Clean = Table.TransformColumns(Source, {"Cost Center Hierarchy", each Text.Combine(Text.SplitAny(Text.BeforeDelimiter(_," ("),"|- <")) }), Split = Table.SplitColumn(Clean, "Cost Center Hierarchy", Splitter.SplitTextByDelimiter(" "), {"1".."6"}), Fill = List.Accumulate({{null,"|"},{"",null},{0},{"|",null}},Split, (s,l)=> if l{0}=0 then Table.FillDown(s,{"1".."6"}) else Table.ReplaceValue(s,l{0},l{1},Replacer.ReplaceValue,{"1".."6"})) in Fill
Bookmarks