You are on page 1of 9

Power BI Learnings

(For Interview Preparation)

(USERELATIONSHIP vs CROSSFILTER)

USERELATIONSHIP
CROSSFILTER

-Sourav Pattanayak

Sourav Pattanayak
Navigating the Data Maze: USERELATIONSHIP vs. CROSSFILTER

The Dynamics

Welcome to the fascinating world of data modeling and analysis using Power BI and DAX functions. Get
ready to dive into the intricacies of USERELATIONSHIP and CROSSFILTER, where tables connect,
relationships work their magic, and clarity emerges from the occasional confusion.

Sourav Pattanayak
DATA MODELLING & RELATIONSHIPS

First we need to check the data modelling and the active and inactive relationship before jumping
further.

Data Modelling:

The data model looks something like this:

We have two tables having both active and inactive relationship with each other.

Active Relationship:

Sales_data[date] with date_dimension[date]

Inactive Relationship:

Sales_data[weekday] with date_dimension[date]

Now we will see how USERELATIONSHIP changes the Data Modelling in it’s use.

Sourav Pattanayak
THE CONVENIENT SUM FUNCTION

Sum Function:

At first, we do want to know the Total amount of sales and for that using the SUM function is enough.
Isn’t it?

Syntax:

SUM(Table[column])
Consider the following two tables, ‘Sales Data’ and date_dimension.
Table->sales_data

Date Amount Weekday


05-07-2024 500 Friday
27-06-2024 350 Thursday
19-05-2024 200 Sunday
26-04-2024 1100 Friday
17-03-2024 280 Sunday
9-02-2024 690 Friday
…….and so on …….and so on …….and so on

Table-> date_dimension
Date Year Month Week
05-07-2024 Friday July 6
27-06-2024 Thursday June 11
19-05-2024 Sunday May 17
26-04-2024 Friday April 21
17-03-2024 Sunday March 26
9-02-2024 Friday February 27
…….and so on …….and so on …….and so on …….and so on
Now, using the SUM function,
total_sales = SUM(sales_data[Amount])
By using a matrix visual and visualizing the sales by month,we get sales by month,
The output table will look like

Sourav Pattanayak
USERELATIONSHIP

Definition:

This DAX function is used to specify a relationship to be used in a calculation. It overrides any existing
active relationships between tables in the data model for the duration of the calculation.

Syntax:

USERELATIONSHIP(<columnName1>,<columnName2>)

Consider the same two tables, ‘Sales Data’ and date_dimension.


Table->sales_data

Date Amount Weekday


05-07-2024 500 Friday
27-06-2024 350 Thursday
19-05-2024 200 Sunday
26-04-2024 1100 Friday
17-03-2024 280 Sunday
9-02-2024 690 Friday
…….and so on …….and so on …….and so on

Table-> date_dimension
Date Year Month Week
05-07-2024 Friday July 6
27-06-2024 Thursday June 11
19-05-2024 Sunday May 17
26-04-2024 Friday April 21
17-03-2024 Sunday March 26
9-02-2024 Friday February 27
…….and so on …….and so on …….and so on …….and so on
Now, using USERELATIONSHIP Function,
Total Sales by Week =
CALCULATE(
SUM(sales_data[Amount]), USERELATIONSHIP(date_dimension[Date],sales_data[Weekday]))

The output table will look like

Sourav Pattanayak
CROSSFILTER

Definition:

This DAX function is used to control how filters flow between related tables. It specifies the direction of
the filter propagation across a relationship. This function enables us to determine whether filtering should
occur in both directions (both tables) or only in one direction (one table) across the relationship.

Syntax:

CROSSFILTER(<columnName1>, <columnName2>, <direction>)

Consider the same two tables, ‘Sales Data’ and date_dimension.


Table->sales_data

Date Amount Weekday


05-07-2024 500 Friday
27-06-2024 350 Thursday
19-05-2024 200 Sunday
26-04-2024 1100 Friday
17-03-2024 280 Sunday
9-02-2024 690 Friday
…….and so on …….and so on …….and so on

Table-> date_dimension
Date Year Month Week
05-07-2024 Friday July 6
27-06-2024 Thursday June 11
19-05-2024 Sunday May 17
26-04-2024 Friday April 21
17-03-2024 Sunday March 26
9-02-2024 Friday February 27
…….and so on …….and so on …….and so on …….and so on
Now, using USERELATIONSHIP Function,
crossfilter sales = CALCULATE( SUM(sales_data[Amount]),
CROSSFILTER(date_dimension[Date], sales_data[Weekday], BOTH))
The output tables will look like(Using the both date and weekday columns of the two tables)

Sourav Pattanayak
CONFUSION ZONE

What do we see?:
Upon creating card visuals of total amount of sales it is been seen that the total amount is same for all
three DAX uses(SUM/USERELATIONSHIP & CROSSFILTER).

What’s the confusion?


When we use two slicers in our report - one for weekdays and another for dates - we'll notice that the
sales amounts change differently based on which function we use: `CROSSFILTER` or `USERELATIONSHIP`.
1. CROSSFILTER:
- With `CROSSFILTER`, when we change the date slicer, the sales amounts change too. This is because
`CROSSFILTER` adapts to slicer changes, making the sales amounts react to the date selection.
Total Sales by Week not changing

2. USERELATIONSHIP:
- But with `USERELATIONSHIP`, changing the date slicer might not affect the sales amounts. Only the
weekday slicer (attached to the sales table) can change the sales amounts. That's because
`USERELATIONSHIP` keeps the relationship fixed and slicers on the date table might not influence it.

Total Sales by Week


changing

Sourav Pattanayak
THE TRAFFIC SYSTEM

1. CROSSFILTER: It's like setting traffic rules between tables, determining


whether filters can flow in one direction, both directions, or not at all. This
guides how data interacts across tables.

2. Active Filters: Imagine users as traffic controllers, selecting specific dates on


a calendar slicer, which then directs the flow of data accordingly, filtering
related tables to show relevant information.

3. USERELATIONSHIP: Think of it as a detour sign on a road; it temporarily


redirects the flow of traffic (data) between tables, changing how they relate to
each other for the duration of the measure calculation.

4. Effect of USERELATIONSHIP: Instead of following the default route between


tables, measures adjust to the new relationship defined by USERELATIONSHIP,
ensuring they respond accurately to the selected slicer values.

5. Impact on Slicers: When users switch between different slicers, it's like
changing the road signs; the measure responds based on the currently active
relationship, prioritizing the one set by USERELATIONSHIP over the default
relationship.

Sourav Pattanayak
Repost if you find it helpful.
for more.

Sourav Pattanayak

You might also like