Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ryan_mayu
Super User
Super User

How to separate 2018 and 2019 data and shows as two lines in the line chart.

Hi all,

 

Below is my sample data. I create two measures to sum 2018 data and 2019 data.  However, I have some complex KPI, for example, rolling 3 month KPI.

 

Rolling 3 month calculation:

2018 Jan= null (becoz no 2017 Dec and Nov data)

2018 Feb = null (becoz no 2017 Dec data)

2018 Mar= 2018Jan+2018Feb+2018Mar

....

2019 Dec=2019 Oct+2019 Nov+2019 Dec

 

 

dateMonthamountOrder
1/1/2018Jan11
2/1/2018Feb22
3/1/2018Mar33
4/1/2018Apr44
5/1/2018May55
6/1/2018Jun66
7/1/2018Jul77
8/1/2018Aug88
9/1/2018Sep99
10/1/2018Oct1010
11/1/2018Nov1111
12/1/2018Dec1212
1/1/2019Jan131
2/1/2019Feb142
3/1/2019Mar153
4/1/2019Apr164
5/1/2019May175
6/1/2019Jun186
7/1/2019Jul197
8/1/2019Aug208
9/1/2019Sep219
10/1/2019Oct2210
11/1/2019Nov2311
12/1/2019Dec2412

 

Now I want to create a chart like below. Is it possible to do that?

 

Capture.JPG

 

 

Thanks a lot.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




1 ACCEPTED SOLUTION

@Nolock 

 

I worked this out. Acutally, you provided me a very useful idea which is pull the year to legend. I tried this on my end. It works. Thanks for your help on this.

 

Capture.JPG





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

4 REPLIES 4
Nolock
Resident Rockstar
Resident Rockstar

Hi Ryan,

 

I've created 2 measures for "start of the period" and "end of the period" for your rolling 3 months. After that another measure that calculates the rolling sum. If you have the rolling sum, then you just need to create a visualisation where you use a year as legend. And one more thing: You have to sort the column Month by Month Order to be able to display Jan, Feb and so on in the expected order and not alphabetically.

 

Period Start = 
FIRSTDATE(DATESINPERIOD(TestTable[date]; [Period End]; -3; MONTH))
Period End = LASTDATE(TestTable[date])
Rolling Sum = 
CALCULATE(
    SUM(TestTable[amount]);
    DATESBETWEEN(TestTable[date]; [Period Start]; [Period End])
)

Annotation 2019-02-25 105648.jpg

 

@Nolock 

 

I worked this out. Acutally, you provided me a very useful idea which is pull the year to legend. I tried this on my end. It works. Thanks for your help on this.

 

Capture.JPG





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@Nolock 

 

Thanks for your help. However, I think your chart is not correct. Based on my logic, we should not have value for 2018 Jan and Feb.

 

And for example, the value of 2018 May is 5 in your chart. That is only the summary of one month. I think the correct value should be 5+4+3= 12. 

 

Any thoughts on this?

 

Thanks in advance.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @ryan_mayu,

 

I'm sorry I've overseen that you want to remove results when the whole interval isn't there. Maybe there is a more elegant solution for that, but I used a check at the end if the length of the period is the expected length.

And I've also corrected the error you found with May 2018. It worked as a table but after that I converted in a visualisation and didn't checked the value again (the fix was to take all rows in Calculate again).

 

Rolling Sum = 
VAR PeriodLength = 3
VAR PeriodEnd =
    LASTDATE ( TestTable[date] )
VAR PeriodStart =
    FIRSTDATE (
        DATESINPERIOD ( TestTable[date]; PeriodEnd; - PeriodLength; MONTH )
    )
VAR IsPeriodThreeMonths =
    DATEDIFF ( PeriodStart; PeriodEnd; MONTH ) = PeriodLength - 1
VAR CalculateRollingSum =
    CALCULATE (
        SUM ( TestTable[amount] );
        ALL ( TestTable );
        DATESBETWEEN ( TestTable[date]; PeriodStart; PeriodEnd )
    )
VAR Result =
    IF ( IsPeriodThreeMonths; CalculateRollingSum; BLANK () )
RETURN
    Result

Annotation 2019-02-25 165038.jpg

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.