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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Anonymous
Not applicable

Get last 4 values/dates from a table

Hi Experts

I have a requirement to calculate 4 four values average.

 

My data is as such

Namedatevalue
Ammonia

1/1/2023

1.25
Ammonia 1/7/20230.21

Ammonia

1/19/20230.3
Ammonia1/22/20231.1
Ammonia2/1/20233.11
Ammonia2/4/20230.32
Ammonia2/12/20230.91
Ammonia2/23/20231.01
Ammonia2/25/20232.43
Potassium1/7/20230.02
Potassium1/11/20230.68
Potassium1/19/20230.81
Potassium2/5/20231.66
Potassium2/23/20231.25

I need to display 2 tables like this. One table with summary and bottom table with only last 4 values for each Name column

NameTimeperiodAverage
Ammonia2/4/2023 - 2/25/20231.2
Potassium1/11/2023 - 2/23/20231.1
Namedatevalueavg
Ammonia2/4/20230.321.2
Ammonia2/12/20230.911.2
Ammonia2/23/20231.011.2
Ammonia2/25/20232.431.2
Potassium1/11/20230.681.1
Potassium1/19/20230.811.1
Potassium2/5/20231.661.1

Potassium

2/23/20231.251.1
1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @Anonymous 

 

You can create a measure that virtually ranks the dates for each Name and filter that to ranks <=4

Latest 4 Value Avg = 
AVERAGEX (
    FILTER (
        SUMMARIZECOLUMNS (
            Data[Name],
            Data[date],
            "@rank", RANKX ( ALL ( Data[date] ), CALCULATE ( AVERAGE ( Data[date] ) ),, DESC, DENSE ),
            "@value", CALCULATE ( SUM ( Data[value] ) )
        ),
        [@rank] <= 4
    ),
    [@value]
)

 

And then another measure referencing the first one but returns a value only for those with ranks <= 4.

Name Avg = 
IF (
    NOT ( ISBLANK ( [Latest 4 Value Avg] ) ),
    CALCULATE ( [Latest 4 Value Avg], ALLEXCEPT ( Data, Data[Name] ) )
)

danextian_1-1740911672604.png

 

Please see the attached sample pbix.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

4 REPLIES 4
danextian
Super User
Super User

Hi @Anonymous 

 

You can create a measure that virtually ranks the dates for each Name and filter that to ranks <=4

Latest 4 Value Avg = 
AVERAGEX (
    FILTER (
        SUMMARIZECOLUMNS (
            Data[Name],
            Data[date],
            "@rank", RANKX ( ALL ( Data[date] ), CALCULATE ( AVERAGE ( Data[date] ) ),, DESC, DENSE ),
            "@value", CALCULATE ( SUM ( Data[value] ) )
        ),
        [@rank] <= 4
    ),
    [@value]
)

 

And then another measure referencing the first one but returns a value only for those with ranks <= 4.

Name Avg = 
IF (
    NOT ( ISBLANK ( [Latest 4 Value Avg] ) ),
    CALCULATE ( [Latest 4 Value Avg], ALLEXCEPT ( Data, Data[Name] ) )
)

danextian_1-1740911672604.png

 

Please see the attached sample pbix.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Anonymous
Not applicable

@danextian, please also let me know to limit table only to display  rows for  last 4 dates. Because I want to display in my 2nd table with only last4 dates. With the above measure "Latest 4 Value Avg" not giving me blanks in my case so I'm not able to filter blank values from table

 

You can see in my example that Latest 4 Value Avg is showing blanks. Please share a sanitized copy of your pbix so I can inspect why.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Anonymous
Not applicable

Thanks for your proposed solution.  For the summarized table (table1) solution is working as expected.

 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.