Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Changing Dates on Columns

Hi!

I've got a query to a file folder which contains Excel files with projected forecasts. Each file contains dates in columns and the values in rows. The problem is that the dates in the columns on each file are different, as it is a different forecast period. when new files are added, the same columns will contain different dates, as it is a new forecast range. So the furthest left column on file 1 might be with date 43920, but on file two this will be 43948. If they were all the same dates, I would just promote the headers and then get rid of the other dates, but because of the above issue, I cannot do that.

 

 

Does anybody know how I could either merge the files so that each new file was added to the right of the previous files instead of appending below, or a different way around this problem?

 

Many thanks,

 

Paddy

 

  • Hi Anonymous ,

     

    You can edit the Transform Sample File to handle it, like:

     

    let
        Source = Excel.Workbook(Parameter1, null, true),
        Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
        #"Filled Down" = Table.FillDown(#"Promoted Headers",{"Quantity"}),
        #"Filtered Rows" = Table.SelectRows(#"Filled Down", each not Text.Contains([Quantity], "Total")),
        Row = Table.UnpivotOtherColumns(Table.Skip(Table.DemoteHeaders(Table.Skip(#"Filtered Rows",1)), 1), {"Column1", "Column2"}, "Attribute", "Value"),
        Header = Table.UnpivotOtherColumns(Table.Skip(Table.DemoteHeaders(Table.FirstN(#"Filtered Rows",1)),1), {"Column1", "Column2"}, "Attribute", "Value"),
        Merge = Table.NestedJoin(
                Table.UnpivotOtherColumns(Table.Skip(Table.DemoteHeaders(Table.Skip(#"Filtered Rows",1)), 1), {"Column1", "Column2"}, "Attribute", "Value"), 
                "Attribute", 
                Table.UnpivotOtherColumns(Table.Skip(Table.DemoteHeaders(Table.FirstN(#"Filtered Rows",1)),1), {"Column1", "Column2"}, "Attribute", "Value"), 
                "Attribute", 
                "Header", 
                JoinKind.Inner
                ),
        #"Expanded Header" = Table.ExpandTableColumn(Merge, "Header", {"Value"}, {"Value.1"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Header",{"Attribute"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Value.1", "Date"}, {"Column1", "Customer"}, {"Column2", "Name"}}),
        #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Customer", "Name", "Date", "Value"}),
        #"Changed Type" = Table.TransformColumnTypes(#"Reordered Columns",{{"Customer", type text}, {"Name", type text}, {"Date", type date}, {"Value", Int64.Type}})
    in
        #"Changed Type"

     

    Check the steps Row and Header, the step Merge are skipping them and using only the code, you can delete the Row and Header once it's ok.

     

5 Replies

  • dax's avatar
    dax
    Community Support

    Hi Anonymous , 

    It seems that you want to combine two excel together, right? If so, and if you want to make the similar columns from two excels into the same column, you need to make sure the column names, column types , column amounts  be the same, then when you append table, it will show in the same columns. By the way, you also could post your sample and your expected output, then I will test this in my environment.

    Please do mask sensitive data before uploading.

    Thanks for your understanding and support.
    Best Regards,
    Zoe Zhi

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi dax 

      Thanks for responding. 

       

      I would send the data but I can't see a way to attach Excel files, sorry! If there is a way to send Excel files I will glady do it.

      But basically, the column names are never the same because each new file is always the latest forecast, and so once a date is in the past, it will no longer be a column name like it was on a previous file.

       

      So at the start of June, you would get a table like the below, with the columns starting from June and going into the future. This is forecasted order quantity by product (name) and customer.

       

      Quantity Date     
      CustomerName01/06/202008/06/202015/06/202022/06/202029/06/202006/07/2020
      C1P1138181209237265265
       P21,3251,7862,0602,3342,6072,607
       P38481,1281,3011,4741,6461,646
       P41,1761,5861,8292,0722,3142,314
       P59631,2711,4651,6601,8541,854
       P6303422487552617617
       P7144185213242270270
       P8000000
      C1 Total4,8976,5597,5648,5719,5739,573
      C2P1404040404040
       P2303030303030
       P3404040404040
       P4404040404040
       P5404040404040
       P6404040404040
       P7555555555555
      C2 Total285285285285285285

       

      But in an earlier file from the end of March, the date columns will start at the end of March and continue from that point.

       

      Quantity Date      
      CustomerName30/03/202006/04/202013/04/202020/04/202027/04/202004/05/202011/05/2020
      C1P1300300300300300300300
       P2332332332332249249249
       P316616624933216683249
       P424924924924924983249
       P524924924924924983249
       P683166838383144166
       P78787 871748787
       P881148747414881148
      C1 Total1,5471,6971,5361,7061,6181,1101,697
      C2P140404040404040
       P247474747474747
       P340404040404040
       P440404040404040
       P526262626262626
       P626262626262626
       P726262626262626
      C2 Total245245245245245245245

       

       

      If all the dates were the same, I would promote the headers so all the dates were columns, and then could unpivot all the value columns and have a 'dates' column and 'values' column, but because 'column 3' will contain different dates due to the issue described above, this will not work.

       

      I could manually edit each file to insert a number of blank columns so that the dates were the same, but was hoping there would be a way to solve the problem in Power Query.

       

      Hope this makes sense.

       

      Thanks!

       

       

      • camargos88's avatar
        camargos88
        Community Champion

        Hi Anonymous ,

         

        You can edit the Transform Sample File to handle it, like:

         

        let
            Source = Excel.Workbook(Parameter1, null, true),
            Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
            #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
            #"Filled Down" = Table.FillDown(#"Promoted Headers",{"Quantity"}),
            #"Filtered Rows" = Table.SelectRows(#"Filled Down", each not Text.Contains([Quantity], "Total")),
            Row = Table.UnpivotOtherColumns(Table.Skip(Table.DemoteHeaders(Table.Skip(#"Filtered Rows",1)), 1), {"Column1", "Column2"}, "Attribute", "Value"),
            Header = Table.UnpivotOtherColumns(Table.Skip(Table.DemoteHeaders(Table.FirstN(#"Filtered Rows",1)),1), {"Column1", "Column2"}, "Attribute", "Value"),
            Merge = Table.NestedJoin(
                    Table.UnpivotOtherColumns(Table.Skip(Table.DemoteHeaders(Table.Skip(#"Filtered Rows",1)), 1), {"Column1", "Column2"}, "Attribute", "Value"), 
                    "Attribute", 
                    Table.UnpivotOtherColumns(Table.Skip(Table.DemoteHeaders(Table.FirstN(#"Filtered Rows",1)),1), {"Column1", "Column2"}, "Attribute", "Value"), 
                    "Attribute", 
                    "Header", 
                    JoinKind.Inner
                    ),
            #"Expanded Header" = Table.ExpandTableColumn(Merge, "Header", {"Value"}, {"Value.1"}),
            #"Removed Columns" = Table.RemoveColumns(#"Expanded Header",{"Attribute"}),
            #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Value.1", "Date"}, {"Column1", "Customer"}, {"Column2", "Name"}}),
            #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Customer", "Name", "Date", "Value"}),
            #"Changed Type" = Table.TransformColumnTypes(#"Reordered Columns",{{"Customer", type text}, {"Name", type text}, {"Date", type date}, {"Value", Int64.Type}})
        in
            #"Changed Type"

         

        Check the steps Row and Header, the step Merge are skipping them and using only the code, you can delete the Row and Header once it's ok.