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

  • End-User Filtered Export capabilities now available February 13, 2019
    For the third straight week, we’re delivering on the item I’ve heard more end-user feedback around than any other we’d promised to deliver in the October blogpost.  At long last, you and your users will be able to export their Power BI reports to either PDF or PowerPoint and have their selections on the screen […]
  • Incremental refresh & query folding February 13, 2019
    As part of our strategy to converge enterprise and self-service BI on Power BI as a single platform, we announced the public preview of incremental refresh last summer. While there is still more work to be done to get it to general availability, we have seen strong uptake of incremental refresh. Incremental refresh is a […]
  • Microsoft a Leader in Gartner’s Magic Quadrant for Analytics and BI Platforms for 12 consecutive years February 13, 2019
      We’re very grateful to our customers, our community members, and our partners for making Power BI what it is today.   Thank you.   The Power BI Team       Get the 2019 Gartner’s Magic Quadrant for Analytics and Business Intelligence Platforms report* to learn more.             *This graphic […]