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
Jdidrik
New Member

Problem with Running Total Sum and Weeks where input = 0

 

Hi,

I have problems with calculating Running sum of number of revisions in a 6 week span. 
I have in total 10  engineers and I want to see the total number of revisions for rolling 6 weeks.

My data is shown in a table like this: (simpified)

Assigned EngineerWeekRevNewSales order
Engineer 1110xxxxx1
Engineer 2110xxxxx2
Engineer 3201xxxxx3
Engineer 2201xxxxx3
Engineer 1210xxxxx4


I have tried many different Dax expressions, but I keep running into the same issue regardless on solutions I try.
With the above table I get the following in return:
Total Rollingsum of rev:
Week 1 = 2

Week 2 = 2

 

It should be:

Week 1 = 2
Week 2 = 3 

 

My table does not sum up number of Rev per week per engineer, it shows Rev or New = 1 or 0 per order. If one order has multiple revisions in same week, it will have multiple lines with rev = 1.

 

What happenes is that when an Engineer has 0 Rev in a week it doesnt add the rolling 6weeks total Rev for that engineer in the week where he has 0. If that engineer has drev in Week 3, it will calculate the correct total sum. 

 

Formula I use (one of the many I have tried):

Rolling6WeekTotalRev =
VAR CurrentWeek =
    MAX ( Query1[Week] )
RETURN
    CALCULATE (
        SUMX (
            FILTER (
                ALL ( Query1 ),
                Query1[Week] <= CurrentWeek
                    && Query1[Week] > CurrentWeek - 6
            ),
            Query1[rev]
        )
    )


What I really dont undestand, is if I make a separate measure per engineer:

Rolling6WeekTotalRev_Engineer2 = 
VAR CurrentWeek = MAX(Query1[Week])
RETURN
CALCULATE(
    SUMX(
        FILTER(
            ALL(Query1),
            Query1[eCOSAssignedEngineer] = "Engineer2" &&
            Query1[Week] <= CurrentWeek &&
            Query1[Week] > CurrentWeek - 6
        ),
        Query1[rev]
    )
)


Then it calculates correctly the running 6Weeks total sum for Engineer1:
Week 1 = 1
Week 2  = 1 (1+0).

I have found workarounds that manage to calculate the correct totalt rolling sum of rev without making separate measures per Engineer, the problem is that as soon as I put in the Engineer as Legend, the calculation fails. (Stacked bar chart).

 

Does anyone have an idea on how I can solve this using one measure? 

What I want in the end, is a graph that shows the Rolling 6 week average "Rate of Late Change (total Sum Rev / Total Sum New)/Nnumber of weeks. per Engineer. this is easy to solve, if I can mange to get the Rolling Total Rev measure to work correctly. 

1 ACCEPTED SOLUTION

So, the below is similar to what you were trying.

 

Rolling6WeekTotalRev_BadEngineerFilter = 
VAR _thisWk = MAX( Query1[Week] )
VAR _filt = FILTER( 
    ALL( Query1[Week] ), 
    Query1[Week] > _thisWk-6 
    && Query1[Week] <= _thisWk 
)
RETURN
CALCULATE( SUM( Query1[rev] ), _filt )

 

This works as you want when Query1[Assigned Engineer] is not used in the visual's fields.

 

MarkLaf_0-1744162461249.png

 

But, it suddenly does not work correctly if we put Query1[Assigned Engineer] in the Legend, as you have highlighted in your posts.

 

MarkLaf_1-1744162582771.png

 

To see what is happening, we can do a simple test. Replace our measure with the following:

Test = 1

 

This is a simple constant, in any part of the visual where a value can be displayed, it will provide a 1. With this, our stacked columns with Query1[Assigned Engineer] in Legend looks like the below.

 

MarkLaf_2-1744162754352.png

It's a little clearer now what the issue is. With Query1[Assigned Engineer] in Legend, it prevents any value from calculating for ( Query1[Week], Query1[Assigned Engineer] ) pairings that do not exist in our table. With the current model, it is impossible to get the result you want.

 

The answer is that you need to put this into a star schema. Or, at the very least, move Engineer into a separate dimension table. Below are some quick steps to do this if you are not already familiar.

 

First, create your dimension table. Click Modeling > New Table and do something like the below:

Engineers = ALL( Query1[Assigned Engineer] )

 

Now, relate your new table to the original:

 

MarkLaf_3-1744163521483.png

 

Your new model should now look something like this.

 

MarkLaf_4-1744163539678.png

 

Next, in your stacked column visual, replace Query1[Assigned Engineer] with Engineers[Assigned Engineer] in the Legend. With this change, the following measure will work. Note it is almost identical except how we calculate the current week at the top.

 

