Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago

Dynamic column name based on a column value coming from a different unrelated table

Hi,

 

I have a requirement to get the column name (for Table A) that can change dynamically based on column value that is coming from a different table (Table B) which is not related to this table (Table A).

 

Note: Table A and Table B are not related with each other and there is no common identifier between then except the column names from Table A and the "Column Name" column value from Table B.

 

Please let me know how to achieve this? 

 

Here is the mock up and end result.

Table A

Column1Column2Column3

Column4

A001ABC1000ttttttttt
A002XYZ3000ggggggggg
A003BBB3000ccccccccc
A004KKK6000bbbbbbbbbb
A005LLL5000ssssssssss
A006PPP8000rrrrrrrr
A007MMM10000fffffffffff

 

Table B

Column NameExtract Date
Column122/05/2024
Column24/06/2024
Column330/04/2024
Column4

6/06/2024

 

Expected End result

Column1 - 22/05/2024Column2 - 4/06/2024Column3 - 30/04/2024Column4 - 6/06/2024
A001ABC1000ttttttttt
A002XYZ3000ggggggggg
A003BBB3000ccccccccc
A004KKK6000bbbbbbbbbb
A005LLL5000ssssssssss
A006PPP8000rrrrrrrr
A007MMM10000fffffffffff

 

 

18 Replies

  • Hi Anonymous - Associating the columns from Table A with the values from Table B, you can do in power Query as below:

    Please follow the steps:

    On table B:

    Do the pivot :and I named it as TableB_Pivoted

     

    On Table A, you can perform Merge query by joining the Table A with TableB_Pivoted use left outer join

     

    Expand all 4 columns , you can see the below image:

     

     

    Click on Add Column -> Custom Column.
    Name the column Column1 - Extract Date and use the following formula

    TableB_Pivoted[Column1]{0} 

    TableB_Pivoted[Column2]{0}

    TableB_Pivoted[Column3]{0}

    TableB_Pivoted[Column4]{0}

     

    Repeat this for all columns in Table A (Column2, Column3, Column4).

     

    Perform last few steps:

    Do a pivot on Table A with Selecting Column 1 

     

    do it for remaining 3 columns too

     

     

    I am not able to share pbix file here, not sure how to share it here.

     

    i will add the complete advanced query editor

     

     

    Please follow the steps you can achieve the same.

     

     

    Did I answer your question? Mark my post as a solution! This will help others on the forum!
    Appreciate your Kudos!!

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks rajendraongole1 

      I have tried to follow the steps you provided, however, I'm getting errors when applying the step - adding columns "Column1 - Extract Date". See below

       

      It would be great if you could attach pbix so I can check where I am making mistake or if you could explian please?

      Thanks

      Brij

       

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

        Hi Anonymous - I am not able to attached my pbix file here,option not available.

         

        you almost reached towards the solution. 

         

        after merge queries, during the expanding option you can remove the original prefix and continoue to add the new custom columns, it works

         

         

         

        Check and confirm

         

        Did I answer your question? Mark my post as a solution! This will help others on the forum!
        Appreciate your Kudos!!

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

    Please try:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RY85DoAwDAT/4prC3LSEMomUkkNpQIIe+L9YCE6mWEujabws1DPnlFGvBmzOzDi3QD77igJynGZsGYpDkKKEVEqlYhOkqCC11tgmFGtEkhrWGIOtQ3JFJGlgnXPYLiTnjwQtnLX2f+Yt9gR5/wA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", Int64.Type}, {"Column4", type text}}),
        Custom1 = Table.FromList(Table.ColumnNames(#"Changed Type")),
        #"Merged Queries" = Table.NestedJoin(Custom1, {"Column1"}, TableB, {"Column Name"}, "TableB", JoinKind.LeftOuter),
        #"Expanded TableB" = Table.ExpandTableColumn(#"Merged Queries", "TableB", {"Extract Date"}, {"Extract Date"}),
        #"Added Custom" = Table.AddColumn(#"Expanded TableB", "Names", each [Column1] & " - " & [Extract Date]),
        Custom2 = Table.Group(#"Changed Type",{"Column1"},{{"Data",each _}}),
        #"Expanded Data" = Table.ExpandTableColumn(Custom2, "Data", {"Column1", "Column2", "Column3", "Column4"}, #"Added Custom"[Names]),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Data",{"Column1"})
    in
        #"Removed Columns"

    Best Regards,
    Gao

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly --  How to provide sample data in the Power BI Forum -- China Power BI User Group

  • Hi,

    In TableB, create this custom culomn formula

    The M code in Table A will be

    let
    Source = Excel.CurrentWorkbook(){[Name="TableA"]}[Content],
    Custom1 = Table.RenameColumns(Source,List.Zip( { TableB[Column Name], TableB[Merged]}),MissingField.Ignore )
    in
    Custom1

    Hope this helps.

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Ashish_Mathur 

      This is working perfectly. However, every time we refresh the data (that changes the extract date for a particular column). It breaks the visual that these columns are listed such as on a table visual! 

       

      Any thoughts why is the case?

       

      Any help is appreciated.

      Thank you

      Brij

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

        You are welcome.  I obviously will not be able to understand any reason from your sentence.  Share a lot more details.