cancel
Showing results for
Did you mean:

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

Frequent Visitor

## How does FIRSTNONBLANK and ALLEXCEPT together with CALCULATE return the first date for a given ID?

I'm trying to understand HOW the following calculation is returning the first date:

`FirstDate = CALCULATE(FIRSTNONBLANK(mytable[activitydate], 1), ALLEXCEPT(mytable, mytable[emailaddress])) `

FIRSTNONBLANK: Returns the first value in the column, column, filtered by the current context, where the expression is not blank.

ALLEXCEPT: Removes all context filters in the table except filters that have been applied to the specified columns.

Let's start with FIRSTNONBLANK. When it says "Returns the first value in the column", and let's say column is a Date/Time column not sorted in any way, does this function somehow perform a sorting to determine what the "FIRST" value should be? In my use-case, I am using this function to get the first date it was seen based on an email address.

What does the 1 actually mean in the FIRSTNONBLANK function above? Is that a boolean or literally the number 1? And why is it needed / what does it do when it's the expression? Does it just return a 1?

Now ALLEXCEPT. Is see that this is being used as a filter for CALCULATE, but I'm not sure what it's actually doing. "Removes all context filters" doesn't mean anything to me yet as a new user.

Ultimately, I don't understand how these three functions return the first seen date. Thanks in advance.

1 ACCEPTED SOLUTION
Super User

On FIRSTNONBLANK, it's not documented the best. Yes, it is a time intelligence function so it is intelligent about dates and times so it will find the "earliest" date in a series of unsorted dates.

So, I'm going to give you my perspective on this even though working with FIRSTNONBLANK can sometimes be tricky:

Suppose you have a 2 tables like this:

Dates

Date

 Monday, January 1, 2018 Sunday, January 1, 2017 Friday, January 1, 2016 Wednesday, December 12, 2018 Tuesday, December 12, 2017 Monday, December 12, 2016

DateValues

Date                                                  Value

 Monday, January 1, 2018 10 Sunday, January 1, 2017 14 Friday, January 1, 2016 Wednesday, December 12, 2018 22 Tuesday, December 12, 2017 55 Monday, December 12, 2016 60

And there is the obvious relationship.

Now you have a measure:

`Measure 1 = FIRSTNONBLANK(Dates[Date],1) `

This is going to return 1/1/2016. The expression isn't doing anything in this case. 1 is always 1 and is never blank, so all dates apply and you get the first.

But, if you do something like this:

`Measure 2 = FIRSTNONBLANK(Dates[Date],CALCULATE(SUM(DateValues[Value]),RELATEDTABLE(DateValues)))`

You get back 12/12/2016 because that is the earliest date where the expression is essentially not null or blank.

Now, ALLEXCEPT removes all other filter contexts except for the filters on the columns specified. This means that if you have other slicers on other columns, ALLEXCEPT removes those filters but keeps the filter that you have on, in your case emailaddress. So, if you had a filter on "First Name" and on "Last Name" for example, the calculation of FirstDate would not be affected by those filters as it would normally, it would only care about the filter on emailaddress.

CALCULATE is just a way to apply a specific filter context to something that you are evaluating.

Become an expert!: Enterprise DNA
External Tools: MSHGQM
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...
6 REPLIES 6
Community Champion

https://exceleratorbi.com.au/lastnonblank-explained/

Matt’s site has a ton of helpful information

Good Luck!

Frequent Visitor

This quote from ExceleratorBI was really helpful to understand why placing 1 in as the second parameter works:

LASTNONBLANK iterates through the Table[Column] and then checks to see if the second parameter has a value.  The number 1 always has a value of course, so placing 1 as the second parameter has the same effect as just ignoring this parameter.

Microsoft

The FIRSTNONBLANK function is just like using a MIN function.  It will just return the oldest date with respect to other filter functions

Proud to be a Datanaut!

Community Champion

As phil mentioned ...this function is very much like the Min function.

The second argument of this function can be anything that returns a nonblank value for the column.

You can use 0,1,"YourName" or a DAX measure or any other expression that returns a single value

But here is one important difference...

Lets take this table

 Product Amount A B C 145 D 164 E 179 F 164

Check the results of following MEASUREs in the TABLE

`Measure 1 = FIRSTNONBLANK(Table1[Product],1)`
`Measure 2= FIRSTNONBLANK(Table1[Product],CALCULATE(sum(Table1[Amount])))`
`Measure 3 = min(Table1[Product])`

The second argument of FirstNonBlank is thus the differentiator

Regards
Zubair

Community Champion

This is not mentioned in the documentation of the function but you are right

"FirstNonBlank /LastNonBlank return the first/last value respectively in the column…..after sorting the column in its native Ascending Order….column, filtered by the current context, where the expression is not blank."

You can check this post as well

http://www.excelnaccess.com/using-firstnonblank-lastnonblank-in-dax/

Regards
Zubair

Super User

On FIRSTNONBLANK, it's not documented the best. Yes, it is a time intelligence function so it is intelligent about dates and times so it will find the "earliest" date in a series of unsorted dates.

So, I'm going to give you my perspective on this even though working with FIRSTNONBLANK can sometimes be tricky:

Suppose you have a 2 tables like this:

Dates

Date

 Monday, January 1, 2018 Sunday, January 1, 2017 Friday, January 1, 2016 Wednesday, December 12, 2018 Tuesday, December 12, 2017 Monday, December 12, 2016

DateValues

Date                                                  Value

 Monday, January 1, 2018 10 Sunday, January 1, 2017 14 Friday, January 1, 2016 Wednesday, December 12, 2018 22 Tuesday, December 12, 2017 55 Monday, December 12, 2016 60

And there is the obvious relationship.

Now you have a measure:

`Measure 1 = FIRSTNONBLANK(Dates[Date],1) `

This is going to return 1/1/2016. The expression isn't doing anything in this case. 1 is always 1 and is never blank, so all dates apply and you get the first.

But, if you do something like this:

`Measure 2 = FIRSTNONBLANK(Dates[Date],CALCULATE(SUM(DateValues[Value]),RELATEDTABLE(DateValues)))`

You get back 12/12/2016 because that is the earliest date where the expression is essentially not null or blank.

Now, ALLEXCEPT removes all other filter contexts except for the filters on the columns specified. This means that if you have other slicers on other columns, ALLEXCEPT removes those filters but keeps the filter that you have on, in your case emailaddress. So, if you had a filter on "First Name" and on "Last Name" for example, the calculation of FirstDate would not be affected by those filters as it would normally, it would only care about the filter on emailaddress.

CALCULATE is just a way to apply a specific filter context to something that you are evaluating.

Become an expert!: Enterprise DNA
External Tools: MSHGQM
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

Announcements

#### Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

#### Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

#### The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors