Forum Discussion

olivere91's avatar
olivere91
Icon for Helper I rankHelper I
3 years ago

GM Dollars Changing Allowances based on Year

Hello,

 

I am trying to switch my Gross Margin Dollars based on the year selected. I want to write a formula that says 'If year is >2022 then return one formula, else return another.  I have this so far. 

 

GM Dollars =
var profitbefore =
    SUMX (
        VALUES ( Item_Sales_Profit[Customer No.] ),
        SWITCH (
            Item_Sales_Profit[Customer No.],
            "customer", Item_Sales_Profit[Margin $],
            [GM $]
        )
    )
var profitafter =
    SUMX (
        VALUES ( Item_Sales_Profit[Customer No.] ),
        SWITCH (
            Item_Sales_Profit[Customer No.],
            "customer", Item_Sales_Profit[Margin $ 2023],
            [GM $]
        )
    )
    var years = CALCULATE(MAX(Dates[Year]),Dates[Year] =2023)
    return
    IF(years=2023,profitafter,profitbefore)

1 Reply

  • Hi , olivere91 

    According to your description, you want to " write a formula that says 'If year is >2022 then return one formula, else return another. ".

    First you need to make sure that the two values you return are correct.
    Then we can use:
    var years = MAX(Dates[Year])


    to get the year selected by the slicer.
    Then we can judge in return:


    return
    IF(years>2022, profit after, profit before)

     

    var years= MAX(Dates[Year])
    
    return
    IF(years>2022,profitafter,profitbefore)

     

     

     

    If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem. (You can also upload you sample .pbix [without sensitive data] to the OneDrive and share with the OneDrive link to me ! )

    Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

     

    Best Regards,

    Aniya Zhang

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly