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
Anonymous
Not applicable

Sum of recent values based on years and Ranking

Hi

I have the following data set wherein Assets are priced in 2018 and 2019 (some are only values in one year like Asset D in 2018 and C in 2019, whereas, some are repeating in both the years like Asset A, B and E). I am looking to get the sum of all assets in one card but only want the latest value for the repeating assets. For example i do not want the values of assets A, B and E for 2018 in the sum.

Asset NameLocationValueYear
ACalifornia502018
BWashington602018
DNew York452018
ENew Mexico682018
ACalifornia552019
BWashington662019
CDelaware702019
ENew Mexico652019

 

The total should be 301. I think it can be done using ranking but not clear on the process.

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

6 REPLIES 6
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Thanks @Ashish_Mathur 

In the measure created 'Total of value in recent years' i am unable to understand the refrence if 'ABCD' not sure from where it is coming from.

You are welcome.  The SUMMARIZE() function requires the 3rd input to be a column heading.  So ABCD is just that heading within double quotes.  It can be any other descriptive heading you want.

If my previous reply helped, please mark it as Answer.   


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
edhans
Super User
Super User

For some reason @CNENFRNL the data lineage using DISTINCT() isn't the first thing that comes to my mind...

One difference though is yours will not allow for multiple values in the same year. My longer measure will. I am not sure if @Anonymous can guarantee one value per asset per year.

 

Modifying @CNENFRNL's measure this way will do a sum.

New Measure = 
SUMX(
    DISTINCT('Table'[Asset Name]),
    VAR varYear = 
        CALCULATE(
            MAX('Table'[Year])
        )
    RETURN
        CALCULATE(
            SUM('Table'[Value]),
            FILTER(
                ALL('Table'[Year]),
                'Table'[Year] = varYear
            )
        )
    )

 I did the full FILTER vs just the predicate. Habbit. 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
CNENFRNL
Community Champion
Community Champion

Hi, @Anonymous , Both DAX and Power Query do the trick with ease. You might want to refer to the attached file for details.

 

DAX measure:

Latest Value = 
SUMX (
    DISTINCT ( Assets[Asset Name] ),
    VAR __yr =
        CALCULATE ( MAX ( Assets[Year] ) )
    RETURN
        CALCULATE ( MAX ( Assets[Value] ), Assets[Year] = __yr )
)

Screenshot 2020-11-28 021143.png

 

PQ solution:

Screenshot 2020-11-28 020841.pngScreenshot 2020-11-28 020909.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

edhans
Super User
Super User

Hi @Anonymous - there might be a simpler way to do this, but I took the long and straighforward route.

 

 

Measure2 = 
VAR varGroupedData = 
ADDCOLUMNS(
    SUMMARIZE(
        'Table',
        'Table'[Asset Name]
    ),
    "Summary Values",
        CALCULATE(
        	VAR varAsset = MAX('Table'[Asset Name])
            VAR varMaxYear =
                MAXX(
                    FILTER(
                        ALL(
                            'Table'[Year],
                            'Table'[Asset Name]
                        ),
                        'Table'[Asset Name] = varAsset
                    ),
                    'Table'[Year]
                )
            RETURN
                SUMX(
                    FILTER(
                        'Table',
                        'Table'[Year] = varMaxYear
                    ),
                    'Table'[Value]
                )
        )
)
RETURN
    SUMX(
        varGroupedData,
        [Summary Values]
    )

 

 

It will return these two visuals if desired:

edhans_0-1606515863178.png

It does this by building a temporary table that is by asset and the latest values for that asset. So the ADDCOLUMNS(SUMMARIZE()) build this:

edhans_1-1606515916805.png

Then SUMX just adds it up.

 

EDIT: slightly cleaned it up. The Asset column in ADDCOLUMNS() wasn't needed. I was just thinking through the issue there.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

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.