Forum Discussion

SteveIOW's avatar
SteveIOW
Icon for Helper II rankHelper II
3 years ago
Solved

Using Variables in Summarize

Hi, wondering if anyone understands what is happening here.   I have built a table using summarize. To start with I used 4 variables: VAR vC2021 = CALCULATE(DISTINCTCOUNT(MainFacts[EpisodeNumber...
  • johnt75's avatar
    3 years ago

    Variables in DAX aren't really variables, they're constants. They are only calculated once, when they are defined, which is why you're seeing the same wrong values for each row.

    Its also worth pointing out that it is not best practice to use SUMMARIZE to produce calculated columns as performance is poor and you can get unexpected results. It is better to use SUMMARIZE to do the grouping and then use ADDCOLUMNS to produce the calculations, so your code would become

    My Table =
    ADDCOLUMNS (
        SUMMARIZE ( MainFacts, MainFacts[Provider_Code] ),
        "Count2021",
            CALCULATE (
                DISTINCTCOUNT ( MainFacts[EpisodeNumber] ),
                MainFacts[Period] = "FY2020-21"
            ),
        "NullFlag2021",
            IF (
                CALCULATE (
                    DISTINCTCOUNT ( MainFacts[EpisodeNumber] ),
                    MainFacts[Period] = "FY2020-21"
                ) = 0,
                1,
                0
            ),
        "Count2122",
            CALCULATE (
                DISTINCTCOUNT ( MainFacts[EpisodeNumber] ),
                MainFacts[Period] = "FY2021-22"
            ),
        "NullFlag2122",
            IF (
                CALCULATE (
                    DISTINCTCOUNT ( MainFacts[EpisodeNumber] ),
                    MainFacts[Period] = "FY2021-22"
                ) = 0,
                1,
                0
            )
    )