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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
tomshaw83
Helper I
Helper I

Calculated column to display last N values

Hi all,

 

I'm looking for some help creating a calculated column, whereby the average of the last 3 values recorded for a city are shown.

I'm getting lost somewhere after AVERAGEX(TOPN,3 - if that is even the right way to start to solve...

 

CityDateTempAverage Last 3 Temps for City
London21/7/20212119.6
New York21/7/20211818.3
London24/7/20212420.1
Paris25/7/20212221.1
London27/7/20212122
    
    
1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@tomshaw83  you can use this measure

 

Measure =
VAR _upper =
    MAX ( 'Table'[Date] )
VAR _city =
    MAX ( 'Table'[City] )
VAR _lower =
    MINX (
        TOPN (
            3,
            FILTER ( ALL ( 'Table' ), 'Table'[City] = _city && 'Table'[Date] <= _upper ),
            'Table'[Date], DESC
        ),
        'Table'[Date]
    )
VAR _avg =
    CALCULATE (
        AVERAGE ( 'Table'[Temp] ),
        FILTER (
            ALL ( 'Table'[Date] ),
            'Table'[Date] >= _lower
                && 'Table'[Date] <= _upper
        ),
        ALLEXCEPT ( 'Table', 'Table'[City] )
    )
RETURN
    _avg

 

 

smpa01_0-1638291073808.png

 

Calculated column

Column = 
VAR _upper =
    CALCULATE(MAX ( 'Table'[Date] ))
VAR _city =
    CALCULATE(MAX ( 'Table'[City] ))
VAR _lower =
    MINX(
        TOPN (
            3,
            FILTER ( ALL ( 'Table' ), 'Table'[City] = _city && 'Table'[Date] <= _upper ),
            'Table'[Date], DESC
        ),'Table'[Date])
VAR _avg =
    CALCULATE (
        AVERAGE ( 'Table'[Temp] ),
        FILTER (
            ALL ( 'Table'[Date] ),
            'Table'[Date] >= _lower
                && 'Table'[Date] <= _upper
        )
    ,ALLEXCEPT('Table','Table'[City]))
RETURN
    _avg

 

smpa01_1-1638291375039.png

 

Pbix is attached

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

1 REPLY 1
smpa01
Super User
Super User

@tomshaw83  you can use this measure

 

Measure =
VAR _upper =
    MAX ( 'Table'[Date] )
VAR _city =
    MAX ( 'Table'[City] )
VAR _lower =
    MINX (
        TOPN (
            3,
            FILTER ( ALL ( 'Table' ), 'Table'[City] = _city && 'Table'[Date] <= _upper ),
            'Table'[Date], DESC
        ),
        'Table'[Date]
    )
VAR _avg =
    CALCULATE (
        AVERAGE ( 'Table'[Temp] ),
        FILTER (
            ALL ( 'Table'[Date] ),
            'Table'[Date] >= _lower
                && 'Table'[Date] <= _upper
        ),
        ALLEXCEPT ( 'Table', 'Table'[City] )
    )
RETURN
    _avg

 

 

smpa01_0-1638291073808.png

 

Calculated column

Column = 
VAR _upper =
    CALCULATE(MAX ( 'Table'[Date] ))
VAR _city =
    CALCULATE(MAX ( 'Table'[City] ))
VAR _lower =
    MINX(
        TOPN (
            3,
            FILTER ( ALL ( 'Table' ), 'Table'[City] = _city && 'Table'[Date] <= _upper ),
            'Table'[Date], DESC
        ),'Table'[Date])
VAR _avg =
    CALCULATE (
        AVERAGE ( 'Table'[Temp] ),
        FILTER (
            ALL ( 'Table'[Date] ),
            'Table'[Date] >= _lower
                && 'Table'[Date] <= _upper
        )
    ,ALLEXCEPT('Table','Table'[City]))
RETURN
    _avg

 

smpa01_1-1638291375039.png

 

Pbix is attached

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.