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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

How would you optimize SUMX with nested Switch or IF? ...its taking around 25sec to run

Hi guys, thanks for the help.

Need a meassure to look at the Quarter and do sum A+B for the past months and C for the current and future.

Any ideas how I can optimize the below?

 

SUMX('All Data',
                    SWITCH(TRUE() ,
                            All Data'[MonthNum]>= Todays_month ,    CALCULATE(SUM('All Data'[A]))+CALCULATE(SUM('All Data'[B])),
                           'All Data'[MonthNum]< Todays_month ,    CALCULATE(SUM('All Data'[C]))
                         )
)
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi all,

so what I ended up doing is a combination of a few Calculate Sums, I did not use the Sumx as it continued to take over 10 secs.

I also added a day condition to the filtering criteria.

Thanks all for the ideas.

 

Here is a snippet of the meassure:

 

var _todaysMonth = MONTH(TODAY())

var _todaysDay = DAY(TODAY())

return

CALCULATE(SUM('All Data'[A]),MONTH('All Data'[Date])<=_todaysMonth && _todaysDay <=7) +CALCULATE(SUM('Net[B]),MONTH('Net'[DateForMonth])<=_todaysMonth && _todaysDay<=7) +

CALCULATE(SUM('All Data'[A]),MONTH('All Data'[Date])=_todaysMonth && _todaysDay>7) + CALCULATE(SUM('Net'[B]),MONTH('Net'[DateForMonth])=_todaysMonth && _todaysDay>7)+

CALCULATE(SUM('All Data'[A]),MONTH('All Data'[Date])>_todaysMonth) + CALCULATE(SUM('Net'[B]),MONTH('Net'[DateForMonth])>_todaysMonth)+

CALCULATE(SUM('All Data'[C]),MONTH('All Data'[Date])<_todaysMonth && _todaysDay>7)

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi all,

so what I ended up doing is a combination of a few Calculate Sums, I did not use the Sumx as it continued to take over 10 secs.

I also added a day condition to the filtering criteria.

Thanks all for the ideas.

 

Here is a snippet of the meassure:

 

var _todaysMonth = MONTH(TODAY())

var _todaysDay = DAY(TODAY())

return

CALCULATE(SUM('All Data'[A]),MONTH('All Data'[Date])<=_todaysMonth && _todaysDay <=7) +CALCULATE(SUM('Net[B]),MONTH('Net'[DateForMonth])<=_todaysMonth && _todaysDay<=7) +

CALCULATE(SUM('All Data'[A]),MONTH('All Data'[Date])=_todaysMonth && _todaysDay>7) + CALCULATE(SUM('Net'[B]),MONTH('Net'[DateForMonth])=_todaysMonth && _todaysDay>7)+

CALCULATE(SUM('All Data'[A]),MONTH('All Data'[Date])>_todaysMonth) + CALCULATE(SUM('Net'[B]),MONTH('Net'[DateForMonth])>_todaysMonth)+

CALCULATE(SUM('All Data'[C]),MONTH('All Data'[Date])<_todaysMonth && _todaysDay>7)

Anonymous
Not applicable

Thanks all for the suggestions and help. Im going to be trying out and see which one works best. Will update later.

Thanks again!

v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

I'd like to suggest you try the following measure to see if it helps.

Measure =
SUMX (
    'All Data',
    SWITCH (
        TRUE (),
        'All Data'[MonthNum] >= Todays_month, 'All Data'[A] + 'All Data'[B],
        'All Data'[MonthNum] < Todays_month, 'All Data'[C]
    )
)

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

mahoneypat
Microsoft Employee
Microsoft Employee

I would do a couple bigger calculations instead of lots of little ones, as follows:

 

Assuming Todays_month is a variable in the same expression,

New measure =
VAR Todays_month =
    MONTH ( TODAY () ) //or whatever your current approach is
VAR __prevsales =
    SUMX ( FILTER ( 'All Data', 'All Data'[Month] < Todays_month ), 'All Data'[C] )
VAR __currentfuturesales =
    SUMX (
        FILTER ( 'All Data', 'All Data'[Month] >= Todays_month ),
        'All Data'[A] + 'All Data'[B]
    )
RETURN
    __prevsales + __currentfuturesales

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


parry2k
Super User
Super User

@Anonymous what is todays_month, you need to use some aggregation if you are adding this as a measure

 

SUMX('All Data',
                    SWITCH(TRUE() ,
                            MAX('All Data'[MonthNum])>= Todays_month ,    CALCULATE(SUM('All Data'[A]))+CALCULATE(SUM('All Data'[B])),
                           MAX('All Data'[MonthNum])< Todays_month ,    CALCULATE(SUM('All Data'[C]))
                         )
)

 

assuming Todays_Month is a measure.

 

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

Thanks for the quick reply

 

todays month its just a var to look up the current month number

var _todaysmonth = month(today())

Helpful resources

Announcements
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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 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.

Top Solution Authors
Top Kudoed Authors