Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
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
Solved! Go to Solution.
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
)
)
Proud to be a Super User!
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
)
)
Proud to be a Super User!
User | Count |
---|---|
70 | |
70 | |
34 | |
23 | |
22 |
User | Count |
---|---|
96 | |
94 | |
50 | |
42 | |
40 |