cancel
Showing results for 
Search instead for 
Did you mean: 

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

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

Helpful resources

Announcements
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

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

Community News

Fabric Community News unified experience

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

Power BI Fabric Summit Carousel

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