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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
sbatra
New Member

Calculation involving multiple facts and trailing 12 months

Hello community members,


I'm building a power bi report to include a few key metrics for a business. One of the metric is Gross Margin Amount (GMA). The report has a slicer, through which the user can select an year and a month (Dimdate table in the model). Here is the image of the model.
 

 

sbatra_0-1734167630889.png

 

sbatra_1-1734167643428.png

 

sbatra_2-1734167649842.png

 

sbatra_3-1734167656111.png

 

 

I have a table fact billed revenue with column billed amount and bill date (amoung other attributes). I have another table fact unbilled revenue with columns unbilled amount and visit date and another table fact cost with columns total cost and transaction date (among other columns).  

 

Requirement: - 

I need to create a measure to calculate gross margin amount (GMA), for the trailing 12 months based upon the year, month selection in the slider, by adding unbilled revenue (fact unbilled revenue), billed revenue (fact billed revenue) and then subtract total cost (fact cost) to get to the final amount. 

 

Here is the DAX formula that I have created. 

Trailing 13 Months GMA v1 =  

VAR SelectedDate = COALESCE(SELECTEDVALUE('DimDate'[Date]), MAX('DimDate'[Date])) 

 

RETURN 

CALCULATE( 

    SUM('Fact Billed Revenue'[BilledAmount]) + 

    SUM('Fact UnBilled Revenue'[UnBilledAmount]) - 

    SUM('Fact Cost'[TotalPayrollCost]), 

    DATESINPERIOD('DimDate'[Date], SelectedDate, -12, MONTH), 

    ALLEXCEPT('DimDate', 'DimDate'[Date]) -- Keep only the date context 

 

I then put the measure “Trailing 13 Months GMA v1” in the table visual and then pulled in the year and month from the date dimension. 

 

 

sbatra_4-1734167772489.png

 

Expectation: Correct GMA amount for the trailing 12 months i.e. June 2023 to May 2024 

However, as you can see, something is not right here. Not only do I see the same GMA amount for each trailing month (suggesting the slicing operation is not working at all), I also see the unexpected date range on the x-axis.  

 

As I’m analyzing this issue, I would appreciate any pointers on  

  • what could be wrong here?  mostly like the DAX measure needs some changes or something else is going on here. 
  • What tool or method I can use for debugging, so that I can see intermediate results, step through calculations and filters interplay. 

Thank you for your inputs! 

6 REPLIES 6
Ashish_Mathur
Super User
Super User

Hi,

Try this measure pattern

=calculate(SUM('Fact Billed Revenue'[BilledAmount]) + SUM('Fact UnBilled Revenue'[UnBilledAmount]) -SUM('Fact Cost'[TotalPayrollCost])

,datesbetween(calendar[date],edate(min(calendar[date]),-12),max(calendar[date])))

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi @Ashish_Mathur, Thanks for your response.

 

The suggestion didn't work. A peculier situation here (as it appears to me atleast) is we have a situation where we need to remove the filter context coming in from the slider (date dim) and also retain it when with do GMA calculation in the measure, so that we can slice the three fact tables when we put date dim in the x-axis along with the GMA measure. Any further thoughts?

You are welcoe.  No further suggestions unless i see the file.  Show the problem and the expected result very clearly.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Please post a sanitized copy your pbix so we will know what's going on inside. Or we will be simply trying to shoot a target without properly  aiming at it.










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


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
danextian
Super User
Super User

Hi @sbatra 

Remove this line: ALLEXCEPT('DimDate', 'DimDate'[Date]) -- Keep only the date context
Mark a date table as a date table. Doing so automatically applies REMOVEFILTERS on the table when using time intelligence functions.

danextian_0-1734169247239.png

danextian_1-1734169367396.png

If you don't mark a date table as one, make sure to use the correct filter modifier in the measure.

Sales L12M = 
CALCULATE (
    [Sales],
    DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -12, MONTH ),
    REMOVEFILTERS ( 'Date' )
)

 

 










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


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

@danextian Thanks for the response.

I seem to understand what you mean here. I used the measure based approach (since the other approach is a bigger impact on the existing report). Unfortunately, this didn't work. Would you have any further thoughts?

 

sbatra_1-1734193638300.png


Revised Measure (FYI, there is no change in the calcuation if I keep or remove REMOVEFILTERS statement)

 

Trailing 13 Months GMA =
CALCULATE(
    SUM('Fact Billed Revenue'[BilledAmount]) + SUM('Fact UnBilled Revenue'[UnBilledAmount]) - SUM('Fact Cost'[TotalPayrollCost]),
    DATESINPERIOD(
        DimDate[Date],
        Max('DimDate'[Date]),
        -12,
        MONTH
    ),
       REMOVEFILTERS ( 'DimDate' )
)

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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