Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Merge two rows based on conditions using Power Query

Hi,

I'm hoping that somebody on here can help me as I've been unable to find a way to do this in Power Query and don't know enough about M Code to be able to obtain my desired solution.

I've attached a sample picture showing my starting data, in the left-hand table, as imported into Power Query and the desired result in the table in the right-hand table.

There are some rows that should be transferred directly (example being the row dated 23/07/2018) but some rows need to be combined (example being 20/07/2018 with the row below). I've tried to show this with the arrows.

The best explanation that I can give is that the rows that need to be combined have, in the first row that needs to be combined, a blank cell in both [Column 5] and [Column 6] and a blank on the second row in [Column 2]. Rows that should be transposed directly have data in [Column 2] and [Column 5] and OR [Column 6] exclusively - not both.

I hope that I've been clear enough and that one of you guys can help me with this frustrating puzzle. If it can be done using the standard editor I'd be interested to see how but I'd like to see the M Code so that I can learn how to achieve this too!

 

 

 

 

  • Hi Anonymous ,
    please check the enclosed file for the solution as well:

    // Table1
    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type datetime}, {"Column2", type text}, {"Column3", type text}, {"Column4", type datetime}, {"Column5", type number}, {"Column6", type number}, {"Column7", type number}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "EntryIndex", each if [Column5] = null and [Column6] = null then null else [Index]),
        #"Filled Up" = Table.FillUp(#"Added Custom",{"EntryIndex"}),
        #"Replaced Value1" = Table.ReplaceValue(#"Filled Up",null, each [Index],Replacer.ReplaceValue,{"EntryIndex"}),
        #"Filled Down" = Table.FillDown(#"Replaced Value1",{"Column1", "Column2"}),
        #"Grouped Rows" = Table.Group(#"Filled Down", {"EntryIndex"}, {{"Column1", each List.Min([Column1]), type nullable datetime}, {"Column2", each List.Min([Column2]), type nullable text}, {"Column5", each List.Sum([Column5]), type nullable number}, {"Column6", each List.Sum([Column6]), type nullable number}, {"Column3", each Text.Combine(_[Column3], " ")}}),
        #"Replaced Value" = Table.ReplaceValue(#"Grouped Rows",null,0,Replacer.ReplaceValue,{"Column5", "Column6"}),
        #"Inserted Addition" = Table.AddColumn(#"Replaced Value", "Value", each [Column6] - [Column5], type number),
        Custom1 = fnRunningTotal( #"Inserted Addition", "EntryIndex", "Value")
    in
        Custom1
    



4 Replies

  • ImkeF's avatar
    ImkeF
    Community Champion

    Hi Anonymous ,
    please check the enclosed file for the solution as well:

    // Table1
    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type datetime}, {"Column2", type text}, {"Column3", type text}, {"Column4", type datetime}, {"Column5", type number}, {"Column6", type number}, {"Column7", type number}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "EntryIndex", each if [Column5] = null and [Column6] = null then null else [Index]),
        #"Filled Up" = Table.FillUp(#"Added Custom",{"EntryIndex"}),
        #"Replaced Value1" = Table.ReplaceValue(#"Filled Up",null, each [Index],Replacer.ReplaceValue,{"EntryIndex"}),
        #"Filled Down" = Table.FillDown(#"Replaced Value1",{"Column1", "Column2"}),
        #"Grouped Rows" = Table.Group(#"Filled Down", {"EntryIndex"}, {{"Column1", each List.Min([Column1]), type nullable datetime}, {"Column2", each List.Min([Column2]), type nullable text}, {"Column5", each List.Sum([Column5]), type nullable number}, {"Column6", each List.Sum([Column6]), type nullable number}, {"Column3", each Text.Combine(_[Column3], " ")}}),
        #"Replaced Value" = Table.ReplaceValue(#"Grouped Rows",null,0,Replacer.ReplaceValue,{"Column5", "Column6"}),
        #"Inserted Addition" = Table.AddColumn(#"Replaced Value", "Value", each [Column6] - [Column5], type number),
        Custom1 = fnRunningTotal( #"Inserted Addition", "EntryIndex", "Value")
    in
        Custom1
    



  • ImkeF's avatar
    ImkeF
    Community Champion

    Hi Anonymous ,

    you picture gives a good explanation, but it would be best if you would have included sample data as well.
    So without I can just give you instructions about the steps:

    1. Add an index column
    2. Check Column1 and Column2 and replace blanks by null 
    3. Add a custom column: if [Column2] = null then null else [Index]
    4. Check Column1 and fill down values
    5. Multiply Column5 by -1
    6. Group on the column you created in step 3
    7. Use the SUM-Operation on the amount fields, MAX on Column1 and Column2 and add an ALL operation for the concatenation of the Description field
    8. In the ALL operation replace "each _" by "each Text.Combine(_[Column3], " ") - that will combine the fields of all rows within the group.
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi ImkeF ,

       

      Firstly, a huge thank you for your reply; I've added a sample table in a new thread (even adding this isn't straight-forward! And thanks for your guidance in how to do this too!), taken by importing a pdf into Power Query and then loading it directly as a table into Excel.  The table is the export to Excel, loaded here directly.  I'm going to follow the steps that you've described above to see if I can follow your explanation but I thought I would also load the sample table as I suspect I will do something incorrectly!  Hopefully, with the "raw" data as taken from the pdf and loaded as a table, you might be able to walk me through the steps also so that I can actually see what I will have done incorrectly!

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi ImkeF,

        I believe I've managed to find the correct way to add the Sample Table; hope this might help you to help me!! Again huge thank you for your help!

         

         

        Column1Column2Column3Column4Column5Column6Column7
        19/07/2018TFRBalance brought forward
        111111 22222222
           841.38
          INTERNAL TRANSFER  2001041.38
        20/07/2018VISKANOO   1041.38
          FOREIGN EXCHA 205.39 835.99
        23/07/2018DDAVRO ENERGY LTD 79 756.99
        25/07/2018ATMCASH HANCO01/07/2025   
          MITIE @10:33 60 696.99
        26/07/2018ATMCASH BANK JUL26    
          ASDA RY@08:53 60 636.99
        27/07/2018CRSALARY  4048.76 
         CRSALARY  994.295680.04
        28/07/2018ATMCASH BANK JUL28    
          ASDA RY@09:02 200 5480.04
        30/07/2018SOFRED BLOGGS    
          FRED 30 5450.04
        01/08/2018DDINSURANCE C/L 27  
         DDINSURANCE C/L 10.15  
         DDTV LICENCE MBP 12.07  
         DDCREDIT CARD 2180.04  
         DDHOUSE INS 45.38  
         SOBROWN RJ&F    
          SAVINGS 1050  
         SOBROWN R&F    
          77777777 10  
         SOBROWN R&F    
          RJ BROWN 20  
         SOFRED R BROWN    
          999999999 50  
         SOVOYAGER EXPLORER    
          FRED BLOGGS 8.75 2036.65
        02/08/2018DDMOBILE LTD 20.68 2015.97
        03/08/2018 Balance carried forward   2015.97