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
taimaizumi
Helper I
Helper I

Relateadtable used in calculate function at calculate column based on inactive relationship

Hi , team

I'm at lost when using relatedtable in calculate function at calculate column.

The detail is as follows.

<Data>
Sales

taimaizumi_0-1682318076371.png

-->Sales can oocur at different location with same product

Product

taimaizumi_1-1682318096111.png

-->Product has different location for sales


<Model>

taimaizumi_2-1682318175723.png

-->Product and sales is related with Key1(on product side) and Key(on sales side) by default
and Key2(on product side) is used as inactive relationship with Key(on sales side)

taimaizumi_3-1682318331886.png


<Requirement>
Compute max sales quantity for each location

<My solution>
To compute max sales for location 1, I wrote below code using default relationship

 

MaxQuantityKey1 = calculate(maxx('Sales','Sales'[Quantity]))

taimaizumi_4-1682318446041.png

 

And to compute max sales for location 2, I wrote below code using Userelationship so that I can use inactive relationship

 

MaxQuantityKey2 = CALCULATE(maxx(relatedtable('Sales'),'Sales'[Quantity]),USERELATIONSHIP('Product'[Key2],Sales[Key]))

 

taimaizumi_5-1682318535840.png

<My question>
Where is filter context generated by row context based on active relationship?
-->
I think row context is transformed into filter context because calculate is invoked and this filter context is based on active relationship. And then new filter context is generated by relatedtable based on inactive relationship.
Finally I think these two filter context don't have intersect resulting in blank.
But result is like this , but relationship 

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @taimaizumi ,

For the table "Product", you can do some processing in Power Query Editor as shown below, so that it will be easier to make some calculation later.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjQwMDBU0lECYSMgBvN1wUJQppFSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Location1 = _t, Location2 = _t, Key1 = _t, Key2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Location1", Int64.Type}, {"Location2", Int64.Type}, {"Key1", type text}, {"Key2", type text}}),
    #"Unpivoted Only Selected Columns1" = Table.Unpivot(#"Changed Type", {"Location2", "Location1"}, "Attribute.1", "Location"),
    #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Unpivoted Only Selected Columns1", {"Key1", "Key2"}, "Attribute", "Key"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Only Selected Columns",{"Attribute.1", "Attribute"})
in
    #"Removed Columns"

vyiruanmsft_0-1682488062182.png

If the above one can't help you, could you please provide more raw data and the specific examples to explain your expected result( Is it 400)? It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

Best Regards

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

View solution in original post

2 REPLIES 2
taimaizumi
Helper I
Helper I

@v-yiruan-msft 
Thank you for your support.

I understand your solution and it will help me!

v-yiruan-msft
Community Support
Community Support

Hi @taimaizumi ,

For the table "Product", you can do some processing in Power Query Editor as shown below, so that it will be easier to make some calculation later.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjQwMDBU0lECYSMgBvN1wUJQppFSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Location1 = _t, Location2 = _t, Key1 = _t, Key2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Location1", Int64.Type}, {"Location2", Int64.Type}, {"Key1", type text}, {"Key2", type text}}),
    #"Unpivoted Only Selected Columns1" = Table.Unpivot(#"Changed Type", {"Location2", "Location1"}, "Attribute.1", "Location"),
    #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Unpivoted Only Selected Columns1", {"Key1", "Key2"}, "Attribute", "Key"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Only Selected Columns",{"Attribute.1", "Attribute"})
in
    #"Removed Columns"

vyiruanmsft_0-1682488062182.png

If the above one can't help you, could you please provide more raw data and the specific examples to explain your expected result( Is it 400)? It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

Best Regards

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

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.