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

Variable seems to break model?

Ok I have the following simple model:

vckbx_0-1651607880303.png

 

I've created the following measure on Registration table:

Active = 
var _date = FIRSTDATE('Date'[Date])
var _start = SELECTEDVALUE('Registration'[StartDate])
var _end = SELECTEDVALUE('Registration'[EndDate])
return
IF(
    _start <= _date
    && OR(_end >= _date,_end=BLANK())
    ,1,0
    )

 

My measure works as expected:

vckbx_1-1651607969036.png

vckbx_2-1651608008368.png

 

But breaks when I pull a field from "Teams" into the Table:

vckbx_3-1651608055901.png

 

Thank you for your help!

1 ACCEPTED SOLUTION
bcdobbs
Super User
Super User

Hi @vckbx ,

Changing your measure to the following will solve the problem:

Active = 
VAR _date = FIRSTDATE('Date'[Date])
VAR _start = SELECTEDVALUE('Registration'[StartDate])
VAR _end = SELECTEDVALUE('Registration'[EndDate])
RETURN
SWITCH ( TRUE (),
    ISEMPTY ( Registration ), BLANK (),
    _start <= _date && ( _end >= _date || _end=BLANK() ), 1,
    0
    )


I'll send a follow up trying to explain why you're hitting this issue in the first place.



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

View solution in original post

3 REPLIES 3
bcdobbs
Super User
Super User

Hi @vckbx ,

Changing your measure to the following will solve the problem:

Active = 
VAR _date = FIRSTDATE('Date'[Date])
VAR _start = SELECTEDVALUE('Registration'[StartDate])
VAR _end = SELECTEDVALUE('Registration'[EndDate])
RETURN
SWITCH ( TRUE (),
    ISEMPTY ( Registration ), BLANK (),
    _start <= _date && ( _end >= _date || _end=BLANK() ), 1,
    0
    )


I'll send a follow up trying to explain why you're hitting this issue in the first place.



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

So the first thing is to recreate the issue with the simplest DAX possible. If you replace your "Active" measure with 

Active = 1

You will find that you get exactly the same rows in the visual. In effect the your measure is returning every combination (a cross join/cartesian product) between Team and Registration. The alteration I suggested to your measure checks to see if for a particular team the Registration table is empty, and return blank if it is which stops this cross join from happening.

 

All of this is being caused by the query Power BI writes under the surface against it's engine. Running the performance analyser and neatening up the query shows that the table visual runs something like:

 

DEFINE
  VAR FilterTable = 
    FILTER(
      KEEPFILTERS(VALUES('Date'[Date])),
      'Date'[Date] >= DATE(2022, 4, 2)
    )

  VAR Result = 
    SUMMARIZECOLUMNS(
      'Registration'[Person],
      'Registration'[Start Date],
      'Registration'[End Date],
      'Team'[Mascot],
      'Team'[Team],
      FilterTable,
      "Active", [Active]
    )

EVALUATE
  Result

 

Refering to DAX Guide there is the following note regarding SUMMARIZECOLUMNS:

"Filters in SUMMARIZECOLUMNS only apply to group-by columns from the same table and to measures. They do not apply to group-by columns from other tables directly, but indirectly through the implied non-empty filter from measures. In order to apply a filter to the group-by column unconditionally, apply the filter through a CALCULATETABLE function that evaluates SUMMARIZECOLUMNS."

 

The date filter doesn't get applied directly to any column so it is literally forming a caresian product of Registrations and Teams for which your original "Active" measure will always return a value. The only way to correct this is to force the measure to return blank on rows we don't want.

 

Another way to see what SUMMARIZECOLUMNS is doing is to create a calculated table in PowerBI with the following DAX:

Test Table = 
 SUMMARIZECOLUMNS(
      'Registration'[Person],
      'Registration'[Start Date],
      'Registration'[End Date],
      'Team'[Mascot],
      'Team'[Team]
    )

I wrote up a similar solution in the following blog: SUMMARIZECOLUMNS and Filters – Dobbs On Data

 

To quote Alberto "SUMMARIZECOLUMNS is likely to be the king of DAX shenanigans"

 

 

 

 

 

 



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

Awesome, thank you so much for the detailed response!

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.