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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Need help in DAX

Hi Experts,

I need help with DAX. I've date dimension (Date) and financial fact table (Date, Revenue)
I calculating the last 12-month revenue based on month selection (Both slicers are single selection).
The requirement is to calculate the difference of each month from the previous month.
Below is Last 12-month measure that I'm using it's working fine

 

 

 

 

 

Revenue (last n months) := 
VAR MaxFactDate =
    CALCULATE ( MAX ( FactFinancial[Date] ), ALL ( 'DimDate' ) )
VAR FDate =
    ENDOFMONTH ( 'DimDate'[Date] )
VAR Edate =
    EDATE ( FDate, - 12 )
RETURN
    IF (
        MaxFactDate < MAX ( 'DimDate'[Date] )
            && MaxFactDate >= Edate,
        CALCULATE ( [Total Revenues], ALL ( 'DimDate' ) )
    )

 

 

 

 

 

I want to get previous month total revenue at each month in Last 12 months line chart. I've tried below DAX but no success

 

 

 

 

Revenue Last 12 PM := 
CALCULATE([Revenue (last n months)], PARALLELPERIOD('DimDate'[Date], -1, MONTH)


Revenue Last 12 PM := 
CALCULATE([Revenue (last n months)], PREVIOUSMONTH('DimDate'[Date])

 

 

 

 


both are not working as I'm expecting, please help.

Ask Question.png

 

 

Here is another try with no success.

 

 

 

Revenue (last n months) = 
CALCULATE (
    [Total Revenues],
    DATESINPERIOD (
        'DimDate'[Date],
        MAX ( 'DimDate'[Date] ),
        -12,
        MONTH
    )
)
---------------------------------
Revenue Last 12 PM = 
CALCULATE (
    [Total Revenues],
    DATESINPERIOD (
        'DimDate'[Date],
        EDATE ( MAX ( 'DimDate'[Date] ), -1 ),
        -12,
        MONTH
    )
)

 

 

 

Solution.png

 

 

 

 

PBIX file

https://www.dropbox.com/s/fy5olrgt7wgd9nq/Finance.pbix?dl=0

1 ACCEPTED SOLUTION

Ok.  I think I got it working.  Here is what I did.

 

1.  Used the MonthYear column from your DimDate table in the slicer (optional but one click instead of two)

2.  Added a MonthIndex column to your FactFinancial table (so I could easily do prev month calculation w/o Time Intelligence)

MonthIndex = Year(FactFinancial[Date])*12+MONTH(FactFinancial[Date])
3.  Made this Prev Revenue measure
Prev Month Revenue =
VAR maxmonthindex =
MIN ( FactFinancial[MonthIndex] )
RETURN
CALCULATE (
[Total Revenues],
ALL ( DimDate ),
ALL (
FactFinancial[MonthYear],
FactFinancial[MonthYearNo],  //needed since used as Sort By Column
FactFinancial[MonthIndex]
),
FactFinancial[MonthIndex] = maxmonthindex - 1
)
 
4.  Made these measures for Last 12 M and Last 12 M Prev Month
Last 12 M =
CALCULATE (
[Total Revenues],
DATESINPERIOD ( DimDate[Date], MAX ( DimDate[Date] ), -12, MONTH )
)
 
Last 12 M Prev Mon =
CALCULATE (
[Prev Month Revenue],
DATESINPERIOD ( DimDate[Date], MAX ( DimDate[Date] ), -12, MONTH )
)
 
5.  Got this result
last 12 months.png
 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

10 REPLIES 10
mahoneypat
Microsoft Employee
Microsoft Employee

Assuming your DimDate table is Marked As Date Table, you can use Time Intelligence functions to do this.  Here are example measures for your 12 Month rolling total, and the 12 Month rolling total starting from the previous month.

 

Running Total 12 M =
CALCULATE (
    [Total Sales],
    DATESINPERIOD (
        'Date'[Date],
        MAX ( 'Date'[Date] ),
        -12,
        MONTH
    )
)


Running Total 12 M PM =
CALCULATE (
    [Total Sales],
    DATESINPERIOD (
        'Date'[Date],
        EDATE ( MAX ( 'Date'[Date] ), -1 ),
        -12,
        MONTH
    )
)

 

 

FYI that I posted a blog recently with a useful "Sandbox" file to practice doing Time Intelligence functions, and used a MinMaxDates measure to confirm I had the right date ranges for the above.  You can see it here, if interested.

https://community.powerbi.com/t5/Community-Blog/A-Self-Contained-quot-Sandbox-quot-PBIX-File-to-Prac...

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Hi @mahoneypat ,

 

I've tried your given DAX you can see the output is attached in question to keep track of what I've tried. Both Dax giving same output.

I've also attached the PBIX file for your reference.

Thanks

Your pic shows that you are looking at the day granularity in your table.  I assumed you were looking at the month level.  Here are some new expressions that will work at the day level.

 

 

Running Total 12 M =
VAR __thisdate =
    MIN ( 'DimDate'[Date] )
RETURN
    CALCULATE (
        [Total Revenues],
        DATESINPERIOD ( 'DimDate'[Date], __thisdate, -12, MONTH )
    )

Running Total 12 M PM =
VAR __thisdate =
    MIN ( 'DimDate'[Date] )
RETURN
    CALCULATE (
        [Total Revenues],
        DATESINPERIOD ( 'DimDate'[Date], EDATE ( __thisdate, -1 ), -12, MONTH )
    )

 

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

@mahoneypat ,

 

No success same results as of 12 months but now 0.00 comes on April 30, 2019.

2nd Solution.png

 

 

Can u please download my attached power BI file and see where is the problem.

Link: https://www.dropbox.com/s/fy5olrgt7wgd9nq/Finance.pbix?dl=0


Let me clear u I've monthly fact data and I'm creating date table as a calculated table.

 

 

DimDate = 
ADDCOLUMNS (
CALENDAR (MIN(FactFinancial[Date]),MAX(FactFinancial[Date])),"DateYear", FORMAT ( [Date], "YYYY" ),"MonthYear",FORMAT([Date],"MMM YY"))

 

 Thanks

I wasn't able to open the pbix.  It prompted me for credentials.  As I don't have them (appropriately), it could not load the data model.  Is this a Direct Query model?

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

@mahoneypat ,

Yes, I'm using Direct Query. I've just changed to import and upload again. Please check it should work now and let me know.

https://www.dropbox.com/s/fy5olrgt7wgd9nq/Finance.pbix?dl=0

Thanks

Found the issue.  You need to use the Month Year column from your DimDate table in the table visual, not the one from the FactFinancial table.  Once I did that and cleared the slicers, it worked as expected.

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

@mahoneypat ,

I got what you said but this is not my requirement. I've tried this way. Sorry to say, we can't clear slicers since we need to show the last 12 months based on month-year selection. By adding Month Year from DimDate, if I select the single month from slicer it'll give only selected month data then we are losing the last 12-month purpose. It should show last 12 month data based on selected month year from slicer plus previous month data at each month level. 

 

Ok.  I think I got it working.  Here is what I did.

 

1.  Used the MonthYear column from your DimDate table in the slicer (optional but one click instead of two)

2.  Added a MonthIndex column to your FactFinancial table (so I could easily do prev month calculation w/o Time Intelligence)

MonthIndex = Year(FactFinancial[Date])*12+MONTH(FactFinancial[Date])
3.  Made this Prev Revenue measure
Prev Month Revenue =
VAR maxmonthindex =
MIN ( FactFinancial[MonthIndex] )
RETURN
CALCULATE (
[Total Revenues],
ALL ( DimDate ),
ALL (
FactFinancial[MonthYear],
FactFinancial[MonthYearNo],  //needed since used as Sort By Column
FactFinancial[MonthIndex]
),
FactFinancial[MonthIndex] = maxmonthindex - 1
)
 
4.  Made these measures for Last 12 M and Last 12 M Prev Month
Last 12 M =
CALCULATE (
[Total Revenues],
DATESINPERIOD ( DimDate[Date], MAX ( DimDate[Date] ), -12, MONTH )
)
 
Last 12 M Prev Mon =
CALCULATE (
[Prev Month Revenue],
DATESINPERIOD ( DimDate[Date], MAX ( DimDate[Date] ), -12, MONTH )
)
 
5.  Got this result
last 12 months.png
 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Great work, if possible please share PBIX file

also can you please look into this issue

https://community.powerbi.com/t5/Desktop/Calculate-revenue-from-last-3-to-6-month-based-on-selection...

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Solution Authors