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
Zyg_D
Continued Contributor
Continued Contributor

Get a value from a related table

This is my data:

PBI_MAX_related.JPG

I want to create the green column - a calculated column. It represents the name from max value of IDs AA and BB which both belong to the parent X. The relation is created between tables on the columns "ID". I tried this, but it failed:

Max_value = maxx(Table1[Name],[Parent]=[Parent])
4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Parent", type text}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"ID"}, Table1, {"ID"}, "Table1", JoinKind.LeftOuter),
    #"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"Value"}, {"Value"}),
    #"Grouped Rows" = Table.Group(#"Expanded Table1", {"Parent"}, {{"Max", each List.Max([Value]), type number}}),
    Joined = Table.Join(#"Expanded Table1", "Parent", #"Grouped Rows", "Parent"),
    #"Removed Columns" = Table.RemoveColumns(Joined,{"Value"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Columns"),
    #"Merged Queries1" = Table.NestedJoin(#"Removed Duplicates", {"Max"}, Table1, {"Value"}, "Table1", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries1", "Table1", {"Name"}, {"Name"}),
    #"Removed Columns1" = Table.RemoveColumns(#"Expanded Table2",{"Max"})
in
    #"Removed Columns1"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-alq-msft
Community Support
Community Support

Hi, @Zyg_D 

 

You may create a calculated column as below.

 

Result = 
var _parent = [Parent]
var _maxvalue = 
CALCULATE(
    MAX(Table1[Value]),
    ALLEXCEPT(Table2,Table2[Parent])
)
return
CONCATENATEX(
    CALCULATETABLE(
        FILTER(
            ALL(Table1),
            Table1[Value] = _maxvalue&&
            RELATED(Table2[Parent]) = _parent
        )
    ),
    [Name],","
)

 

 

Result:

b2.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

parry2k
Super User
Super User

@Zyg_D try this DAX expression by adding a column

 

Column = 
VAR __ids = CALCULATETABLE ( VALUES ( Table2[id] ), ALLEXCEPT ( Table2, Table2[Parent] ) )
RETURN
CALCULATE ( MAX ( Table1[Name] ), ALL ( Table1 ), TREATAS( __ids, Table1[Id] ) )

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Zyg_D
Continued Contributor
Continued Contributor


@parry2k wrote:

@Zyg_D try this DAX expression by adding a column

 

 

Column = 
VAR __ids = CALCULATETABLE ( VALUES ( Table2[id] ), ALLEXCEPT ( Table2, Table2[Parent] ) )
RETURN
CALCULATE ( MAX ( Table1[Name] ), ALL ( Table1 ), TREATAS( __ids, Table1[Id] ) )

 


I was too quick to accept this as solution. In your fomula MAX looks at [Name] and returns it. But I want it to look at [Value] while returning [Name].

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.