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
ranbeermakin
Resolver III
Resolver III

DAX Help: Find last 5 nonblank values in a column

Hi All,

 

I require help in getting AVG of the last 5 nonblank values from column B. My table is setup like this:

 

Table

So, pickup values 3788, 3522, 3604, 2326, 3058 and then avg it.

 

Can someone help?

 

Thanks.

 

 

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

@ranbeermakin 

I would suggest this measure - a slight modification of the above reply that ensures the top 5 dates are taken from those where B is not blank:

Average of B Over Last 5 Nonblank Dates = 
VAR Last5Dates =
    CALCULATETABLE ( 
        TOPN ( 5, VALUES ( YourTable[Date] ), YourTable[Date]  ),
        NOT ISBLANK ( YourTable[B] )
    )
RETURN
    CALCULATE ( AVERAGE ( YourTable[B] ), Last5Dates )


Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

3 REPLIES 3
OwenAuger
Super User
Super User

@ranbeermakin 

I would suggest this measure - a slight modification of the above reply that ensures the top 5 dates are taken from those where B is not blank:

Average of B Over Last 5 Nonblank Dates = 
VAR Last5Dates =
    CALCULATETABLE ( 
        TOPN ( 5, VALUES ( YourTable[Date] ), YourTable[Date]  ),
        NOT ISBLANK ( YourTable[B] )
    )
RETURN
    CALCULATE ( AVERAGE ( YourTable[B] ), Last5Dates )


Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn
Anonymous
Not applicable

Hey Ranbeer,

 

CALCULATE(AVERAGE(Table1[ColumnB]), // Col b is nmberic that we want avg for
TOPN(
5,
Table1,
Table1[Date])
)
 
 
n_s
Frequent Visitor

Hello!

 

The CALCULATE expression allows you to compute values based on certain filters, more info here.

So you would say

CALCULATE(AVERAGE(Column Name),Column Name<>BLANK())

And then in order to select the last 5 measures, you can select the column name in the filters selection > Select ‘Top N’ in the drop-down > switch it to Bottom > enter 5.

Or if you have ranked the column by date, and on the filter, select whichever ones you want by adding another filter to the CALCULATE measure:

CALCULATE(AVERAGE(‘Column Name’),’Column Name’<>BLANK(),’Date’ > (Specific Date))

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.