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

Having a DAX issue with calculating correct total

 

Hi all,

 

One of my clients has given me a table with their spend data. This table holds spend purchasing document data and the purchase orders they have been booked on. Below you will find a sample of this data

 

Sample order dataSample order data

For the client it is very important to know the SPEND VALUE for the orders that were recorded AFTER the FIRST document was recorded. Furthermore, they want to limit this value to the orders that were created in the context year.

 

As you can see in the above table I've already added a column showing which order were created before the first document was created (TRUE) and which were created after the first document was created (FALSE).

 

Below are the steps I've already taken:

 

  1. Created a measure for total spend (BTW, I've added a calendar table called Calendar).
    Total Spend = 
    CALCULATE (
        SUM ( 'Spend'[Amount] ),
        USERELATIONSHIP ( 'Spend'[Document date], 'Calendar'[Date] )
    )
    Below you will find the output.
    Output #1.PNG
  2. Created a measure filtering out the orders that were created after the first document was created.
    Filter #1 = 
    CALCULATE (
        [Total Spend],
        FILTER ( Spend, Spend[Order before document] = FALSE () )
    )
    Below you will find the output.
    Output #2.PNG
  3. Created a measure limiting the filtered amount to the orders created in the context year.
    Filter #2 = 
    CALCULATE (
        [Filter #1],
        DATESBETWEEN (
            Spend[Order date],
            MIN ( 'Calendar'[Date] ),
            MAX ( 'Calendar'[Date] )
        )
    )
    Below you will find the output.
    Output #3.PNG

As you can see the last measure "Filter #2" does not give the correct total. This is because we have a double filter on dates. First on document date (Total Spend measure) and later on on order date (Filter #2). On the total line both of these contexts are dropped, creating an incorrect total.

 

My question is obvious. What DAX code will calculate the correct total value? I'm thinking about using ISFILTERED, but still can't come up with any working code.

 

Can you help me?

 

Regards,

Erwin

 

 

 

1 ACCEPTED SOLUTION

Hi Greg,

 

I already knew I was having this particular problem. Nontheless, you've put me on the right track and I've come up with the code myself (which is always best, I think).

 

Filter #3 = 
IF (
    HASONEFILTER('Calendar'[Year]),
    [Filter #2],
    CALCULATE (
        [Filter #2],
        FILTER ( Spend, YEAR ( Spend[Order date] ) = YEAR ( Spend[Document date] ) )
    )
)

 

Output #4.PNG 

 

Many thanks for your advice.

Rg. Erwin

View solution in original post

2 REPLIES 2
Greg_Deckler
Super User
Super User

You have a measure total problem. See this article. https://community.powerbi.com/t5/DAX-Commands-and-Tips/Dealing-with-Measure-Totals/td-p/63376

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi Greg,

 

I already knew I was having this particular problem. Nontheless, you've put me on the right track and I've come up with the code myself (which is always best, I think).

 

Filter #3 = 
IF (
    HASONEFILTER('Calendar'[Year]),
    [Filter #2],
    CALCULATE (
        [Filter #2],
        FILTER ( Spend, YEAR ( Spend[Order date] ) = YEAR ( Spend[Document date] ) )
    )
)

 

Output #4.PNG 

 

Many thanks for your advice.

Rg. Erwin

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.

Top Solution Authors