Forum Discussion
Transform text lines into date columns
Hi.
I have a budget control that must show how much money was budgeted and how much was spent and compare both using a columns and line visual. The problem is my data has no date because the months with the budgeted and spent amount are columns. Here's what I mean (month abreviation is in portuguese):
I have:
| Project | Budget Jan | Spent Jan | Budget Fev | Spent Fev | Budget Mar | Spent Mar | Budget Abr | SpentAbr | Budget Mai | SpentMai | Budget Jun | SpentJun | Budget Jul | SpentJul | Budget Ago | SpentAgo | Budget Set | SpentSet | Budget Out | SpentOut | Budget Nov | SpentNov | Budget Dez | SpentDez |
| 1 | 12.543,32 | 2.526,48 | 1.115,66 | 2.526,48 | 1.115,66 | 2.526,48 | 1.115,66 | 0,00 | 1.115,66 | 0,00 | 2.231,33 | 0,00 | 2.231,33 | 0,00 | 2.231,33 | 0,00 | 2.231,33 | 0,00 | 2.789,16 | 0,00 | 2.789,16 | 0,00 | 3.346,99 | 0,00 |
| 2 | 37.702,98 | 0,00 | 0,00 | 2.060,03 | 0,00 | 271,76 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 |
| 3 | 0,00 | 0,00 | 3.188,93 | 1.294,62 | 3.188,91 | 1.819,48 | 3.188,91 | 0,00 | 3.188,91 | 0,00 | 6.377,82 | 0,00 | 6.377,82 | 0,00 | 6.377,82 | 0,00 | 6.377,82 | 0,00 | 7.972,28 | 0,00 | 7.972,28 | 0,00 | 9.566,73 | 0,00 |
| 4 | 0,00 | 0,00 | 2.212,99 | 1.095,88 | 2.212,98 | 816,26 | 2.212,98 | 0,00 | 2.212,98 | 0,00 | 4.425,95 | 0,00 | 4.425,95 | 0,00 | 4.425,95 | 0,00 | 4.425,95 | 0,00 | 5.532,44 | 0,00 | 5.532,44 | 0,00 | 6.638,93 | 0,00 |
| 5 | 5.713.616,84 | 5.199,03 | 0,00 | 22.813,24 | 0,00 | 24.664,47 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 | 0,00 |
I need:
| Project | Month | Budget | Spent |
| 1 | Jan | 12543,3 | 2526,48 |
| 1 | Fev | 1156,66 | 2526,48 |
| … | … | … | … |
| 5 | Nov | 0,00 | 0,00 |
| 5 | Dez | 0,00 | 0,00 |
And so on. How to do that in Power Query?
Thank you very much in advance!
Please try this WilliamAzevedo
What I think the reason could be is, that your data does not seem to have strictly two rows for a particular project number and month i.e. The Budget and The Spent. Apart from these two, there are certain rows for a particular project number and month which are null as well that get picked up while expanding when just "Column1" and "Column2" are asked to be picked up by the code. I'll leave the changed code where I have tried remove any null values from the list after grouping ( List.RemoveNulls(_[Value]) ) which eventually expand just the rows with numbers. Thanks!let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Project", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Project"}, "Attribute", "Value"),
Custom1 = Table.TransformColumns(#"Unpivoted Other Columns", {"Attribute", each Text.End(_,3)}),
#"Grouped Rows" = Table.Group(Custom1, {"Project", "Attribute"}, {{"All", each _[Value]}}),
Custom2 = Table.TransformColumns(#"Grouped Rows",{"All", each List.RemoveNulls(_)}),
#"Extracted Values" = Table.TransformColumns(Custom2, {"All", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "All", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"All.1", "All.2"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"All.1", type text}, {"All.2", type text}})
in
#"Changed Type1"
19 Replies
- lbendlinSuper User
Your source data is inconsistent. Sometimes there is a space after "Spent" and sometimes there is none.
After unpivoting and cleaning your data you can load it into Power BI
- SundarRajSuper User
Hi WilliamAzevedo , here's a solution you could look at. I'll leave the code used below. Thanks!
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Project", Int64.Type}, {"Budget Jan", type text}, {"Spent Jan", type text}, {"Budget Fev", type text}, {"Spent Fev", type text}, {"Budget Mar", type text}, {"Spent Mar", type text}, {"Budget Abr", type text}, {"SpentAbr", Int64.Type}, {"Budget Mai", type text}, {"SpentMai", Int64.Type}, {"Budget Jun", type text}, {"SpentJun", Int64.Type}, {"Budget Jul", type text}, {"SpentJul", Int64.Type}, {"Budget Ago", type text}, {"SpentAgo", Int64.Type}, {"Budget Set", type text}, {"SpentSet", Int64.Type}, {"Budget Out", type text}, {"SpentOut", Int64.Type}, {"Budget Nov", type text}, {"SpentNov", Int64.Type}, {"Budget Dez", type text}, {"SpentDez", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Project"}, "Attribute", "Value"),
Custom1 = Table.TransformColumns(#"Unpivoted Other Columns", {"Attribute", each Text.End(_,3)}),
#"Grouped Rows" = Table.Group(Custom1, {"Project", "Attribute"}, {{"All", each Table.FromRows({_[Value]})}}),
#"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Column1", "Column2"}, {"Column1", "Column2"}),
Custom2 = Table.RenameColumns(#"Expanded All", List.Zip({Table.ColumnNames(#"Expanded All"),{"Project","Month","Budget","Spent"}}))
in
Custom2- WilliamAzevedoAdvocate II
Hi!
Since the data I posted was just an example, I'm having some difficulty on replicating the process to my actual data.
Until here:
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Project"}, "Attribute", "Value"), Custom1 = Table.TransformColumns(#"Unpivoted Other Columns", {"Attribute", each Text.End(_,3)}),I have this result:
But from here I couldn't figure how to make the "Spent" lines become a column. If I try this:
Here's the result:
And lbendlin, I appreciate very much your help! I just need the "Spent" field to be a column in my table, so I can use it as a field in the graph and line visual instead of a matrix.
- SundarRajSuper User
When you grouped the column and recieved the table in the column "Todos". The column that I extracted "Value", is the amount column name "Value" as well? If not, then change the _[ColumnName] that is _[Valor]. Post this, you can simply follow the next steps in the code. Let me know if this works. Thanks
- dufoq3Community Champion
Hi WilliamAzevedo, another solution:
Output
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("xZJZbsMwDETv4u8BIS7ichYj979GZaeNZSQwUBhovzR6oihSw3VdeMHCQt0UKkMPKQ7LDRNzh/svaUNrH4mQKEP1PokssF8RJTVH1Q95YF229jQomqDyCH3laD7k/E4wwt8C/3bZCte3IyXOROn+z1IGl4PullJyPa2Z6Pn2RJw0Ail3SVCFQPKKFHV3hM4d2gc7hOVpIFOrjsyDbjLZIX5m57sTMTLpqH6XdOoqMLsiTq7f3rz663tksJKPstP2LVed502GaQqZcomRu8Hiv6fw8QU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Project = _t, #"Budget Jan" = _t, #"Spent Jan" = _t, #"Budget Fev" = _t, #"Spent Fev" = _t, #"Budget Mar" = _t, #"Spent Mar" = _t, #"Budget Abr" = _t, SpentAbr = _t, #"Budget Mai" = _t, SpentMai = _t, #"Budget Jun" = _t, SpentJun = _t, #"Budget Jul" = _t, SpentJul = _t, #"Budget Ago" = _t, SpentAgo = _t, #"Budget Set" = _t, SpentSet = _t, #"Budget Out" = _t, SpentOut = _t, #"Budget Nov" = _t, SpentNov = _t, #"Budget Dez" = _t, SpentDez = _t]), Unpivoted = Table.UnpivotOtherColumns(Source, {"Project"}, "Attribute", "Value"), ReplacedValue = Table.ReplaceValue(Unpivoted," ","",Replacer.ReplaceText,{"Attribute"}), SplitColumn = Table.SplitColumn(ReplacedValue, "Attribute", Splitter.SplitTextByCharacterTransition({"a".."z"}, {"A".."Z"}), {"Attribute", "Month Name"}), Pivoted = Table.Pivot(SplitColumn, List.Distinct(SplitColumn[Attribute]), "Attribute", "Value"), Ad_Date = Table.AddColumn(Pivoted, "Date", each Date.FromText("2025" & [Month Name] & "01", [Format="yyyyMMMdd", Culture="pt-PT"]), type date) in Ad_Date- WilliamAzevedoAdvocate II
Hi!
I don't know what I did wrong, but the result was the unpivoted columns returning to pivoted.
- dufoq3Community Champion
Hi WilliamAzevedo, if you don't know how to use my query - read note below my post.
- v-priyankataCommunity Support
May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.
Thank you.
- WilliamAzevedoAdvocate II
Hello everyone and I'm sorry it took me so long to reply. I was out of the office those days and because of that I had neither worked on the task I asked help for, nor accessed the community.
So, today I had to make a change a in my model (SharePoint folder with .txt files as source instead of .xlsx files) and for that reason I think I have to ask: do the methods still apply? While this message reaches you, I will try it anyway.
- v-sathmakuriCommunity Support
Hi WilliamAzevedo ,
May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.
Thank you.