Rolling6WeekTotalRev = 
VAR _thisWk = CALCULATE( MAX( Query1[Week] ), REMOVEFILTERS( Engineers ) )
VAR _filt = FILTER( 
    ALL( Query1[Week] ), 
    Query1[Week] > _thisWk-6 
    && Query1[Week] <= _thisWk 
)
RETURN
CALCULATE( SUM( Query1[rev] ), _filt )

 

MarkLaf_5-1744163732897.png

 

View solution in original post

4 REPLIES 4
Jdidrik
New Member

Thanks for trying to help me @isjoycewang ! 
Your solution does not work either.  I have made a sample .pbix to show the issue, but I dont understand how to upload it. I get "The file type (.pbix) is not supported." when I try to drag & drop.

In the report the upper graph shows total number of rev each week, and the bottom the running total sum for 6weeks. If we take week 6 as an example. The Rolling sum should be: 7+9+8+6+5+5 = 40. But the "total running sum measure" calculates 36.


Jdidrik_0-1744109107481.png


Since I am unable to upload the file, below is the table from my sample file:

WeekAssigned Engineerrevorg
1Engineer 11 
1Engineer 21 
1Engineer 31 
1Engineer 41 
1Engineer 1001
1Engineer 101 
2Engineer 10 
2Engineer 101
2Engineer 11 
2Engineer 501
2Engineer 201
2Engineer 21 
2Engineer 701
2Engineer 601
2Engineer 61 
2Engineer 31 
2Engineer 40 
2Engineer 401
2Engineer 801
2Engineer 901
2Engineer 1001
2Engineer 101 
3Engineer 101
3Engineer 11 
3Engineer 50 
3Engineer 201
3Engineer 21 
3Engineer 71 
3Engineer 601
3Engineer 61 
3Engineer 801
3Engineer 91 
3Engineer 1001
3Engineer 101 
4Engineer 10 
4Engineer 101
4Engineer 11 
4Engineer 501
4Engineer 51 
4Engineer 201
4Engineer 21 
4Engineer 701
4Engineer 601
4Engineer 61 
4Engineer 31 
4Engineer 401
4Engineer 41 
4Engineer 801
4Engineer 81 
4Engineer 100 
4Engineer 1001
4Engineer 101 
5Engineer 101
5Engineer 11 
5Engineer 501
5Engineer 51 
5Engineer 201
5Engineer 21 
5Engineer 71 
5Engineer 601
5Engineer 61 
5Engineer 31 
5Engineer 401
5Engineer 41 
5Engineer 801
5Engineer 81 
5Engineer 100 
5Engineer 1001
5Engineer 101 
6Engineer 101
6Engineer 11 
6Engineer 201
6Engineer 21 
6Engineer 601
6Engineer 61 
6Engineer 31 
6Engineer 401
6Engineer 41 
6Engineer 801
6Engineer 81 
6Engineer 901
6Engineer 100 
6Engineer 1001
6Engineer 101 
7Engineer 101
7Engineer 11 
7Engineer 501
7Engineer 51 
7Engineer 201
7Engineer 21 
7Engineer 701
7Engineer 601
7Engineer 61 
7Engineer 401
7Engineer 41 
7Engineer 801
7Engineer 81 
7Engineer 1001
7Engineer 101 
8Engineer 11 
8Engineer 501
8Engineer 51 
8Engineer 201
8Engineer 21 
8Engineer 701
8Engineer 61 
8Engineer 301
8Engineer 401
8Engineer 41 
8Engineer 801
8Engineer 81 
8Engineer 1001
8Engineer 101 
9Engineer 101
9Engineer 11 
9Engineer 501
9Engineer 51 
9Engineer 201
9Engineer 21 
9Engineer 601
9Engineer 61 
9Engineer 31 
9Engineer 401
9Engineer 41 
9Engineer 801
9Engineer 81 
9Engineer 1001
9Engineer 101 
10Engineer 101
10Engineer 11 
10Engineer 501
10Engineer 51 
10Engineer 201
10Engineer 21 
10Engineer 701
10Engineer 71 
10Engineer 601
10Engineer 61 
10Engineer 401
10Engineer 41 
10Engineer 801
10Engineer 81 
10Engineer 91 
10Engineer 1001
10Engineer 101 
11Engineer 10 
11Engineer 101
11Engineer 11 
11Engineer 501
11Engineer 51 
11Engineer 201
11Engineer 21 
11Engineer 601
11Engineer 61 
11Engineer 31 
11Engineer 401
11Engineer 41 
11Engineer 801
11Engineer 91 
11Engineer 1001
11Engineer 101 
12Engineer 101
12Engineer 11 
12Engineer 501
12Engineer 51 
12Engineer 201
12Engineer 21 
12Engineer 601
12Engineer 61 
12Engineer 401
12Engineer 41 
12Engineer 801
12Engineer 81 
12Engineer 901
12Engineer 1001
12Engineer 101 
13Engineer 10 
13Engineer 101
13Engineer 11 
13Engineer 501
13Engineer 51 
13Engineer 21 
13Engineer 60 
13Engineer 601
13Engineer 61 
13Engineer 401
13Engineer 41 
13Engineer 801
13Engineer 81 
13Engineer 90 
13Engineer 1001
13Engineer 101 
14Engineer 101
14Engineer 11 
14Engineer 51 
14Engineer 201
14Engineer 21 
14Engineer 601
14Engineer 61 
14Engineer 41 
14Engineer 801
14Engineer 81 
14Engineer 91 
14Engineer 1001
14Engineer 101 
15Engineer 101
15Engineer 201
15Engineer 601
15Engineer 401
15Engineer 41 
15Engineer 801
15Engineer 81 
15Engineer 1001
15Engineer 101 


