Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
THEG72
Helper V
Helper V

Add New Column in M based on Parent

I have a Table of Accounts which has an Account ID and Parent ID. I want to ADD a new Column in the Query Editor for the Parent Name.

 

What code should i use to add the new Parent Name field based on image below? I have the ID's  for both Parent and Acccount (Child). The Account ID will indicate what the Parent's name will be.

New Column for Parent Name based on Account ID shown in Parent ID ColumnNew Column for Parent Name based on Account ID shown in Parent ID Column

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

You can simply use your current table as a lookuptable and use the Parent ID. Your formula would look like so:

 

Table.NestedJoin(#"Removed Other Columns",{"ParentAccountID"},#"Removed Other Columns",{"AccountID"},"Source",JoinKind.LeftOuter)

 

See this little screencast how it works: https://www.youtube.com/watch?v=looCm3cbINw

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

2 REPLIES 2
ImkeF
Super User
Super User

You can simply use your current table as a lookuptable and use the Parent ID. Your formula would look like so:

 

Table.NestedJoin(#"Removed Other Columns",{"ParentAccountID"},#"Removed Other Columns",{"AccountID"},"Source",JoinKind.LeftOuter)

 

See this little screencast how it works: https://www.youtube.com/watch?v=looCm3cbINw

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

@ImkeF

thanks again, the trick to change back to source is truly amazing with M....thanks for help again and the video made it easy!

 

Grouping Parent HeadersGrouping Parent Headers

Here is the final code, i changed the source to my Table Select Columns so i didnt have to reselect the columnns to display

let
    Source = Accounts,
    TableSelect = Table.SelectColumns(Source,{"AccountID", "ParentAccountID", "Account Name", "AccountTypeID", "AccountLevel", "IsTotal"}),
    FitlerTable = Table.SelectRows(TableSelect, each ([AccountTypeID] = "H")),
    #"Merged Queries" = Table.NestedJoin(TableSelect,{"ParentAccountID"},FitlerTable,{"AccountID"},"FitlerTable",JoinKind.LeftOuter),
    #"Expanded FitlerTable" = Table.ExpandTableColumn(#"Merged Queries", "FitlerTable", {"Account Name"}, {"FitlerTable.Account Name"})
in
    #"Expanded FitlerTable"

Hopefully, others will find this useful to group accounts together.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.