cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jj_0511
Helper I
Helper I

how to understand effect of crossfilter()

Hi, I have two tables: Date and Sales

 

Date: 

Values in Date column is unique, values in “related date in 2019” is not unique.

Sample data:

Date

related date in 2019

2-4-2023

  2-9-2019

2-5-2023

  2-10-2019

2-6-2023

  2-11-2019

2-7-2023

  2-12-2019

2-8-2023

  2-13-2019

2-9-2023

  2-14-2019

2-10-2023

  2-15-2019

 

 

Sales sample data:

date

store

sales

2/11/2019

  A

4

2/14/2019

  A

7

2/11/2019

  B

2

2/14/2019

  B

6

2/11/2019

  C

7

2/14/2019

  C

3

 

There are 2 relationships defined between Date and Sales. One is one to many between 'Date'[Date] and Sales[date]; the other is many to many between ‘Date’[related date in 2019] and Sales[date]

 

WTD sales 2019 for 2/9/2023 is calculated as:

For 2/9/2023, WTD dates are 2/6/2023 - 2/9/2023. The corresponding dates in 2019 for these dates based on ‘Date’[related date in 2019] are 2/11/2019 to 2/14/2019. As a result, WTD sales 2019 for 2/9/2023 is calculated as total sales for 2/11/2019 to 2/14/2019.

 

I have following measure:

 

Sales WTD 2019 =

VAR firstDayofWeek =  SELECTEDVALUE('Date'[Date])

- WEEKDAY(SELECTEDVALUE('Date'[Date]),2)

+ 1

var CurrentDate = SELECTEDVALUE('Date'[Date])

 

return

CALCULATE(

        SUM(Sales[sales])

        , DATESBETWEEN( 'Date'[Date] , firstDayofWeek , currentdate )

        , USERELATIONSHIP( 'Date'[related date in 2019] , Sales[date] )

        //, CROSSFILTER( 'Date'[Date] , Sales[date] , None )

    )

 

When CROSSFILTER( 'Date'[Date] , Sales[date] , None ) is NOT used, WTD sales 2019 for 2/9/2023 is 32 which is incorrect.

jj_0511_0-1685718122278.png

 

 

When CROSSFILTER( 'Date'[Date] , Sales[date] , None ) is used, WTD sales 2019 for 2/9/2023 is 29 which is correct.

jj_0511_1-1685718122282.png

 

My questions are:

  1. Why is crossfilter() is needed in the measure? Shouldn’t the other relationship be deactivated when userelationship() is in place?
  2. When crossfilter() is not used, how is the value of 32 calculated?

Please find pbix here: pbix  Thanks.

3 REPLIES 3
lbendlin
Super User
Super User

USERELATIONSHIP  only switches an inactive connection to be active temporarily, it doesn't change the connection's filter direction.  CROSSFILTER decides if the standard filtering direction should be modified (ie which table controls the other). CROSSFILTER,,NONE  basically renders the connection useless and makes it into a crossjoin.

@lbendlin Thanks, but when USERELATIONSHIP( 'Date'[related date in 2019] , Sales[date] ) is used, the relationship between 'Date'[Date] and Sales[date] is deactivated, right? Since only one relationship can be active at any time. In that case, why do we still need CROSSFILTER( 'Date'[Date] , Sales[date] , None )?

 

Also, When crossfilter() is not used, how is the value of 32 calculated?

 

 

 the other is many to many between ‘Date’[related date in 2019] and Sales[date]

I didn't catch this part.  You are not supposed to switch a column in a fact table between two columns in a dimension table. You are supposed to switch a column in the dimension table between two columns in a fact table, like Order Date and Ship Date.

 

Use TREATAS to project weird filters between unrelated tables.

Helpful resources

Announcements
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

MPPC 2023 PBI Carousel

Power Platform Conference-Power BI and Fabric Sessions

Join us Oct 1 - 6 in Las Vegas for the Microsoft Power Platform Conference.

Top Solution Authors