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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Joorge_C
Resolver II
Resolver II

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
Joorge_C
Resolver II
Resolver II

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
Joorge_C
Resolver II
Resolver II

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)

Joorge_C
Resolver II
Resolver II

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, @Joorge_C 

 

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
Employee
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

@Joorge_C 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.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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