Power BI: calculate values per month

To calculate a value based on month, the best is to create a calendar table but it is not mandatory based on your data. Let’s take an example, I have this simple data:

power bi

To know how many incidents by month, I will create a column with this formula:

CALCULATE(COUNT('table'[argument1]),'table'[argument2]='table'[argument2])

power bi

NOTE: change “table” and “argument” by yours

I will create a table visual (left picture) and for “opened”, I will just keep “month” (right picture):

power bi power bi

This is the result:

power bi

Doing the same thing for “closed” (left picture) and to remove the “blank” month, in “filters”, exclude it:

power bi power bi power bi

To get all months, I will create a measure with this formula and formatting it as “whole number”:

IF(ISBLANK(COUNT('table'[argument1])),"",COUNT('table'[argument1]))

power bi

NOTE: to show 0 instead of blank, change "" by 0

power bi
Opened Closed Opened Closed
power bi power bi power bi power bi

Now, in some cases, it is best to use a calendar table instead to use those month columns but before that, I will need to group the data by day, month, etc. based on what I want. As you can see, for instance, the “opened” columns have multiple same values:

  • INC001, INC004, INC005 and INC006 have same month
  • INC004 and INC005 have same date
  • Etc.

To group them, select “opened” then click on “data groups -> new data groups”:

power bi

In the popup:

  • In “name”, put whatever you want or let it be
  • In “bin size”, put 1 and change “days” by “months”
power bi

NOTE:

  • Since I want to calculate by month, I select “months” to group them monthly but if I want to calculate by day, by year, etc., I have to select the corresponding one
  • To group the data weekly and since there is no “week” option, I have to put like that:
    power bi
  • As you have understood, you will have to define the correct number in “bin size” and its type to group the data correctly based on what kind of result you want
power bi

Now, I will create a monthly calendar table by clicking on “table tools -> new table”:

power bi

Then put this formula:


var FullCalendar = ADDCOLUMNS(CALENDAR(YEAR(NOW())&"/1/1",YEAR(NOW())&"/12/31"),"calendar",FORMAT([Date],"MMMM YYYY"))
return
SUMMARIZE(FullCalendar,[calendar])
              
power bi

NOTE: for more options, read Power BI: create an automatically updated calendar

Select the “calendar” column then in “data type”, change “text” to “date”:

power bi power bi

I will create a relationship by clicking on “manage relationships”:

power bi power bi

Once done, I can use this calendar instead of the “opened” column. Take note that for a better data analysis, it is best to have different calendar tables, for instance, I will create another one for the “closed” column to not use the same as “opened”.

Interesting Topics