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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
PowerWhy
Helper IV
Helper IV

Variables within a loop?

Hello,

 

Please can you tell me if it's possible to have variables within a loop?

 

Below I am calculating Output and Net Production twice whereas I thought it would be more efficient to use variables, and re-use them.

 

Thank you for any help,

 

PW

 

Output =
// loop through all customers
sumx('Customers, by Fiscal Year',
if(
calculate(if (round([Output (using calculated field)],2) <> 0, round([Net Production (USING CALCLATED FIELD)],2) / round([Output (using calculated field)],2), sign(round([Net Production (USING CALCLATED FIELD)],2))),
ALL('Dim_Calendar'[Fiscal Period]),
ALL('Dim_Calendar'[Fiscal Quarter])
) < 0.39999999999,
'Calculations'[Output (using calculated field)], 0)
)
1 ACCEPTED SOLUTION
DataInsights
Super User
Super User

@PowerWhy,

 

Yes, you can use variables in a loop (iterator). Here's an example. I used the DIVIDE function instead of "/" (avoids division by 0 errors).

 

Output =
// loop through all customers
SUMX (
    'Customers, by Fiscal Year',
    VAR vOutput =
        ROUND ( [Output (using calculated field)], 2 )
    VAR vNetProd =
        ROUND ( [Net Production (USING CALCLATED FIELD)], 2 )
    RETURN
        IF (
            CALCULATE (
                IF ( vOutput <> 0, DIVIDE ( vNetProd, vOutput ), SIGN ( vNetProd ) ),
                ALL ( 'Dim_Calendar'[Fiscal Period] ),
                ALL ( 'Dim_Calendar'[Fiscal Quarter] )
            ) < 0.39999999999,
            'Calculations'[Output (using calculated field)],
            0
        )
)

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

2 REPLIES 2
PowerWhy
Helper IV
Helper IV

Thank you @DataInsights 

DataInsights
Super User
Super User

@PowerWhy,

 

Yes, you can use variables in a loop (iterator). Here's an example. I used the DIVIDE function instead of "/" (avoids division by 0 errors).

 

Output =
// loop through all customers
SUMX (
    'Customers, by Fiscal Year',
    VAR vOutput =
        ROUND ( [Output (using calculated field)], 2 )
    VAR vNetProd =
        ROUND ( [Net Production (USING CALCLATED FIELD)], 2 )
    RETURN
        IF (
            CALCULATE (
                IF ( vOutput <> 0, DIVIDE ( vNetProd, vOutput ), SIGN ( vNetProd ) ),
                ALL ( 'Dim_Calendar'[Fiscal Period] ),
                ALL ( 'Dim_Calendar'[Fiscal Quarter] )
            ) < 0.39999999999,
            'Calculations'[Output (using calculated field)],
            0
        )
)

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

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.