0

I need to concatenate columns on a pipe | from a table of column names in such a way that keeps the original columns

I can do this with merge, but I lose the original columns

How to do this without losing the columns being contaminated?

Thanks

enter image description here


#"Trimmed Text" = Table.TransformColumns(#"Sorted Rows",{{"Id", Text.Trim, type text}}),
    newMergedHeader = Table.ColumnNames(mergeHeaders){0},
    
    #"Merged Columns" = 
        if newMergedHeader <> null then 
          Table.RenameColumns(
          Table.CombineColumns(#"Trimmed Text",Table.Column(mergeHeaders,newMergedHeader),each Combiner.CombineTextByDelimiter("|", QuoteStyle.None)(List.Difference(List.RemoveNulls(_), {""})),"Merged"),{{"Merged",newMergedHeader}})
        else #"Trimmed Text

Input

| Other Colums | Label              | Segment              | Type   | Other Colums |
|--------------|--------------------|----------------------|--------|--------------|
| stuff        | Alexis Bhat        |                      |        | stuff        |
| stuff        | Alphonse Wilkinson |                      | Person | stuff        |
| stuff        | Bobbi Carter       | Network Blue         |        | stuff        |
| stuff        |                    | Community Healers    | Person | stuff        |
| stuff        | Dannie Cano        | Community Healers    | Person | stuff        |
| stuff        | Darius Neel        | Community Healers    | Person | stuff        |
| stuff        |                    |                      |        | stuff        |
| stuff        | Delores Rana       | Community Healers    | Person | stuff        |
| stuff        | Dionne Merino      | Education Commission | Person | stuff        |

Output

| Other Colums | Label              | Segment              | Type   | Other Colums | Blue                                      |
|--------------|--------------------|----------------------|--------|--------------|-------------------------------------------|
| stuff        | Alexis Bhat        |                      |        | stuff        | Alexis Bhat                               |
| stuff        | Alphonse Wilkinson |                      | Person | stuff        | Alphonse Wilkinson|Person                 |
| stuff        | Bobbi Carter       | Network Blue         |        | stuff        | Bobbi Carter|Network Blue                 |
| stuff        |                    | Community Healers    | Person | stuff        | Community Healers|Person                  |
| stuff        | Dannie Cano        | Community Healers    | Person | stuff        | Community Healers                         |
| stuff        | Darius Neel        | Community Healers    | Person | stuff        | Person                                    |
| stuff        |                    |                      |        | stuff        |                                           |
| stuff        | Delores Rana       | Community Healers    | Person | stuff        | Delores Rana|Community Healers|Person     |
| stuff        | Dionne Merino      | Education Commission | Person | stuff        | Dionne Merino|Education Commission|Person |

3
  • Please post sample input and expected output data as text. Commented Aug 29, 2022 at 20:25
  • 1
    I have no idea yet what you want (Yet) but why not duplicate the columns and merge the duplicates, so they originals are left behind when the duplicates disappear? Commented Aug 29, 2022 at 20:32
  • yeah, that seems like it would work, but it leaves me in more-or-less the same position. How to duplicate columns based on my mergedHeaders table Commented Aug 29, 2022 at 22:29

1 Answer 1

2

If I understand you correctly, you can just add a column that concatenates the columns you wish to "merge" using the pipe | as the delimiter.

The columns to merge, and the merged column header will come from a different table in the workbook.

let
    Source = Excel.CurrentWorkbook(){[Name="Input"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"Other Colums", type text}, {"Label", type text}, {"Segment", type text}, {"Type", type text}, {"Other Colums 2", type text}}),

//Replace blank with nulls to more easily ignore when concatenating
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type","",null,Replacer.ReplaceValue,{"Other Colums", "Label", "Segment", "Type", "Other Colums 2"}),

//Get headers to merge and merged header name from table
    Source2 = Excel.CurrentWorkbook(){[Name="mergeHeaders"]}[Content],
    mergeHeaders = List.RemoveFirstN(Source2[New Concatenation Header Name],1),
    MergedHeaderName = Source2[New Concatenation Header Name]{0},

//add the merged column
    merge = Table.AddColumn(#"Replaced Value", MergedHeaderName, (r)=> Text.Combine(List.RemoveNulls(
            Record.ToList(Record.SelectFields(r,mergeHeaders))           
            ),"|"), type text)

in
    merge

Input
enter image description here

mergeHeaders
enter image description here

Results
enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.