Jan Didrik 

 

So, the below is similar to what you were trying.

 

Rolling6WeekTotalRev_BadEngineerFilter = 
VAR _thisWk = MAX( Query1[Week] )
VAR _filt = FILTER( 
    ALL( Query1[Week] ), 
    Query1[Week] > _thisWk-6 
    && Query1[Week] <= _thisWk 
)
RETURN
CALCULATE( SUM( Query1[rev] ), _filt )

 

This works as you want when Query1[Assigned Engineer] is not used in the visual's fields.

 

MarkLaf_0-1744162461249.png

 

But, it suddenly does not work correctly if we put Query1[Assigned Engineer] in the Legend, as you have highlighted in your posts.

 

MarkLaf_1-1744162582771.png

 

To see what is happening, we can do a simple test. Replace our measure with the following:

Test = 1

 

This is a simple constant, in any part of the visual where a value can be displayed, it will provide a 1. With this, our stacked columns with Query1[Assigned Engineer] in Legend looks like the below.

 

MarkLaf_2-1744162754352.png

It's a little clearer now what the issue is. With Query1[Assigned Engineer] in Legend, it prevents any value from calculating for ( Query1[Week], Query1[Assigned Engineer] ) pairings that do not exist in our table. With the current model, it is impossible to get the result you want.

 

The answer is that you need to put this into a star schema. Or, at the very least, move Engineer into a separate dimension table. Below are some quick steps to do this if you are not already familiar.

 

First, create your dimension table. Click Modeling > New Table and do something like the below:

Engineers = ALL( Query1[Assigned Engineer] )

 

Now, relate your new table to the original:

 

MarkLaf_3-1744163521483.png

 

Your new model should now look something like this.

 

MarkLaf_4-1744163539678.png

 

Next, in your stacked column visual, replace Query1[Assigned Engineer] with Engineers[Assigned Engineer] in the Legend. With this change, the following measure will work. Note it is almost identical except how we calculate the current week at the top.

 

Rolling6WeekTotalRev = 
VAR _thisWk = CALCULATE( MAX( Query1[Week] ), REMOVEFILTERS( Engineers ) )
VAR _filt = FILTER( 
    ALL( Query1[Week] ), 
    Query1[Week] > _thisWk-6 
    && Query1[Week] <= _thisWk 
)
RETURN
CALCULATE( SUM( Query1[rev] ), _filt )

 

MarkLaf_5-1744163732897.png

 

@MarkLaf , Thanks for your help!

What a simple solution to the issue :S

 

I must applaud your thorough explanation of the reason for the issue and the solution. This was really insightful for me and helped me understand how Power BI works.

 

Jan Didrik

 

Jan Didrik

isjoycewang
Super User
Super User

Hi @Jdidrik,

 

Does this work as expected for you? It seems the issue is the engineer legend.

Attached the demo file for reference. Let me know if any questions. 

Rolling6WeekTotalRev = 
VAR CurrentWeek =
    MAX ( 'Table'[Week] )
VAR User = 
    VALUES('Table'[Assigned Engineer])
RETURN
    CALCULATE (
        SUMX (
            FILTER (
                ALL ( 'Table' ),
                'Table'[Week] <= CurrentWeek
                    && 'Table'[Week] > CurrentWeek - 6
                    && 'Table'[Assigned Engineer] IN User
            ),
            'Table'[rev]
        )
    )

 

isjoycewang_0-1744105092055.png

 

Best Regards,

Joyce

Helpful resources

Announcements
March PBI video - carousel

Power BI Monthly Update - March 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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

Top Kudoed Authors