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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
av9
Helper III
Helper III

Optimize SUMX and FIlter measure

Hi there, I am trying to find a way to potentially write this dax measure better and improve its speed. I currently get Customer Sales data for each country at different times of the month. So I need to show the latest sales value per customer based on the last date each counties data was refreshed. In order to do this I created 4 measures the main one being: 

Latest Sales Amount =
SUMX (
    VALUES ( Country[Country] ),
    IF (
        [Max Sales Date] = [Last Country Refresh Date],
        [Sum Sales],
        BLANK ()
    )
)

The dependant measures are:

Max Sales Date = CALCULATE ( MAX ( Sales[Date] ), REMOVEFILTERS ( Customer[Name] ) )
Sum Sales =
CALCULATE (
    SUMX ( VALUES ( Country[Country] )SUM ( Sales[Amount] ) ),
    FILTER ( Sales, ( MAX ( Sales[Date] ) = [Max Sales Date] ) )
)
Last Country Refresh Date = CALCULATE ( LASTDATE ( ( Sales[Date] ) ) )

Was hoping to see if anyone thinks these could be written better? or have I got a good solution.

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @av9 ,

You can create a measure as below:

01_Measure = 
VAR _tab =
    SUMMARIZE (
        'Sales',
        'Sales'[Customer],
        'Sales'[Country],
        "latest date", MAX ( 'Sales'[Date] ),
        "sAmount",
            CALCULATE (
                SUM ( 'Sales'[Amount] ),
                FILTER ( 'Sales', 'Sales'[Date] = MAX ( 'Sales'[Date] ) )
            )
    )
RETURN
    SUMX ( _tab, [sAmount] )

Optimize SUMX and FIlter measure.JPG

Best Regards
Community Support Team _ Rena Ruan
If this post helps, then please consider Accept it as the solution to help the other members find it more.

Community Support Team _ Rena
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
v-yiruan-msft
Community Support
Community Support

Hi @av9 ,

You can create a measure as below:

01_Measure = 
VAR _tab =
    SUMMARIZE (
        'Sales',
        'Sales'[Customer],
        'Sales'[Country],
        "latest date", MAX ( 'Sales'[Date] ),
        "sAmount",
            CALCULATE (
                SUM ( 'Sales'[Amount] ),
                FILTER ( 'Sales', 'Sales'[Date] = MAX ( 'Sales'[Date] ) )
            )
    )
RETURN
    SUMX ( _tab, [sAmount] )

Optimize SUMX and FIlter measure.JPG

Best Regards
Community Support Team _ Rena Ruan
If this post helps, then please consider Accept it as the solution to help the other members find it more.

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
amitchandak
Super User
Super User

@av9 , Refer to last sales measure in the file attached after signature

Last Sales = CALCULATE(SUM(Sales[Amount]), FILTER(Sales, Sales[Date] = CALCULATE(max(Sales[Date]), ALLEXCEPT(Sales,Sales[Customer],Sales[Country]))))
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
av9
Helper III
Helper III

Attached is the file https://drive.google.com/file/d/1QZ9ZBXsZykX5X5g31Rk63hC4r2OWL0HR/view?usp=sharing

 

I should also point out in the file I created another measure 'Total Sales' to show zeros instead of blanks.

amitchandak
Super User
Super User

@av9 ,Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

 

Try a measure like one of the two


sumx(values(Country[Country]), lastnonblankvalue(Sales[Date] , SUM ( Sales[Amount] )))

or

calculate(sumx(values(Country[Country]), lastnonblankvalue(Sales[Date] , SUM ( Sales[Amount] ))), allexpcept(Country[Country]))

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
mahoneypat
Microsoft Employee
Microsoft Employee

Please try this expression instead.  This assumes that your [Sum Sales] measure is working as intended.  Your SUM(Sales[Amount]) should be wrapped in a CALCULATE(), and the [Max Sales Date] should likely be calculated up front in a variable and not inside the FILTER.  Does that measure return the expected results when used in a table visual with Country?  Is it performant?

 

Latest Sales Amount =
VAR summary =
    ADDCOLUMNS (
        VALUES ( Country[Country] ),
        "maxdate", [Max Sales Date],
        "lcrefresh", [Last Country Refresh Date],
        "sumsales", [Sum Sales]
    )
RETURN
    SUMX (
        FILTER (
            summary,
            [maxdate] <> [lcrefresh]
        ),
        [sumsales]
    )

 

Regards,

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


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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

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