Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
SteveIOW
Helper II
Helper II

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]), MainFacts[Period] = "FY2020-21")
VAR vC2122 = CALCULATE(DISTINCTCOUNT(MainFacts[EpisodeNumber]), MainFacts[Period] = "FY2021-22")
VAR vNF2021 = IF(CALCULATE(DISTINCTCOUNT(MainFacts[EpisodeNumber]), MainFacts[Period] = "FY2020-21") = 0,  1, 0)
VAR vNF2122 = IF(CALCULATE(DISTINCTCOUNT(MainFacts[EpisodeNumber]), MainFacts[Period] = "FY2021-22") = 0,  1, 0)
 
I plugged these in using RETURN:
SUMMARIZE(
MainFacts,
MainFacts[Provider_Code],
"Count2021", vC2021,
"NullFlag2021", vNF2021,
"Count2122", vC2122,
"NullFlag2122", vNF2122
)
and it doesn't work... I get...SteveIOW_0-1683881531688.png
 
However, if I repace the variables with the actual statement it does work...
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)
)
SteveIOW_1-1683881624116.png

 

What on earth is going on??

 

cheers

 

Steve

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

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

View solution in original post

4 REPLIES 4
johnt75
Super User
Super User

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

But what if i wanted to use that variable again in a later calculation? In this case Count2122 for example.

Instead of using ADDCOLUMNS you could use a combination of GENERATE and ROW, the general pattern is something like

My Table =
GENERATE (
    SUMMARIZE ( 'Table', 'Table'[Column 1], 'Table'[Column 2] ),
    VAR Var1 =
        CALCULATE ( SUM ( 'Table2'[Value] ) )
    VAR Var2 =
        IF (
            Var1 > 0,
            CALCULATE ( SUM ( 'Table2'[Value2] ) ),
            CALCULATE ( SUM ( 'Table2'[Value3] ) )
        )
    RETURN
        ROW ( "Var1", Var1, "Var2", Var2 )
)

all of this is incredibly helpful. Many many thanks.

 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.