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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
topgun_ninja
New Member

How to get last year's same period sales on the same axis for the line graph?

Hi, I was trying to come up with a DAX code to return me the last 12 months' sales from the max dashboard's slicer date. I came up with the following code: 

 

mo_months_to_show =
Var MAX_DATE = MAX('Time'[Date])
Var PREVIOUS_DATES = DATESINPERIOD('calender_months_display'[Date],MAX_DATE,-12,MONTH)
Var RESULT = CALCULATE([Value - Net Sales],REMOVEFILTERS('Time'),KEEPFILTERS(PREVIOUS_DATES),USERELATIONSHIP('Time'[Date],calender_months_display[Date]))
Return RESULT

 

 

 
I want to write another code to show me the same period as the above sales but last year. I also want to plot the line graphs side by side. So far I have written the following code: 
 
mo_months_to_show_prev =
Var MAX_DATE = CALCULATE( MAX('Time'[Date]), DATEADD('Time'[Date], -12, MONTH) )
Var PREVIOUS_DATES = DATESINPERIOD('calender_months_display'[Date],MAX_DATE,-12,MONTH)
Var RESULT = CALCULATE([Value - Net Sales],REMOVEFILTERS('Time'),KEEPFILTERS(PREVIOUS_DATES),USERELATIONSHIP('Time'[Date],calender_months_display[Date]))
Return RESULT​

 

However, this code shows the line graph but trailing. I want to show the line side by side.

topgun_ninja_0-1670485029020.png

 

 

I have also tried another code:

 

CALCULATE([mo_months_to_show],SAMEPERIODLASTYEAR('Time'[Date]))

 

 

But I get the same graph result.

I want both lines overlapped.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @topgun_ninja ,

Please try below steps, i create a sample for you:
1. below is my test table

Table:

vbinbinyumsft_0-1670572489763.png

Table 2:

create with below dax formula:

Table 2 =
ADDCOLUMNS (
    CALENDAR ( FIRSTDATE ( 'Table'[Date] ), LASTDATE ( 'Table'[Date] ) ),
    "YYYY-MMM",
        FORMAT ( [Date], "mmm" ) & " "
            & YEAR ( [Date] )
)

vbinbinyumsft_1-1670572581385.png

Model:

vbinbinyumsft_2-1670572735566.png

 

2. create measure with below dax formula

Current Year= SUM('Table'[Sale])
Last Year =
VAR cur_ym =
    SELECTEDVALUE ( 'Table 2'[YYYY-MMM] )
VAR cur_year =
    INT ( RIGHT ( cur_ym, 4 ) )
VAR _month =
    CALCULATE ( MONTH ( MAX ( 'Table 2'[Date] ) ), 'Table 2'[YYYY-MMM] = cur_ym )
RETURN
    CALCULATE (
        [Measure],
        YEAR ( 'Table 2'[Date] ) = cur_year - 1
            && MONTH ( 'Table 2'[Date] ) = _month
    )

3. add line chart with field and measure, and apply some filter

vbinbinyumsft_3-1670572933087.png

Please refer the attached .pbix file.

 

Best regards,
Community Support Team_Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi @topgun_ninja ,

Please try below steps, i create a sample for you:
1. below is my test table

Table:

vbinbinyumsft_0-1670572489763.png

Table 2:

create with below dax formula:

Table 2 =
ADDCOLUMNS (
    CALENDAR ( FIRSTDATE ( 'Table'[Date] ), LASTDATE ( 'Table'[Date] ) ),
    "YYYY-MMM",
        FORMAT ( [Date], "mmm" ) & " "
            & YEAR ( [Date] )
)

vbinbinyumsft_1-1670572581385.png

Model:

vbinbinyumsft_2-1670572735566.png

 

2. create measure with below dax formula

Current Year= SUM('Table'[Sale])
Last Year =
VAR cur_ym =
    SELECTEDVALUE ( 'Table 2'[YYYY-MMM] )
VAR cur_year =
    INT ( RIGHT ( cur_ym, 4 ) )
VAR _month =
    CALCULATE ( MONTH ( MAX ( 'Table 2'[Date] ) ), 'Table 2'[YYYY-MMM] = cur_ym )
RETURN
    CALCULATE (
        [Measure],
        YEAR ( 'Table 2'[Date] ) = cur_year - 1
            && MONTH ( 'Table 2'[Date] ) = _month
    )

3. add line chart with field and measure, and apply some filter

vbinbinyumsft_3-1670572933087.png

Please refer the attached .pbix file.

 

Best regards,
Community Support Team_Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Mahesh0016
Super User
Super User

can you give a expected result spanshot

 

This is my expected result snapshot:

topgun_ninja_0-1670493553781.png

 

Instead, I get this:

topgun_ninja_1-1670493576641.png

 

Mahesh0016
Super User
Super User

Mahesh0016
Super User
Super User

Mahesh0016_0-1670488186115.png

Hello @topgun_ninja ,

i hope it is helpful for you.
Thank you!

I have already tried this method. It doesn't work.

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.