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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
AlexaderMilland
Helper III
Helper III

Efficient rolling total measure

I've created a functional rolling total measure to add to my matrix table. It is however very inefficient, takes extremely long to compute and often requires more than 1GB RAM limit you encounter on Power BI service for the users of the measure.

I've already limited the data amount and the amount of potential filters to reduce calculation resources, however it does not seem to be doing the job well enough.

 

AlexaderMilland_0-1659950029941.png

 

 

 

"

Items: Running Total This Year =

var selectedisoweek = ALLSELECTED(OrderLines[ISOWEEK])
var selectedntype = ALLSELECTED(ProductTable[nType])
var selectedbrand = ALLSELECTED('ProductTable'[Brand])
var selectedtype = ALLSELECTED('ProductTable'[producttype])

var returnval =
CALCULATE (
    SUM ( OrderLines[items] ),
        FILTER(ALL('OrderLines'),'OrderLines'[ISOWEEK] <= MAX( 'OrderLines'[ISOWEEK] )
    && OrderLines[ISOWEEK] in selectedisoweek
    ),FILTER('ProductTable',ProductTable[nType] in selectedntype&&ProductTable[producttype] in selectedtype && ProductTable[Brand] in selectedbrand)

    ,'Calendar'[CalendarDate].[Year] = 2022
)

return returnval

"

 

Any suggestions for optimizing the query? 
Could this simply be resolved using a year-to-date calculation instead?

 

5 REPLIES 5
m3tr01d
Continued Contributor
Continued Contributor

hi @AlexaderMilland 

I think there are two things that impact the performance of your measures :
1) You are filtering a table in both Filter statement. Filtering a table is really expensive when done on a huge fact table

m3tr01d_0-1660672013195.png

If you look at some of the videos from SQLBI, its the first thing that they will explain to you is avoid filtering table, always filter a column instead.

2) Second thing is you are not using your calendar table. ISOWEEK should be an attribute of your calendar and you should filter the column ISOWEEK from your calendar table and not the one from the Fact table. Iterating on a column of a dimension will be faster.

m3tr01d_1-1660672137511.png

 

You can also remove any columns in the OrderLines table as they take space and impact comprehension of each columns.

 

m3tr01d
Continued Contributor
Continued Contributor

Hi @AlexaderMilland 

There are some things I don't understand :

var selectedisoweek = ALLSELECTED(OrderLines[ISOWEEK])
var selectedntype = ALLSELECTED(ProductTable[nType])
var selectedbrand = ALLSELECTED('ProductTable'[Brand])
var selectedtype = ALLSELECTED('ProductTable'[producttype])

var returnval =
CALCULATE (
    SUM ( OrderLines[items] ),
        FILTER(ALL('OrderLines'),'OrderLines'[ISOWEEK] <= MAX( 'OrderLines'[ISOWEEK] )
    && OrderLines[ISOWEEK] in selectedisoweek
    ),FILTER('ProductTable',ProductTable[nType] in selectedntype&&ProductTable[producttype] in selectedtype && ProductTable[Brand] in selectedbrand)

    ,'Calendar'[CalendarDate].[Year] = 2022
)

What are you trying to accomplish by explicitly filtering the Product table with your variables?

DataInsights
Super User
Super User

@AlexaderMilland,

 

It's simpler to use a year-to-date calculation, and specify the year in a slicer/filter.

 

 Be sure to mark your Calendar table as a date table. Right-click the table and select "Mark as date table":

 

DataInsights_0-1659967097647.png

 





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

Proud to be a Super User!




Having trouble making the year-to-date last year measure.

I do not want it to sum to the same days,I want it to sum up the same weeks from the last year. 

ISOWEEK = 0 is excluded, e.g. the first few days of the year. 

This year week 1 starts on 3rd of January, so i want the YTD measure on week 3 to be the sum of week 1+2+3 for 2022. This works fine with a regular YTD for this year, e.g. it sums from 3/1/2022 to 23/1/2022. 

When i do the same for last year (2021) i want it to sum from 4/1/2021 to 24/1/2021, since week 1 start on a later day in 2021. 

 

 

 

Any ideas?

@AlexaderMilland,

 

Try this measure. Add the column ISOWEEK to the Calendar table and use this field as matrix rows. Use a slicer/filter to specify 2022 (and thus get 2021 data).

 

Items: Running Total Last Year =
CALCULATE (
    SUM ( OrderLines[items] ),
    'Calendar'[Year] = MAX ( 'Calendar'[Year] ) - 1,
    'Calendar'[ISOWEEK] <= MAX ( 'Calendar'[ISOWEEK] )
)

 





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

Proud to be a Super User!




Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.