cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
justlogmein
Helper III
Helper III

FIRSTNONBLANK to get the previous row value is not working as expected

I am trying to get the TRUE/FALSE value of the previous row. I have read to be able to do this for a row that isn't a number I need to use FIRSTNONBLANK with a 1 as the expression. However, for some reason this works for a bit then just wants to stick to a single value.

 

I have used the following DAX to get to this:

 

=
CALCULATE
	(
	FIRSTNONBLANK
		(
		'Sample Data'[Oil Changed],1
		),
	FILTER
		(
		'Sample Data',
		'Sample Data'[Sample Date] < EARLIER
			(
			'Sample Data'[Sample Date]
			)
		&& 'Sample Data'[Unit Number] = EARLIER
			(
			'Sample Data'[Unit Number]
			)
		)
	)

 

This is the output, plus what I actually want in the end column.

 

justlogmein_0-1655018986000.png

 

Does anyone know how I can do this? Power BI example file below.

 

Get Previous Value 

 

Just as a side rant, there really needs to be a better way of doing something simple like this. In Excel, it is literally "=[cell you want to reference]" in a table and it's done for the whole column. In DAX this is several lines of code...

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @justlogmein

 

How about this?

Oil Changed for Previous Row 2 =
VAR PreDate_ =
    CALCULATE (
        MAX ( 'Sample Data'[Sample Date] ),
        FILTER (
            'Sample Data',
            'Sample Data'[Sample Date] < EARLIER ( 'Sample Data'[Sample Date] )
                && 'Sample Data'[Unit Number] = EARLIER ( 'Sample Data'[Unit Number] )
        )
    )
VAR PreOilChanged_ =
    CALCULATE (
        FIRSTNONBLANK ( 'Sample Data'[Oil Changed], 1 ),
        FILTER (
            'Sample Data',
            'Sample Data'[Sample Date] = PreDate_
                && 'Sample Data'[Unit Number] = EARLIER ( 'Sample Data'[Unit Number] )
        )
    )
RETURN
    PreOilChanged_

Icey_0-1655260945642.png

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
CNENFRNL
Super User
Super User

CNENFRNL_1-1655026161036.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 beyond their comprehension!

DAX is simple, but NOT EASY!

Thank you for this, however when I try I am not getting the expected results. I am getting 1's and 0's and the pattern doesn't seem to line up with the Oil Changed column. Any ideas?

 

justlogmein_0-1655030994232.png

 

Icey
Community Support
Community Support

Hi @justlogmein

 

How about this?

Oil Changed for Previous Row 2 =
VAR PreDate_ =
    CALCULATE (
        MAX ( 'Sample Data'[Sample Date] ),
        FILTER (
            'Sample Data',
            'Sample Data'[Sample Date] < EARLIER ( 'Sample Data'[Sample Date] )
                && 'Sample Data'[Unit Number] = EARLIER ( 'Sample Data'[Unit Number] )
        )
    )
VAR PreOilChanged_ =
    CALCULATE (
        FIRSTNONBLANK ( 'Sample Data'[Oil Changed], 1 ),
        FILTER (
            'Sample Data',
            'Sample Data'[Sample Date] = PreDate_
                && 'Sample Data'[Unit Number] = EARLIER ( 'Sample Data'[Unit Number] )
        )
    )
RETURN
    PreOilChanged_

Icey_0-1655260945642.png

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Excel is a spreadsheet and has a named cell reference. This means every datapoint has its own address. This is very useful, but comes at a cost. Try adding a million rows of data and writing a formula in each row.  Power BI is not a spreadsheet, it is a column store reporting database. It is optimised for fast and efficient aggregations, calculations and data storage. But that comes at a cost. There is no cell reference and no referential "order" in the data itself, hence this particular problem is difficult to solve in PBI. 

Depending on the problem, you may be better off loading the value from the "previous row" using Power Query and loading it into the final loaded table in PBI. I show how to do this in my video here. 
https://youtu.be/xN2IRXQ2CvI

This may add preprocessing time, but it's often a better way to solve the problem. 

 



* Matt is a Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

Helpful resources

Announcements
September Update

Check it Out!

Click here to learn more about the September 2022 updates!

Power BI Dev Camp Session 26

Check it Out!

Mark your calendars and join us on Thursday, September 29 at 11a PDT for a great session with Ted Pattison!

Top Solution Authors