cancel
Showing results for
Did you mean:

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Helper III

## Difference in values for seeming same measures

Can anyone share any insight as why these dax measures have different results? I can't find any literature on why this would be the case. I've noticed the phenomena twice so far.
First Measure set
1. Occupancy Residency Count_. =
CALCULATE (
[Source.StarRez.DistinctCountofRedIds],
FILTER (
OccupancyInfo,
OccupancyInfo[Check In Date] <= MIN ( 'Date'[Date] )
&& OccupancyInfo[Check Out Date] >= MIN ( 'Date'[Date] )
),
OccupancyInfo[Entry Status Description 5] <> "Cancelled"
)

Occupancy Count YTD_. =
2. MAXX ( SUMMARIZE ( 'Date', 'Date'[Date] ), [Occupancy Residency Count_.] )

Second Measure.

Occupancy Count YTDRemake =
VAR occu =
CALCULATE (
[Source.StarRez.DistinctCountofRedIds],
FILTER (
OccupancyInfo,
OccupancyInfo[Check In Date] <= MIN ( 'Date'[Date] )
&& OccupancyInfo[Check Out Date] >= MIN ( 'Date'[Date] )
),
OccupancyInfo[Entry Status Description 5] <> "Cancelled"
)
RETURN
MAXX ( SUMMARIZE ( 'Date', 'Date'[Date] ), occu )
1 ACCEPTED SOLUTION
Super User

@rtaylor wrote:
Can anyone share any insight as why these dax measures have different results? I can't find any literature on why this would be the case. I've noticed the phenomena twice so far.

The reason is that variables are calculated once within the scope where they are defined. So in your second measure set the "occu" variable will be calculated once for all dates in the current context, this makes the MAXX(SUMMARIZE(...)) call redundant as occu will have the same value for every row.

Whereas in the first measure set when you reference one measure within the summarize in the second measure the expression is re-evaluated within the context of the summarize.

5 REPLIES 5
Super User

@rtaylor wrote:
Can anyone share any insight as why these dax measures have different results? I can't find any literature on why this would be the case. I've noticed the phenomena twice so far.

The reason is that variables are calculated once within the scope where they are defined. So in your second measure set the "occu" variable will be calculated once for all dates in the current context, this makes the MAXX(SUMMARIZE(...)) call redundant as occu will have the same value for every row.

Whereas in the first measure set when you reference one measure within the summarize in the second measure the expression is re-evaluated within the context of the summarize.

Helper III

Thank you for the reply. That makes the most sense. Is there anyway to contain the entire formula within a variable and retrieve the same result?

Thanks Again

Super User

So if you don't want to have 2 separate measures you should be able to insert the calculate() from the first measure into the second as follows. Note, I changed the summarize() to an addcolumns() as it's a safer pattern for an inlined measure like this (the guys at sqlbi.com have an article on why to use addcolumns instead of summarize)

`Occupancy Count YTD_. =MAXX (  ADDCOLUMNS (    VALUES ( 'Date'[Date] ),    "Occupancy Residency Count_.",     CALCULATE (      [Source.StarRez.DistinctCountofRedIds],      FILTER (        OccupancyInfo,        OccupancyInfo[Check In Date] <= MIN ( 'Date'[Date] )         && OccupancyInfo[Check Out Date] >= MIN ( 'Date'[Date] )      ),      OccupancyInfo[Entry Status Description 5] <> "Cancelled"    )  ),  [Occupancy Residency Count_.])`

You could wrap the whole thing in a variable, but you would not really gain any benefit from that as you are not breaking down the calculation into smaller pieces or reusing the variable. It's probably better to just have 2 measures if you can re-use the first measure anywhere or to just inline the calculation as above.

```Occupancy Count YTD_. =
VAR _result = MAXX (ADDCOLUMNS (VALUES ( 'Date'[Date] ),"Occupancy Residency Count_.", CALCULATE ([Source.StarRez.DistinctCountofRedIds],FILTER (OccupancyInfo,OccupancyInfo[Check In Date] <= MIN ( 'Date'[Date] )&& OccupancyInfo[Check Out Date] >= MIN ( 'Date'[Date] )),OccupancyInfo[Entry Status Description 5] <> "Cancelled")),[Occupancy Residency Count_.])
RETURN _result```

Helper III

Hello,

I'm sorry it took me such a long time to reply.

Thank you for comments. My endgame for all of this is to speed up calculation. Right now it takes almost 25 seconds for the calcuation to complete. I'm hoping to get down to 15.

Also I tried your solution and still getting the incorrect display.

Super User

I actually do not believe either is correct. It seems like what you really want is this:

`MAXX ( SUMMARIZE ( 'Date', 'Date'[Date], "__occu",[Occupancy Residency Count_.]),[__occu])`

For the 2nd one you would want:

`MAXX ( SUMMARIZE ( 'Date', 'Date'[Date], "__occu",occu),[__occu])`

Become an expert!: Enterprise DNA
External Tools: MSHGQM
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...