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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
tomerb
Helper I
Helper I

Calculating cumulative total across categories

I'm using this to calculate cumulative/running total across categories, where the sum of each category is added to the next category in line, based on a pre-defined order:

Running Total = 
VAR CurrentCategoryOrder = MAX(TABLE[CATEGORY_ORDER])
RETURN
CALCULATE(
    SUM(TABLE[VALUE]),
    FILTER(
        ALL(TABLE[CATEGORY], TABLE[CATEGORY_ORDER]),
        TABLE[CATEGORY_ORDER] <= CurrentCategoryOrder
    )
)

This works most of the time, except for when no data for a category exists for a specific month, in which case, instead of the cell showing just the sum of all previous categories, it is showing 0.

Any suggestions on how to handle this correctly?

1 ACCEPTED SOLUTION

Why would you expect 21?

 

lbendlin_0-1734308684385.png

 

View solution in original post

15 REPLIES 15
v-tianyich-msft
Community Support
Community Support

Hi @tomerb ,

lbendlin seems to have solved the problem, and if nothing else, you can mark his solution as a solution for other members to find.

Best regards,
Community Support Team_ Scott Chang

Unless I'm missing something, this issue is not solved yet. Please point me to the comment you think provide a solution.

Hi @tomerb ,


What are your expected results?


Best regards,
Community Support Team_ Scott Chang

My apologies. I missed the fact that @lbendlin attached a file and a screenshot, and just referred to his comment. I see the solution provided now.

This is a great answer, thanks @lbendlin. The issue I have with it is that it forces me to use the static table in the Rows section of the table, which doesn't work with real data. Any suggestion on how to cope with that?
To provide more info about the model, the main table ('Table' in our example) holds multiple columns which form a hierarchy I use in the Rows section of the matrix. If I mix the categories from Table2 with this data, the data gets duplicated in the table.

Does it mean that I'll need to duplicate the data from the columns I need? This means that effectively I'll need to duplicate the entire data table, and all just for the table to act reasonably. 🙂

Please provide sample data that fully covers your issue.
Please show the expected outcome based on the sample data you provided.

I will mark this thread as solved, and will create a new one with the requested data, as it might be a bit of a variation from the initial question.

 

Thank you for your help!

tomerb
Helper I
Helper I

Please see the this file, based on @Jihwan_Kim reply, with a modified measure to match the one I'm using.

Basically, when all the categories for all months exist in the data set, there's no issue, as shown here:

 

Screenshot 2024-12-15 at 11.32.52.png

But if in one of the months an entry with one category is missing comletely, the data for that cell will be missing too, and this is what I get:

Screenshot 2024-12-15 at 11.34.01.png

In this case, I removed the last row from the generated table, which means that category 'd' doesn't exist in the dataset for December. What I expect to see instead of a blank cell, is the value 16, since this is the running total for that date.

Why would you expect 21?

 

lbendlin_0-1734308684385.png

 

I would not expect 21, I excpet 16 instead of an empty cell.

Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your semantic model looks like, but please try something like below whether it works.
I experimented by using the attached pbix file.

 

Jihwan_Kim_0-1734171289228.png

 

WINDOW function (DAX) - DAX | Microsoft Learn

 

Running Total = 
CALCULATE (
    SUM ( 'Table'[value] ),
    WINDOW (
        1,
        ABS,
        0,
        REL,
        ALL ( 'Table' ),
        ORDERBY ( 'Table'[date], ASC, 'Table'[category_order], ASC ),
        ,
        ,
        MATCHBY ( 'Table'[category_order], 'Table'[date] )
    )
)

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Thank you for the response.

I looked at the file you shared. In your example table a value exists for the entire set of categories for each month. This means that it might be that for a certain month category X has the value 0, in this case the running total indeed will get calculated.

However, in my case, I have missing categories for some months. This means that category X doesn't exist for that month, and in this case the running total will not get added to that month and category - I validated this in your dataset by removing the last row from the table.

Perhaps something along the line of what @lbendlin is suggesting is the only way to go?

lbendlin
Super User
Super User

To report on things that are not there you need to cross join disconnected tables and use measures.

Thank you for the response.

Do you mean I should create a helper table that has all the categories and their mapped order, then join that with the data table I have? If so, can you point me to a documentation, or some other data point that help me get started?
On that matter, perhaps it's easire to go back to the data ingestion pipeline, and add dummy items for months that have "missing" data?

adding dummy data is not a preferred approach in most cases.

 

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
Do not include sensitive information. Do not include anything that is unrelated to the issue or question.
Please show the expected outcome based on the sample data you provided.

Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...
Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

It looks like I accidentally replied to my own thread, instead of your comment. Please find my response here.

Helpful resources

Announcements
Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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

