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
Gauravi
New Member

DAX Calculation

The following is my data model:

1. Contracts table with the following fields – ContractId, SalesRepId, CreatedDate, ContractAmount, CancelledDate
2. Date table with basic date fields – date, month, quarter. Contracts table and Date table are joined on CreatedDate
3. SalesPerson – SalesRepId, SalesRepName

I need a DAX calculated to show the total contract amount created last year but cancelled this year. For example, if a user selects 2021, the calculation should show the sum of contract amount created in 2020 but cancelled in 2021. Similarly, if a user selects Jan 2021, the calculation should show the sum of contract amount created in Jan 2020 but cancelled in Jan 2021.

Any idea how to do this?

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @Gauravi ,

 

Try this:

Measure =
VAR SelectedDate_ =
    VALUES ( 'Date'[Date] )
VAR SamePeriodLastYear_ =
    CALCULATETABLE ( VALUES ( 'Date'[Date] ), SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
RETURN
    CALCULATE (
        SUM ( Contracts[ContractAmount] ),
        ALL ( 'Date' ),
        Contracts[CreatedDate] IN SamePeriodLastYear_,
        Contracts[CancelledDate] IN SelectedDate_
    )

Icey_4-1644562628171.png

 

Icey_2-1644562563647.png

Icey_3-1644562591859.png

 

 

 

 

For more details, please check the attached .pbix file.

 

 

Best Regards,

Icey

 

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

5 REPLIES 5
Icey
Community Support
Community Support

Hi @Gauravi ,

 

Try this:

Measure =
VAR SelectedDate_ =
    VALUES ( 'Date'[Date] )
VAR SamePeriodLastYear_ =
    CALCULATETABLE ( VALUES ( 'Date'[Date] ), SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
RETURN
    CALCULATE (
        SUM ( Contracts[ContractAmount] ),
        ALL ( 'Date' ),
        Contracts[CreatedDate] IN SamePeriodLastYear_,
        Contracts[CancelledDate] IN SelectedDate_
    )

Icey_4-1644562628171.png

 

Icey_2-1644562563647.png

Icey_3-1644562591859.png

 

 

 

 

For more details, please check the attached .pbix file.

 

 

Best Regards,

Icey

 

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

Samarth_18
Community Champion
Community Champion

Hi @Gauravi ,

You can try any of the below measure:-

Contract_Sales =
VAR selected_year =
    SELECTEDVALUE ( date[year] )
RETURN
    CALCULATE (
        SUM ( Contract[ContractAmount] ),
        CALCULATETABLE (
            VALUES ( Contract[ContractAmount] ),
            YEAR ( Contract[CreatedDate] ) = selected_year - 1
        ),
        CALCULATETABLE (
            VALUES ( Contract[ContractAmount] ),
            YEAR ( Contract[CancelledDate] ) = selected_year
        )
    )
Contract_sales =
VAR selected_year =
    SELECTEDVALUE ( date[year] )
RETURN
    CALCULATE (
        SUM ( Contract[ContractAmount] ),
        FILTER (
            Contract,
            YEAR ( Contract[CreatedDate] ) = selected_year - 1
                && YEAR ( Contract[CancelledDate] ) = selected_year
        )
    )

 

Thanks,

Samarth

 

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

The calculation is not working because my contracts table is linked/joined to the date dimension. Is there a way to make it work with the join to the date table?

Gauravi
New Member

The calculation returns blank. If I just use one condition (contract date or canceled date), it shows the correct value. But if I use both the conditions, it returns blank

amitchandak
Super User
Super User

@Gauravi , Try a measure like

 

Year behind Sales = CALCULATE(CALCULATE(SUM(Contract[ContractAmount]),dateadd('Date'[Date],-1,Year)), filter(Contract, year(Contract[Cancelled Date]) = year(max('Date'[Date])))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

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!

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.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.