Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
Hello!
I am new to DAX and I have a question about reusing code.
I have this calculated measure:
OH CompareTables tofix =
VAR currentYearSelected = IF(MAX(DateTable[uYear]) = YEAR(TODAY()), TRUE(), FALSE())
VAR sumOH = CALCULATE(
SUM('Table'[OH]),
REMOVEFILTERS(DateTable[Year With Current]),
REMOVEFILTERS(DateTable[uYear]),
USERELATIONSHIP(DateTable[uDate], CompareDates[uDate])
)
RETURN
IF(
currentYearSelected,
IF(
ISINSCOPE(DateTable[QuarterText]),
IF(
MAX(DateTable[uMonth]) = MONTH(TODAY()),
CALCULATE(
SUM('Table'[OH]),
REMOVEFILTERS(DateTable[Year With Current]),
REMOVEFILTERS(DateTable[uYear]),
USERELATIONSHIP(DateTable[uDate], CompareDates[uDate]),
DateTable[uDay] <= DAY(TODAY())
),
sumOH
),
CALCULATE(
SUM('Table'[OH]),
REMOVEFILTERS(DateTable[Year With Current]),
REMOVEFILTERS(DateTable[uYear]),
USERELATIONSHIP(DateTable[uDate], CompareDates[uDate]),
DateTable[uMonth] <= MONTH(TODAY()) && DateTable[uDay] <= DAY(TODAY())
)
),
sumOH
)
As you can see, I have written this block of code 3 times.
SUM('Table'[OH]),
REMOVEFILTERS(DateTable[Year With Current]),
REMOVEFILTERS(DateTable[uYear]),
USERELATIONSHIP(DateTable[uDate], CompareDates[uDate])
Is there a way I can reuse the code, so it seems simpler?
Solved! Go to Solution.
Hi @manosjxmaah ,
Based on the description, it is impossible to reuse the code. Because the sumOH is calculated by condition, the sumOH is value, not a condition anymore.
What’s more, the duplicate code can not be defined as a variable.
SUM('Table'[OH]),
REMOVEFILTERS(DateTable[Year With Current]),
REMOVEFILTERS(DateTable[uYear]),
USERELATIONSHIP(DateTable[uDate], CompareDates[uDate])
Best Regards,
Wisdom Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @manosjxmaah ,
Based on the description, it is impossible to reuse the code. Because the sumOH is calculated by condition, the sumOH is value, not a condition anymore.
What’s more, the duplicate code can not be defined as a variable.
SUM('Table'[OH]),
REMOVEFILTERS(DateTable[Year With Current]),
REMOVEFILTERS(DateTable[uYear]),
USERELATIONSHIP(DateTable[uDate], CompareDates[uDate])
Best Regards,
Wisdom Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi! Since you defined the VAR of sumOH, you would use the variable in the two spots that you have it written out again.
Use variables to improve your DAX formulas - DAX | Microsoft Learn
Proud to be a Super User! | |
Sorry for the late reply.
But as you can see, in this code block for example, I add the filter DateTable[uDay] <= DAY(TODAY()), so it isn't exactly the sumOH variable I have created.
CALCULATE(
SUM('Table'[OH]),
REMOVEFILTERS(DateTable[Year With Current]),
REMOVEFILTERS(DateTable[uYear]),
USERELATIONSHIP(DateTable[uDate], CompareDates[uDate]),
DateTable[uDay] <= DAY(TODAY())
)
I have tried using this, but it doesn't give the same result.
CALCULATE(
sumOH,
DateTable[uDay] <= DAY(TODAY())
)
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
64 | |
55 | |
53 | |
36 | |
34 |
User | Count |
---|---|
85 | |
73 | |
55 | |
45 | |
43 |