Power BI: cumulative total values

I will explain different ways to do a cumulative value including the WINDOW function because based on the database, one will work better than the others. This is an example of the cumulative total result:

power bi

I have this original data in Table1:

power bi

The “opened” column has duplicate values so I have to extract the unique ones but first, I will grouped the date by month:

power bi power bi

Using this “month” column, I will export the unique values by creating a new DAX table:

power bi power bi

With this new table, I will add a “year” column using this formula:

power bi

I will count all incidents per month using this formula:

CALCULATE(COUNT('table1'[argument1]),FILTER('table1','table1'[argument2]='Table2'[argument2]))

power bi

NOTE:

  • Replace “table” and “argument” by yours
  • I can use COUNTROWS instead of COUNT

This formula will do the cumulative sum:

CALCULATE(SUM('table'[argument1]),ALL('table'),'table'[argument2]<=EARLIER('table'[argument2]))

power bi

To do the cumulative by year, I will use this formula:

CALCULATE(SUM('table'[argument1]),ALL('table'),'table'[argument2]<=EARLIER('table'[argument2]) && 'table'[argument3]=EARLIER('table'[argument3]))

power bi

To show only the last year:

IF('table'[argument3]=YEAR(NOW())-1,CALCULATE(SUM('table'[argument1]),ALL('table'),'table'[argument2]<=EARLIER('table'[argument2]) && 'table'[argument3]=EARLIER('table'[argument3])))

power bi

To show only the current year:

IF('table'[argument3]=YEAR(NOW()),CALCULATE(SUM('table'[argument1]),ALL('table'),'table'[argument2]<=EARLIER('table'[argument2]) && 'table'[argument3]=EARLIER('table'[argument3])))

power bi
power bi

Instead to use calculated columns, I can get the same result by using only measures. In this case, I will only use my original table Table1. I will add a “year” column using the same method as I did for the “month” one:

power bi

The measure for the continuous cumulative sum:

CALCULATE(COUNT('table'[argument1]),FILTER(ALL('table'),'table'[argument2]<=MAX('table'[argument2])))

power bi
power bi

The measure by year:

CALCULATE(COUNT('table'[argument1]),FILTER(ALL('table'),'table'[argument2]<=MAX('table'[argument2]) && 'table'[argument3]=MAX('table'[argument3])))

power bi
power bi

The measure for the last year:

CALCULATE(CALCULATE(COUNT('table'[argument1]),FILTER(ALL('table'),'table'[argument2]<=MAX('table'[argument2]) && 'table'[argument3]=MAX('table'[argument3]))),YEAR('table'[argument3])=YEAR(NOW())-1)

power bi

The measure for the current year:

CALCULATE(CALCULATE(COUNT('table'[argument1]),FILTER(ALL('table'),'table'[argument2]<=MAX('table'[argument2]) && 'table'[argument3]=MAX('table'[argument3]))),YEAR('table'[argument3])=YEAR(NOW()))

power bi
power bi

To layer the last and current years for a comparison, I will need to add a new column:

power bi

And for the visual, I will use this new column then sorting the axis by ascending:

power bi

Before to close this topic, I will talk about the WINDOW function. Based on my data, I will use one of those 2 formulas to create a measure:

  • If my data has a column with numbers like the new table:
    CALCULATE(SUM('table'[argument1]),WINDOW(1,ABS,0,REL,ALL('table'),ORDERBY('table'[argument2])))
    power bi
  • If my data hasn’t this column like the original table:
    CALCULATE(COUNT('table'[argument1]),WINDOW(1,ABS,0,REL,ALL('table'),ORDERBY('table'[argument2])))
    power bi

The only difference is only for the “argument1”, one uses SUM and the other COUNT so for my following explanations, I will just use the COUNT formula to not repeat the same thing.

IMPORTANT: the WINDOW function works better with an original data with unique values. In my example, my original data Table1 has duplicate values including the new table because it has been created from it by excluding them.

So putting the formula, I will get a duplicate row error (I will get the same thing with the new table using SUM):

power bi

As I said before, if the original data has unique values, this error will not appear. There are 2 options to solve it:

  • Option 1: replace ALL('table') by DISTINCT(ALLSELECTED('table'))
    CALCULATE(COUNT('table'[argument1]),WINDOW(1,ABS,0,REL,DISTINCT(ALLSELECTED('table')),
    ORDERBY('table'[argument2])))
    power bi
  • Option 2: add MATCHBY
    CALCULATE(COUNT('table'[argument1]),WINDOW(1,ABS,0,REL,ALL('table'),ORDERBY('table'[argument2]),,,
    MATCHBY('table'[argument2])))
    power bi

NOTE:

  • In spite that the duplicate error disappears, MATCHBY may not work in visual, particularly with COUNT
  • DISTINCT will take longer for a huge data

Now that I explain how to resolve the duplicate errors, for the following explanations, let’s imagine that my table has unique values and no duplicates. For the measure by year, I will have to add PARTITIONBY:

CALCULATE(COUNT('table'[argument1]),WINDOW(1,ABS,0,REL,ALL('table'),ORDERBY('table'[argument2]),,
PARTITIONBY('table'[argument3])))

power bi

NOTE: PARTITIONBY is just before MATCHBY

power bi

The measure for the last year:

CALCULATE(CALCULATE(COUNT('table'[argument1]),YEAR('table'[argument2])=YEAR(NOW())-1),
WINDOW(1,ABS,0,REL,ALL('table'),ORDERBY('table'[argument3]),,PARTITIONBY('table'[argument4])))

power bi

The measure for the current year:

CALCULATE(CALCULATE(COUNT('table'[argument1]),YEAR('table'[argument2])=YEAR(NOW())),
WINDOW(1,ABS,0,REL,ALL('table'),ORDERBY('table'[argument3]),,PARTITIONBY('table'[argument4])))

power bi

NOTE:

  • I don’t use the “month” column to reference the year because I need the original column which is “opened”
  • Some functions like ALL, USERELATIONSHIP, etc. work better after the WINDOW function

For ORDERBY, PARTITIONBY and MATCHBY, I can add other criteria by adding a coma just after the argument, for instance: ORDERBY('table'[argument2],new criteria1,new criteria2,etc)

Interesting Management