USERELATIONSHIP apeears to null with Variables

successken

New Member
Joined
Oct 3, 2011
Messages
27
Here is an interesting one. I have been reworking my DAX to clean them up utilizing Variables. However, I have discovered (or not doing it correctly) that when I use a VAR in a Calculate that utilizes the USERELATIONSHIP DAX it ignores the DAX and defaults back to the active relationship.

Example-

$ of Gross Sales - Special =
VAR TypeFilter = FILTER( 'OrderItems' , 'OrderItems'[itemtype] = "S" )
VAR JobFilter = FILTER('Orders','Orders'[Type] = "Special" )
RETURN
CALCULATE( SUM( 'OrderItems'[Retail] ) , USERELATIONSHIP( 'OrderItems'[orderitemsdate] , 'Calendar'[Date] ) , TypeFilter , JobFilter )
vs.
CALCULATE( SUM( 'OrderItems'[Retail] ) , USERELATIONSHIP( 'OrderItems'[orderitemsdate] , 'Calendar'[Date] ) , 'OrderItems'[itemtype] = "S", 'Orders'[Type] = "Special")


The first returns incorrect vs the second that is correct. Am I doing something incorrect?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
The both return correct answers, but clearly one is not what you want. Variables are executed in the initial filter context. If you want them to use a different relationship, you will need to apply calculate to the variables too
 
Upvote 0
Hi successken

To get the same result using your structure with variables, you could write:

Code:
$ of Gross Sales - Special =
VAR TypeFilter =
    FILTER ( ALL ( 'OrderItems'[itemtype] ), 'OrderItems'[itemtype] = "S" )
VAR JobFilter =
    FILTER ( ALL ( 'Orders'[Type] ), 'Orders'[Type] = "Special" )
RETURN
    CALCULATE (
        SUM ( 'OrderItems'[Retail] ),
        USERELATIONSHIP ( 'OrderItems'[orderitemsdate], 'Calendar'[Date] ),
        TypeFilter,
        JobFilter
    )

A boolean filter argument for calculate like 'OrderItems'[itemtype] = "S" is a filter on the single column itemtype that ignores any existing filters. So it can be replicated with the FILTER ( ALL() ) pattern.

In this case, I'm not sure if variables would make any difference to performance.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top