Power BI: calculate values with one calendar

When I analyze data, some tables may have multiple date columns, for instance, open date, update date, close date, etc. so to manage values based on dates, the simpler way is to create a calendar for each of them, at the end, I may have 3, 4 or more calendar tables but the best way to do it, it is to have one unique and single calendar for all of them. Let´s take an example, I have this table:

power bi

I want to count how many tickets per month, for that, I will have to regroup the date. I will select the “opened” column then click on “data groups -> new data groups”:

power bi

About the fields:

  • Name: change it if you want
  • Bin size: put the number to group then select the type
power bi

For instance, I put 1 to group all date in 1 month. For a quarter, I will put “3 months”. To count by day, I will put “1 days”, etc. I will do the same thing for the “closed” column:

power bi

I will create a monthly calendar (for more information, read Power BI: create an automatically updated calendar):

power bi

I will create 2 relationships, one active and one inactive:

power bi power bi

NOTE: no matter how many date columns, only 1 should be active and the others should be inactive.

Now, for each formula that should involve the “inactive” relationship, I have to include the USERELATIONSHIP function:

CALCULATE(formula,USERELATIONSHIP('table'[argument],'calendar'[argument]))

For instance, to count how many tickets for the “closed”:

power bi

NOTE: as said above, for “opened”, since the relationship is “active”, there is no need to use the function

Let´s create a chart to see the result:

power bi power bi

Important thing to know, the USERELATIONSHIP function only works for formula but it is not working for filtering. For instance, I will create a table, a slicer and 2 cards; with the chart, I select April:

power bi power bi

As we can see, only in the table, “closed” is not showing correctly (2 results instead of 3). Even if I use the slicer, I will get the same result. The reason is that the chart and the slicer is using the calendar which has an “active” relationship with “opened” so the only solution is to create another calendar for “closed” and repeat the same process as above but for “closed” instead of “opened”:

  • The relationship:
    power bi
  • The formula of “measure 2”:
    power bi

The result:

power bi power bi

Now, in the table, “closed” is showing correctly but not “opened” so to display both versions in 1 single page, use bookmarks, for more information, read Power BI: bookmarks or pages ?

Interesting Topics