Forum Discussion

WilliamAzevedo's avatar
WilliamAzevedo
Advocate II
1 year ago
Solved

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:

ProjectBudget JanSpent JanBudget FevSpent FevBudget MarSpent MarBudget AbrSpentAbrBudget MaiSpentMaiBudget JunSpentJunBudget JulSpentJulBudget AgoSpentAgoBudget SetSpentSetBudget OutSpentOutBudget NovSpentNovBudget DezSpentDez
112.543,322.526,481.115,662.526,481.115,662.526,481.115,660,001.115,660,002.231,330,002.231,330,002.231,330,002.231,330,002.789,160,002.789,160,003.346,990,00
237.702,980,000,002.060,030,00271,760,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,00
30,000,003.188,931.294,623.188,911.819,483.188,910,003.188,910,006.377,820,006.377,820,006.377,820,006.377,820,007.972,280,007.972,280,009.566,730,00
40,000,002.212,991.095,882.212,98816,262.212,980,002.212,980,004.425,950,004.425,950,004.425,950,004.425,950,005.532,440,005.532,440,006.638,930,00
55.713.616,845.199,030,0022.813,240,0024.664,470,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,00

 

I need:

ProjectMonthBudgetSpent
1Jan12543,32526,48
1Fev1156,662526,48
5Nov0,000,00
5Dez0,000,00

 

And so on. How to do that in Power Query?

 

Thank you very much in advance!

  • SundarRaj's avatar
    SundarRaj
    1 year ago

    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

  • 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

     

     

     

     

     

     

     

  • 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

     

    • WilliamAzevedo's avatar
      WilliamAzevedo
      Advocate 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.

      • SundarRaj's avatar
        SundarRaj
        Super 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

  • dufoq3's avatar
    dufoq3
    Community 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
    • WilliamAzevedo's avatar
      WilliamAzevedo
      Advocate II

      Hi!

       

      I don't know what I did wrong, but the result was the unpivoted columns returning to pivoted.

  • v-priyankata's avatar
    v-priyankata
    Community 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.

  • 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-sathmakuri's avatar
      v-sathmakuri
      Community 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.