"); $(".slidesjs-pagination" ).prependTo(".pagination_sec"); $(".slidesjs-pagination" ).append("
"); $(".slidesjs-play.slidesjs-navigation").appendTo(".playpause_sec"); $(".slidesjs-stop.slidesjs-navigation").appendTo(".playpause_sec"); $(".slidesjs-pagination" ).append(""); $(".slidesjs-pagination" ).append(""); } catch(e){ } /* End: This code is added by iTalent as part of iTrack COMPL-455 */ $(".slidesjs-previous.slidesjs-navigation").attr('tabindex', '0'); $(".slidesjs-next.slidesjs-navigation").attr('tabindex', '0'); /* start: This code is added by iTalent as part of iTrack 1859082 */ $('.slidesjs-play.slidesjs-navigation').attr('id','playtitle'); $('.slidesjs-stop.slidesjs-navigation').attr('id','stoptitle'); $('.slidesjs-play.slidesjs-navigation').attr('role','tab'); $('.slidesjs-stop.slidesjs-navigation').attr('role','tab'); $('.slidesjs-play.slidesjs-navigation').attr('aria-describedby','tip1'); $('.slidesjs-stop.slidesjs-navigation').attr('aria-describedby','tip2'); /* End: This code is added by iTalent as part of iTrack 1859082 */ }); $(document).ready(function() { if($("#slides .item").length < 2 ) { /* Fixing Single Slide click issue (commented following code)*/ // $(".item").css("left","0px"); $(".item.slidesjs-slide").attr('style', 'left:0px !important'); $(".slidesjs-stop.slidesjs-navigation").trigger('click'); $(".slidesjs-previous").css("display", "none"); $(".slidesjs-next").css("display", "none"); } var items_length = $(".item.slidesjs-slide").length; $(".slidesjs-pagination-item > button").attr("aria-setsize",items_length); $(".slidesjs-next, .slidesjs-pagination-item button").attr("tabindex","-1"); $(".slidesjs-pagination-item button").attr("role", "tab"); $(".slidesjs-previous").attr("tabindex","-1"); $(".slidesjs-next").attr("aria-hidden","true"); $(".slidesjs-previous").attr("aria-hidden","true"); $(".slidesjs-next").attr("aria-label","Next"); $(".slidesjs-previous").attr("aria-label","Previous"); //$(".slidesjs-stop.slidesjs-navigation").attr("role","button"); //$(".slidesjs-play.slidesjs-navigation").attr("role","button"); $(".slidesjs-pagination").attr("role","tablist").attr("aria-busy","true"); $("li.slidesjs-pagination-item").attr("role","list"); $(".item.slidesjs-slide").attr("tabindex","-1"); $(".item.slidesjs-slide").attr("aria-label","item"); /*$(".slidesjs-stop.slidesjs-navigation").on('click', function() { var itemNumber = parseInt($('.slidesjs-pagination-item > a.active').attr('data-slidesjs-item')); $($('.item.slidesjs-slide')[itemNumber]).find('.c-call-to-action').attr('tabindex', '0'); });*/ $(".slidesjs-stop.slidesjs-navigation, .slidesjs-pagination-item > button").on('click keydown', function() { $.each($('.item.slidesjs-slide'),function(i,el){ $(el).find('.c-call-to-action').attr('tabindex', '-1'); }); var itemNumber = parseInt($('.slidesjs-pagination-item > button.active').attr('data-slidesjs-item')); $($('.item.slidesjs-slide')[itemNumber]).find('.c-call-to-action').attr('tabindex', '0'); }); $(".slidesjs-play.slidesjs-navigation").on('click', function() { $.each($('.item.slidesjs-slide'),function(i,el){ $(el).find('.c-call-to-action').attr('tabindex', '-1'); }); }); $(".slidesjs-pagination-item button").keyup(function(e){ var keyCode = e.keyCode || e.which; if (keyCode == 9) { e.preventDefault(); $(".slidesjs-stop.slidesjs-navigation").trigger('click').blur(); $("button.active").focus(); } }); $(".slidesjs-play").on("click",function (event) { if (event.handleObj.type === "click") { $(".slidesjs-stop").focus(); } else if(event.handleObj.type === "keydown"){ if (event.which === 13 && $(event.target).hasClass("slidesjs-play")) { $(".slidesjs-stop").focus(); } } }); $(".slidesjs-stop").on("click",function (event) { if (event.handleObj.type === "click") { $(".slidesjs-play").focus(); } else if(event.handleObj.type === "keydown"){ if (event.which === 13 && $(event.target).hasClass("slidesjs-stop")) { $(".slidesjs-play").focus(); } } }); $(".slidesjs-pagination-item").keydown(function(e){ switch (e.which){ case 37: //left arrow key $(".slidesjs-previous.slidesjs-navigation").trigger('click'); e.preventDefault(); break; case 39: //right arrow key $(".slidesjs-next.slidesjs-navigation").trigger('click'); e.preventDefault(); break; default: return; } $(".slidesjs-pagination-item button.active").focus(); }); }); // Start This code is added by iTalent as part of iTrack 1859082 $(document).ready(function(){ $("#tip1").attr("aria-hidden","true").addClass("hidden"); $("#tip2").attr("aria-hidden","true").addClass("hidden"); $(".slidesjs-stop.slidesjs-navigation, .slidesjs-play.slidesjs-navigation").attr('title', ''); $("a#playtitle").focus(function(){ $("#tip1").attr("aria-hidden","false").removeClass("hidden"); }); $("a#playtitle").mouseover(function(){ $("#tip1").attr("aria-hidden","false").removeClass("hidden"); }); $("a#playtitle").blur(function(){ $("#tip1").attr("aria-hidden","true").addClass("hidden"); }); $("a#playtitle").mouseleave(function(){ $("#tip1").attr("aria-hidden","true").addClass("hidden"); }); $("a#play").keydown(function(ev){ if (ev.which ==27) { $("#tip1").attr("aria-hidden","true").addClass("hidden"); ev.preventDefault(); return false; } }); $("a#stoptitle").focus(function(){ $("#tip2").attr("aria-hidden","false").removeClass("hidden"); }); $("a#stoptitle").mouseover(function(){ $("#tip2").attr("aria-hidden","false").removeClass("hidden"); }); $("a#stoptitle").blur(function(){ $("#tip2").attr("aria-hidden","true").addClass("hidden"); }); $("a#stoptitle").mouseleave(function(){ $("#tip2").attr("aria-hidden","true").addClass("hidden"); }); $("a#stoptitle").keydown(function(ev){ if (ev.which ==27) { $("#tip2").attr("aria-hidden","true").addClass("hidden"); ev.preventDefault(); return false; } }); }); // End This code is added by iTalent as part of iTrack 1859082
Top Kudoed Authors