cancel
Showing results for
Did you mean:
Helper II

## If previous day value is blank, then take lastnonblankvalue before current date

Hello,

I have a question. I'm creating a stock report. The end stock of the previous day, is the opening stock of the current day. However, on some days there is no end stock.

This is an example of the table visual:

On 03/06/222 there are no stock calculations.

So therefore the beginstock of 04/06/2022 is empty.  But I would like to see the endstock of 02/06/2022 (which is 20) as begin stock of 04/06/2022.

What I would like to achieve is the following:

If previous day value is blank, then take lastnonblankvalue before current date

This is the calculation of begin stock:

Begin stock =
VAR beginstock = CALCULATE(SUM(RetailStock[StockHoeveelheid]), PREVIOUSDAY('Datum'[Datum]))
Var Result = IF(
not(
ISBLANK(
SUM(RetailStock[StockHoeveelheid]))),
beginstock, blank())
Return
Result

How can I achieve this?

4 REPLIES 4
Solution Supplier

Hello LamSar

Try this measure:

``````Begin stock =

var _day = max('Datum'[Datum])

Var _daybefore =
calculate(
MAX(RetailStock[Datum]),
Filter ( ALL(RetailStock),RetailStock[Datum]<_day)

VAR beginstock =
CALCULATE(
SUM(RetailStock[StockHoeveelheid]),
Filter ( ALL(RetailStock),RetailStock[Datum]=_daybefore)

Var Result =
IF(
not( ISBLANK(
SUM(RetailStock[StockHoeveelheid]))),
beginstock, blank())
Return
Result``````

Best Regards
BC

Helper II

Sadly this will not work, because the RetailStock table does not hold a column with a date column. It only holds a column that holds a surrogate key (datatype int) that refers to the date dimension.

I can't add a column because I'm using a tabular modal.

Solution Supplier

Hi again,

So use the id

``````Begin stock =

var _dayID = max('Datum'[DatumID])

Var _daybeforeID =
calculate(
MAX(RetailStock[DatumID]),
Filter ( ALL(RetailStock),RetailStock[DatumID]<_dayID)

VAR beginstock =
CALCULATE(
SUM(RetailStock[StockHoeveelheid]),
Filter ( ALL(RetailStock),RetailStock[DatumID]=_daybeforeID)

Var Result =
IF(
not( ISBLANK(
SUM(RetailStock[StockHoeveelheid]))),
beginstock, blank())
Return
Result``````

Best Regards
BC

Helper II

I already tried that. But then I get the wrong values:

Announcements

#### 2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

#### Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

#### 2022 Monthly Feature Releases

We had a great 2022 with a ton of feature releases to help you drive a data culture.

Top Solution Authors
Top Kudoed Authors