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

  • Visualizing SharePoint lists now available to all users July 22, 2021
    A couple of months ago, we launched a new Power BI integration for visualizing SharePoint lists for all Targeted release users. We’re now excited to announce that the experience has rolled out to Standard release users and is now accessible to everyone!
  • Behind the scenes: Running the Power BI service July 22, 2021
    I’m excited to share a new whitepaper that describes the Power BI team’s approach to maintaining a reliable, performant, and scalable service for our customers.
  • Power BI July 2021 Feature Summary July 21, 2021
    Welcome to the July update! This month we are making small multiples generally available, as well as the new model view and sensitivity labels in Desktop. Also, we have a new preview for streaming dataflows. There is much more, so read on!