Forum Discussion

bcintron's avatar
bcintron
New Member
2 years ago
Solved

Add all rows from two columns to create one column

I've been looking all over for a solution and have not found anything of use, even copilot/ai cannot help. Is it not possible to have two columns with data and add all the rows together from those two columns into a new third column? In the example below, I have column1 and column2, and would like see the result of column3. Any help would be greatly appreciated.

Column1Column2Column3
Data1Data4Data1
Data1Data4Data1
Data2Data4Data2
Data2Data4Data2
Data3Data5Data3
Data3Data5Data3
  Data4
  Data4
  Data4
  Data4
  Data5
  Data5
  • After messaging, this was the solution that we found to work.

    let
        Source = SharePoint.Tables("my sharepoint url", [Implementation=null, ApiVersion=15]),
        #"sharepoint key" = Source{[Id="sharepoint key"]}[Items],
        #"Renamed Columns" = Table.RenameColumns(#"key",{{"ID", "ID.1"}}),
        #"Added Index" = Table.AddIndexColumn(#"Renamed Columns", "Index", 1, 1, Int64.Type),
        #"Added Custom" = Table.Combine({Table.FromColumns({#"Renamed Columns"[Column1]}, {"Column3"}), Table.FromColumns({#"Renamed Columns"[Column2]}, {"Column3"})})
    in
        #"Added Custom"

    "Renamed Columns" was for my specific scenario, but everything else worked and was essential. I replaced Column1 and Column2 with the names of the specific columns I wanted to join together, and Column3 can be named anything else. This achieved what I was looking for, but does not return the initial Column1 and Column2. 

    Thank you jgeddes !

9 Replies

  • Hi! In Power Query you can create a custome column and have column 1 added to column 2 to create column 3. You can also create a calculated column to do this in Power BI desktop where Column 3 = 'YourTable'[Column1]+'YourTable'[Column2], however, you will get better performance if you add it in Power Query. Also, I would only suggest adding a new column if you need to be able to filter by the value in that column. If you do not need it to filter, you should use a measure instead. For a measure I would make 3.... 

    Measure1 = SUM('YourTable'[Column1])

    Measure2 = SUM('YourTable'[Column2])

    Measure3 = [Measure1]+[Measure2]

    • bcintron's avatar
      bcintron
      New Member

      Hi audreygerred , thank you for your response.

      When attemping to use:

      Column 3 = 'YourTable'[Column1]+'YourTable'[Column2]

      I believe it's trying to add the values as numeric, although the values are strings. I am getting the error "Cannot convert value 'Data1' of type Text to type Number."

      In the column tools, I've changed all three column's data types and format to Text and still get the same error.

      Thanks,

      • audreygerred's avatar
        audreygerred
        Icon for Super User rankSuper User

        If you want to be able to sum and other aggregations you will need to have your values formatted as numeric fields.

  • Here is a different solution that will work in Power Query.
    Starting with the table 

    and ending up with the table 

    Paste the following code into the advanced editor of a blank query so you can review the steps required.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckksSTRU0gHTJkqxOrhEjIgQMYaKmGITiQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
        #"Added Custom" = Table.Combine({Table.FromColumns({Source[Column1]}, {"Column3"}), Table.FromColumns({Source[Column2]}, {"Column3"})}),
        #"Added Index1" = Table.AddIndexColumn(#"Added Custom", "Index", 1, 1, Int64.Type),
        #"Merged Queries" = Table.NestedJoin(#"Added Index1", {"Index"}, #"Added Index", {"Index"}, "Merged Tables", JoinKind.LeftOuter),
        #"Expanded Added Index1" = Table.ExpandTableColumn(#"Merged Queries", "Merged Tables", {"Column1", "Column2"}, {"Column1", "Column2"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Added Index1",{"Index"}),
        #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Column1", "Column2", "Column3"})
    in
        #"Reordered Columns"
    • bcintron's avatar
      bcintron
      New Member

      When I paste the code it works as far as showing that specific data, but how can I use this for my own table? Where can I specify the location of my table so that the code references my columns?

      Thank you,

      • jgeddes's avatar
        jgeddes
        Icon for Super User rankSuper User

        In theory you would replace my source step with the source step from your query. 
        However you will very likely run into issues with column names in your query not matching the example column names. 

        Please send me a direct message and I can go into more detail.

  • foodd's avatar
    foodd
    Icon for Community Champion rankCommunity Champion

    bcintron , jgeddes's  solution works well, and you have the option to

    change the data source by using the Advanced Editor or by accessing the

    Power Query Editor. To do this, select the query you are working on, and

    from the ribbon, choose Home > New Source. Then, select the source type

    and browse to the file (for example, if this were an Excel file).

     

    For a fun example, the following method also involves unpivoting columns,

    sorting, and removing nulls, but assumes that the user only wants a

    single column to remain. I used to utilize this for early morning

    transaction activity reports where the columns were

    always strings, and I required a quick single-column report first thing in

    the morning.      Files attached below signature.

     

    let
        Source = Excel.Workbook(File.Contents("C:\wip\happyhappy.xlsx"), null, true),
        Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
        #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"Yum", type text}, {"YumYum", type text}}),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {}, "Attribute", "Value"),
        #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Value", "HappyHappy"}}),
        #"Filtered Rows" = Table.SelectRows(#"Renamed Columns", each ([HappyHappy] <> ""))
    in
        #"Filtered Rows"

     

     

     

    If your requirement is solved, please make THIS ANSWER a SOLUTION ✔️ and help other users find the solution quickly. Please hit the LIKE 👍 button if this comment helps you.