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
Synik
Helper II
Helper II

Doing division based on multiple years

Hello all, I'm running into what I thought was a simple issue, but its turned into a multiple hours event of my DAX code not working. I'll try to keep it simple in what I'm attempting to do. 

I have 2 columns I am attempting to divide, for simplicity, I'll just call them A and B. 
I have another date column with year in it. Year is very simple, it has 2021 through 2018. 

What I'm trying to do in pseudocode is add up everything from A and B for only 2020-2018 and then divide A by B. 

Stuff I have tried: 

  • I have tried CALCULATE, and this works great when I do it for 1 year, but not all 3. 
  • My years need to be dynamic, so I have been using YEAR(NOW())-1, -2, and -3 for 2020-2018, respectively. 
  • I have tried using FILTER in the 2nd part of calculate along with && to get 3 years in, but that didn't work. 

I'm kinda lost at this point. Normally when I do a CALCULATE its on a single filter, or multiple filters from multiple places, not the same filter with different choices. 

Let me know if I can help by giving any additional information. I appreciate any guidence on this, thank you! 

1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

Have you tried a measure expression like this?  This should work in a card.  If you use it in a table/matrix/other visual, you may need to add other terms to the CALCULATE.

 

A divided by B Prev 3 yrs =
VAR thisyear =
    YEAR ( TODAY ()
VAR result =
    CALCULATE (
        DIVIDE ( SUM ( Table[A] ), SUM ( Table[B] ),
        Table[Year] >= thisyear - 3
            && Table[Year] <= thisyear - 1
    )
RETURN
    result

 

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

2 REPLIES 2
mahoneypat
Employee
Employee

Have you tried a measure expression like this?  This should work in a card.  If you use it in a table/matrix/other visual, you may need to add other terms to the CALCULATE.

 

A divided by B Prev 3 yrs =
VAR thisyear =
    YEAR ( TODAY ()
VAR result =
    CALCULATE (
        DIVIDE ( SUM ( Table[A] ), SUM ( Table[B] ),
        Table[Year] >= thisyear - 3
            && Table[Year] <= thisyear - 1
    )
RETURN
    result

 

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


Thank you @mahoneypat 

This logic worked well enough to allow me to continue to troubleshoot further. I am indeed using this in a table. The result returns slightly skrewed numbers than I would expect, but this isn't a problem with the DAX logic. 

So close, yet so far! 

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.