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
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

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
Community Champion
Community Champion

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 still way 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

 

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 an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

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.

Top Solution Authors