DAX – Properly calculating YTD

This seems like it should be straight forward, but it’s not. The documentation on the TOTALYTD function is not great.

Suppose I have a fact table with monthly revenue, and a standard date dimension.  The date dimension has a date value, of type date, for every day in the period, no holes.  Two formula’s to calculate the running  total of that data should be;

Revenue Calc 1 = TOTALYTD(SUM(Finance[actual revenue]),’Date'[FullDate])

Revenue Calc 2 = CALCULATE(SUM(Finance[actual revenue]),DATESYTD(‘Date'[FullDate]),ALL(‘Date’))

These two formulas are the same, however, the second one offers a little more flexibility, as multiple filters can be used.   I accidentally discovered something interesting about the difference in these two.  I was working on a file where I’d imported monthly revenue, with one line per month.  I had a date dimension with a relationship between the two.  Since I had only one revenue item per month, and thus one date, I could then do something stupid like this:

powerBI bad relationship

So in other words, every revenue item in the finance table would have it’s own context when TOTALYTD was evaluated, I’d get the monthly total, rather than the YTD as follows;

powerBI bad relationships revenue

All the while, the second calculation would work correctly as it changes the context in which the expression is evaluated.  In other words, it doesn’t care if you’re made mistakes in the relationships.

In other words, the cardinality of the relationship will define how TOTALYTD works.  Typically, the engine wouldn’t allow you do to this, as you’d have multiple dates in the fact table.  However, this was a good exercise in displaying the difference in context.

Here is the powerBI file if you want to see for yourself.




Far better an approximate answer to the right question, which is often vague, than the exact answer to the wrong question, which can always be made precise. -John Tukey
The plural of anecdote is not data. - John Myles White

Recent Posts

RSS PowerBI blog

  • Using XMLA endpoints to change data sources in a Power BI dataset April 19, 2021
    The purpose of this article is to provide Power BI developers with a deeper understanding of the dependencies when updating data sources in a Power BI dataset through XMLA endpoints. Most importantly, Power BI maintains security-related data source information such as credentials and privacy settings outside the dataset, whereas Azure Analysis Services and SQL Server […]
  • Power BI April 2021 Feature Summary April 16, 2021
    Welcome to the first Power BI feature summary of the Spring! In this update we have an exciting new preview for the Power Automate visual! Also, the ongoing previews (Small Multiples and DirectQuery for Power BI datasets and Azure Analysis Services) are getting some new features. Next to that, we are presenting huge improvements to […]
  • Announcing the new Power Automate visual for Power BI reports April 15, 2021
    Empower your end-users to go from insight to action. With the new Power Automate visual, end-users can run an automated flow all within a Power BI report. Furthermore, the executed flow can be data contextual, meaning that the Flow inputs can be dynamic based on the filters set by the end-user.