Sunday, November 22, 2020

How to use the USERRELATIONSHIP Function to activate In Active Relationship in Power BI

USERRELATIONSHIP Function to activate the In Active Relationship in Power BI
The USERRELATIONSHIP Function can be used as filter argument in CALCUALTE to activate the inactive relationship between two tables, for that calculation.
Syntax :
USERELATIONSHIP(<columnName1>,<columnName2>)

Remarks :
USERELATIONSHIP can only be used in functions that take a filter as an argument, for example: CALCULATE, CALCULATETABLE, CLOSINGBALANCEMONTH, CLOSINGBALANCEQUARTER, CLOSINGBALANCEYEAR, OPENINGBALANCEMONTH, OPENINGBALANCEQUARTER, OPENINGBALANCEYEAR, TOTALMTD, TOTALQTD and TOTALYTD functions.

USERELATIONSHIP cannot be used when row level security is defined for the table in which the measure is included. For example, CALCULATE(SUM([SalesAmount]), USERELATIONSHIP(FactInternetSales[CustomerKey], DimCustomer[CustomerKey])) will return an error if row level security is defined for DimCustomer.

USERELATIONSHIP uses existing relationships in the model, identifying relationships by their ending point columns.

In USERELATIONSHIP, the status of a relationship is not important; that is, whether the relationship is active or not does not affect the usage of the function. Even if the relationship is inactive, it will be used and overrides any other active relationships that might be present in the model but not mentioned in the function arguments.

An error is returned if any of the columns named as an argument is not part of a relationship or the arguments belong to different relationships.

If multiple relationships are needed to join table A to table B in a calculation, each relationship must be indicated in a different USERELATIONSHIP function.

If CALCULATE expressions are nested, and more than one CALCULATE expression contains a USERELATIONSHIP function, then the innermost USERELATIONSHIP is the one that prevails in case of a conflict or ambiguity.

Up to 10 USERELATIONSHIP functions can be nested; however, your expression might have a deeper level of nesting, ie. the following sample expression is nested 3 levels deep but only 2 for USEREALTIONSHIP: =CALCULATE(CALCULATE( CALCULATE( &lt;anyExpression&gt;, USERELATIONSHIP( t1[colA], t2[colB])), t99[colZ]=999), USERELATIONSHIP( t1[colA], t2[colA])).

Scenario :
Suppose, we have the Data Model as follows, where we have two relationships between the tbl_Calendar and tbl_Sales tables.
Active Relationship : tbl_Calendar[Date_Id] to tbl_Sales[Order_Date]
In Active Relationship : tbl_Calendar[Date_Id] to tbl_Sales[Shipping_Date]



From above Model, we will calculate the Total Sales by Shipping Date, using the function USERRELATIONSHIP, which activates the relationship between tbl_Calendar[Date_id] and tbl_Sales[Shipping_Date].

Sales by ShipDate = CALCULATE(SUM(tbl_Sales[Gross_Sales]),
USERELATIONSHIP(tbl_Sales[Shipping_Date],tbl_Calendar[Date_Id]))

Result :


Thanks, Tamatam

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog