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

Join 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.

Reply
manosjxmaah
Frequent Visitor

Reuse Code in same DAX Measure

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? 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

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.

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

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.

audreygerred
Super User
Super User

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





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

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

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.