馃攳
Power BI Show Year-to-Date (YTD) Across Multiple Years - YouTube
Channel: Avi Singh - PowerBIPro
[0]
How can you compare the performance of the current year with that of prior years
[5]
but still have it be a fair "Apples to Apples" comparison
[9]
even though your current year is a partial year.
[12]
Of course, you can do that if you only focus on the completed months.
[16]
So let's say we were in the middle of 2018, we just finished October. The way to do a fair comparison,
[21]
I mean right now, you can see that these numbers are comparing a
[25]
partial year with the full prior year which isn't quite "Apples to Apples".
[29]
What we would like to do really, is to just compare the completed months or the year-to-date.
[35]
Now, this is the end result we're going for and we're gonna show you not one but two ways to get that. Keep watching.
[41]
Hey, my name is avi Singh. I'm a Microsoft MVP and a best-selling power bi author
[45]
And if you would like to become a power bi Pro, make sure to subscribe and click that bell
[50]
So you were notified whenever I go live to answer your power bi questions
[55]
So this question was asked by Sean McDonald from the sunny state of, California
[59]
And he was joining us in one of her live talk power bi shows
[62]
So again, you've seen the question we have the data in this case, and we're pretending that right now. We're in September 2018
[69]
I just wanted to use that so we can kind of show you the scenario a little more accurately. So
[76]
so here again, we have partial data for 2018 and we have full data for
[81]
2017 and
[82]
2016 and the end result we want is to just compare the completed months
[87]
Now, of course, we don't want any you know
[89]
we want all of this to run automatically if
[92]
September is the completed month right now the next month automatically it should pull in the the new month, right?
[98]
So as soon as we have data over here, it should automatically pull in the current year today
[103]
So that's the goal that we're working towards now one note about the calendar
[107]
We are using the ultimate calendar which you can go and find on my channel
[111]
Just go on the channel home page and look under the learn power bi playlist
[114]
But we have changed one parameter to again pretend that we're back in the future and we did that
[120]
To show you kind of the partial year as promised
[124]
We're gonna show you not one, but two solutions do this and we're gonna build it step-by-step at any point of time
[130]
You can click the link shown on this video to download file at the link shown here. So let's get started now first things first
[137]
When you think kind of year-to-date that language was used when Sean McDonald kind of asked that question
[143]
And and you might you might think back to the classic year-to-date
[148]
Formula which looks like this. Let's take a look at this formula really quick and it's a classic pattern. It's coming up right now
[156]
So, you know calculate sales dates here today uses one of the inbuilt time Intelligence functions
[161]
But that is not for this purpose. That one is for giving you cumulative totals across here
[167]
So you can see how dates here date kind of adds this up
[170]
Cumulatively over years and then resets at the beginning of the year and then resets again
[175]
And of course September 2018 in our scenario is a partial year
[180]
That's why getting a flatline at the end and there are other methods to solve that but again
[184]
That's not what we here to solve. We are here to give
[188]
Shawn this result. We're automatically it knows kind of what the completed month is and it's gonna say, yep, you know
[195]
We just completed August so I'm gonna show you data as of August and I'm gonna compare apples to apples
[201]
Now first of all, this there was a decision point here
[205]
and by the way
[205]
power bi data modeling often comes down to your the assumptions you're going in with and the decisions you make so the first decision point
[212]
That I had was do how to decide which is the current month now
[217]
I was tempted to go to my actual data set to say hey
[221]
I have data to August and use that but I I decided not to do that for two very good reasons
[228]
For one in a lot of cases you can have data for future days. In fact at Microsoft
[233]
we would have data for future months because there would be some revenue which would be recognized booked for future months and again,
[241]
They can be lots of other scenarios
[242]
We have data for future dates, but they're not what you're trying to show right now
[246]
And the other reason is that it may not be just one data set you might have multiple data sets and different
[252]
Varying levels of data maybe for one data set we have data to last week for another one
[257]
We have data last month and it all gets really confusing. So I think the right way to do this is to leverage your
[264]
Calendar so let's go back over here
[267]
We have is our calendar is the ultimate calendar again, you can find it on my channel
[272]
Just look for the ultimate calendar playlist and download that
[276]
so here
[278]
In one of the features for the ultimate calendar is the current month offset now idea is really really simple
[284]
So let's take a look at that. So what it does is and again we are pretending here
[289]
So we have set up a parameter in here to pretend that the current date is
[294]
August 30th, 2018 and
[297]
Let's go back to or current offset
[299]
Yeah, so what it's gonna do is it's gonna say that yep that month is the current month
[305]
So it's gonna mark it as zero and anything after that it is gonna mark it as one two, three four
[311]
And of course going back - one - two - three now
[313]
This is an extremely powerful concept lets you do really mind-bogglingly amazing things
[318]
And again watch the ultimate calendar playlist on my channel for that
[324]
Here, you know I just figured I was showing you my channel
[326]
So all the good stuff is right at the top in this learn power bi playlist. There is a tutorial there is ultimate calendar
[334]
There's modeling. Dax and so forth, right?
[336]
So that's where you can find that I so we're gonna use the current month offset to tell us that what the current complete in
[342]
Month is right. So great. So let's gonna let's build it kind of step by step
[346]
So again what we have right now here is just a simple sales measure, right?
[350]
So sum of sales amount and clearly it's not it's not working, right?
[353]
It's giving us these these year and totals which is exactly kind of apples and oranges, right?
[358]
So we're these are oranges which are being plugged in over here. And this is our Apple
[363]
Which is a partial year now 2018 was a ballistic year for adventureworks. They're doing great. You should definitely buy their stock
[370]
I would recommend that
[371]
But no, but yeah I said this is a complete year versus partial year. So that's what we want to change now
[378]
What I like to do is to solve the axe issues is to solve it thinking about
[384]
Like how a human would solve it and this is the tricky part because humans are intuitive
[389]
So you have to break down that thinking step by minut step
[393]
And then all you have to do is just teach those steps to power bi it's easy, right?
[396]
So how would a human calculate it? Well for one?
[401]
How would a human know that which is the current month and again?
[405]
So we decided that so again a human has to know that oh, it's still oddest
[409]
I have to stop at August and so we have to determine that well, how do we do that?
[413]
Well, we do that using as we discuss the current month offset. So let's
[419]
Let's go kind of solve that first
[423]
So I'm gonna write my very first Dax measure and
[426]
This is actually one that we're not gonna be using but I just want to kind of start there as a baby step
[431]
so let's go to the modeling tab over here and hold on one second all
[436]
Right. It was just acting up a little bit
[438]
So let's do a new measure over here and we're gonna say max month again
[444]
Our goal is to be able to tell the model that eight August is the maximum month. So we'll go in over here
[452]
Perfect, and we'll say max month and I'll say try one. Oops
[459]
Try one and what if we just said max of
[465]
Month num, right? How would that work? Now? I know that wouldn't work. But let's just see what happens with this formula
[474]
Alright so max month and what I'm gonna do is I have my special formulas table and we're gonna place it there and
[479]
I'm gonna place it on this matrix and you can see that the max month is returning 12
[483]
So it's just you know, so it's just going to each year and saying oh well, yeah, my max month is 12
[488]
That's not quite what we're looking for
[491]
We want the month num where the current month off tat is zero. So we just want that one specific result
[497]
So let's try this again and we're gonna say new measure
[502]
max month
[508]
So we're gonna try the same thing but we're gonna bring out a magic wand calculator, right
[513]
So if calculate and we're gonna say the same thing month num
[518]
There we go, right but then this is where the magic wand kind of comes in you can specify additional filters or
[525]
functions and the first function we're gonna use is all because we want to clear the calendar filter, even if we are under
[533]
2060 you know
[534]
We don't want to consider that you want to clear all filters on calendar
[538]
So that's the first thing that we do all calendar here and then we're gonna zoom in again, right?
[543]
So we're gonna zoom out all calendar and then with zoom in again to the calendar current month offset is zero
[550]
So let's say curl month offset. Perfect equals zero and that's it
[555]
Now you can you can try it without all and see how it would work. Okay, I didn't need that and
[562]
Let's see the result of this
[565]
So again bingo so 8th August so it goes to the most recent, you know
[570]
It goes to the current amount offset again based on our ultimate calendar table and picks out the month
[575]
Um for that so now we have the month num now all we have to tell power bi is - yeah
[580]
Yeah, you know you have a month
[581]
Um, right buddy, go ahead and when you're calculating the sales only calculate with a month numb up to that month numb, right?
[589]
So we're a month is less than or equal to that maximum simple, right?
[592]
So again think about think break break down how human would solve a kind of step by step and then we're teaching at power bi
[599]
In a step by step as well. So we have a
[601]
ignore the maximum
[602]
try one but we're gonna focus on on the second one this result and use that so I'm gonna say a new measure and
[609]
We'll say let's say sales
[613]
across
[614]
Prior years and I mean days are not using the year-to-date language because it has a very different connotation in
[620]
Power bi generally and it has that built in ear to date functions. So I'm just saying sales across prior years
[625]
I mean if you want you can use the year-to-date function, right? So again, so we're gonna say calculate to actually change my mind
[631]
We're we're gonna start by getting the value off the max month
[636]
So we we're gonna define a variable max month and just reference the max month measure we're doing and and just as a test
[643]
I often like to just return
[645]
Something really quick and just to see that it's working
[648]
Now, of course it is I expect it to work but you know, I like kind of stressful you said okay. It's already there
[653]
So you can see that it has it's returning the same result
[656]
So it's not quite sales yet, but we're really really close to it
[659]
So get into action here and let's see if we can solve this problem. So
[665]
Again, so now I'm going to bring out my magic wand alkylate and again, the logic is simple
[670]
I've already taught the human. I broke it down like what the steps the human would take and I'm teaching power
[675]
We are the same steps and say hey calculate sales and the only thing you have to remember is not to go beyond the max
[681]
Month only go to the max month and I've already figured out what the maximum is. So that one becomes so easy
[686]
So we say calendar month, um is less than equal to
[691]
max month and
[693]
That is that should do it my friends. Let's check the result sales across prior years. It's calculating looks great
[701]
Let's put some formatting in here
[704]
We're gonna make it shine a little bit so currency
[708]
0 and and yeah, you can I'll let you do the math
[712]
but yeah, if you were to add up January through August this would add up to exactly
[718]
4 point 4 1 3 million and same thing for
[722]
2017 now a few things here now for so again
[725]
this is just solution 1 wait till you see the second solution and
[729]
then you can decide which one is easier which one you would prefer a
[734]
Few things here for one. Did you realize that if I hadn't used the variable that it wouldn't work?
[740]
Do you know what error we would get if you do know?
[744]
That Y variable is used and why not using a variable will not work drop in a comment
[748]
Let me know or go ahead and give it a try and let me know what happened
[752]
Right, so so that part is important
[754]
Now the next thing is not I like building step by step Lego block as in Lego block. You don't you don't necessarily
[761]
Need the max month to be a separate variable. In fact, we can define a new version of this
[769]
so we can
[771]
Copy the same measure go over here
[774]
new measure and let's say V 2 V 2 and
[779]
Instead of instead of having to define that in a separate variable
[783]
what we can do is I'm gonna copy that formula from
[786]
my other feet and
[788]
Just put that in right?
[790]
So you saw what we did instead of building it kind of in two variables
[793]
We can choose to do it in a single variable
[796]
That's totally up to you and they both of course would give you the same result
[801]
But this was the solution one, which was kind of a Dax approach of doing it
[805]
There is a totally different way to solve it using
[809]
Leveraging the query editor and adding on to our ultimate calendar table and and that's why
[816]
It is called the Ottoman calendar table because not only is it is it chock full of goodies?
[820]
But it's really easy to customize so for this challenge
[823]
I wanted to take another shot at it and let me just give you kind of the final solution show you what it looks like
[828]
so here
[831]
In the alternate solution what we have is we have a flag in our table which
[838]
Marks, what is year-to-date?
[840]
So let's say let me clear this flag for a second so you can see
[843]
and again
[844]
This is being done in the query editor and what I'll let you do is that I'll let you download the file from this link
[851]
And that way you can examine exactly how that column is being built. In fact, it is not just that column
[857]
I also added a few new things to the ultimate calendar table, which is the not just in year to date flag
[863]
But quarter to date and month to date so it's going to give you a lot more flexibility
[867]
So now so this measure that I'm using right now is actually my sales measure if I'm not wrong. Let's go take a look
[875]
so this is
[877]
Sales measure sum of sales sales man, right that that's it
[881]
That's the extent of Dax on this page, but given the strength of the ultimate calendar table
[887]
What do you need to do so that you get the same apples to apples comparison?
[892]
You need to drag and make a slicer out of that
[896]
That flag here the date field built into your calendar table
[900]
You can just copy paste and use that ultimate calendar and you're gonna click on this one button
[906]
And this is the exact same result we used. Yeah, we got using the Dax approach. So what do you think my friends?
[911]
Who which?
[912]
Approach is better or which one would you prefer and of course there could be other fun steps that you play with you can change
[919]
The measure to only use completed months. So in that you could you're gonna use current melt offset
[924]
It's minus 1 instead of 0 that we used
[926]
You could change the measure to work at a daily level not just in a month level
[930]
So go ahead and try it out. And of course an easy way to get started on that would be
[935]
Just to download the file and go from there
[937]
Hey keep watching more videos and deep learning power bi but if you did enjoy this video
[943]
I would love to hear from you. So leave a comment like subscribe
[947]
All the good stuff while we're on my friends
Most Recent Videos:
You can go back to the homepage right here: Homepage





