Forum Discussion

rtaylor's avatar
rtaylor
Helper III
7 years ago
Solved

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 )

  • 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

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    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])

  • 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.

    • rtaylor's avatar
      rtaylor
      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

      • d_gosbell's avatar
        d_gosbell
        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