Forum Discussion
Dax - Optimise LASTNONBLANKVALUE
Hello,
I currently have a model that relies heavily on the LASTNONBLANKVALUE function to act as a (forward) filldown of the plants' costs that are missing for certain dates. I have a calendar table which encompass every day between the first and last day of my fact table. All my visuals only show dates that are the first of the month.
The LastNonblankValue is so ressource intensive that in some instance visual won't display anything as the memory requirement is over the 1024MB limit.
Would you have input on how to restrict the scope of the function actions?
Here's one of the measure using lastnonblank and an excerpt of my fact table :
Price xUM =
CALCULATE(
LASTNONBLANKVALUE(
_Calendar[Date],
CALCULATE(SUM(PurchasePrices[CostValue (USD/HL)]), PurchasePrices[CostType] <> "Upstream")
),
_Calendar[Date] <= MAX(_Calendar[Date])
)
| RIPCode | CostType | PlantCode | ApplDate | CostValue (USD/HL) | CostCategory |
| XY | B Cost | P12 | 11/18/2024 | 156.36 | B |
| XY | B Cost | P12 | 10/1/2023 | 168.17 | B |
| XY | D Cost | P12 | 11/18/2024 | 31.91 | D |
| XY | D Cost | P12 | 10/1/2023 | 31.89 | D |
| XY | Margin | P12 | 11/18/2024 | 10.57 | NM |
| XY | Admin Fee | P12 | 11/18/2024 | 7.12 | NM |
| XY | Fee | P12 | 11/18/2024 | 16.02 | NM |
| XY | Margin | P12 | 10/1/2023 | 10.94 | NM |
| XY | Admin Fee | P12 | 10/1/2023 | 7.12 | NM |
| XY | Fee | P12 | 10/1/2023 | 11.57 | NM |
| XY | Upstream | P12 | 2/1/2025 | -22.00 | D |
| XY | Upstream | P12 | 1/16/2025 | -22.00 | D |
| XY | Upstream | P12 | 1/1/2025 | -22.00 | D |
| XY | Upstream | P12 | 12/1/2024 | -19.70 | D |
| XY | Upstream | P12 | 11/1/2024 | -19.70 | D |
| XY | Upstream | P12 | 10/1/2024 | -19.70 | D |
| XY | Upstream | P12 | 9/16/2024 | -19.70 | D |
| XY | Upstream | P12 | 9/1/2024 | -19.70 | D |
| XY | Upstream | P12 | 8/1/2024 | -19.70 | D |
| XY | Upstream | P12 | 7/16/2024 | -19.70 | D |
| XY | Upstream | P12 | 7/1/2024 | -19.70 | D |
| XY | Upstream | P12 | 6/1/2024 | -19.70 | D |
| XY | Upstream | P12 | 5/16/2024 | -19.70 | D |
| XY | Upstream | P12 | 5/1/2024 | -19.70 | D |
| XY | Upstream | P12 | 4/16/2024 | -19.70 | D |
| XY | Upstream | P12 | 4/1/2024 | -19.70 | D |
| XY | Upstream | P12 | 3/16/2024 | -19.70 | D |
| XY | Upstream | P12 | 3/1/2024 | -19.70 | D |
| XY | Upstream | P12 | 2/1/2024 | -19.70 | D |
| XY | Upstream | P12 | 1/16/2024 | -19.70 | D |
| XY | Upstream | P12 | 1/1/2024 | -19.70 | D |
| XY | Upstream | P12 | 12/16/2023 | -19.70 | D |
| XY | Upstream | P12 | 12/1/2023 | -19.70 | D |
| XY | Upstream | P12 | 11/16/2023 | -19.70 | D |
| XY | Upstream | P12 | 11/1/2023 | -19.70 | D |
| XY | Upstream | P12 | 10/16/2023 | -19.70 | D |
| XY | Upstream | P12 | 10/1/2023 | -19.70 | D |
| XY | Upstream | P12 | 9/16/2023 | -19.70 | D |
| XY | Upstream | P12 | 9/1/2023 | -19.70 | D |
| XY | Upstream | P12 | 8/16/2023 | -19.70 | D |
| XY | Upstream | P12 | 8/1/2023 | -19.70 | D |
| XY | Upstream | P12 | 7/16/2023 | -19.70 | D |
| XY | Upstream | P12 | 7/1/2023 | -19.70 | D |
| XY | Upstream | P12 | 6/16/2023 | -19.70 | D |
| XY | Upstream | P12 | 6/1/2023 | -19.70 | D |
| XY | Upstream | P12 | 5/16/2023 | -19.70 | D |
| XY | Upstream | P12 | 5/1/2023 | -19.70 | D |
In this case the measure works and is able to display values for all CostType up until first of Feb 25 for the the plant P12 but it requires too much memory.
Thanks a lot for your time and let me know if more info are needed!
- Anonymous1 year ago
Hi Anonymous ,
Due to I don't know what column you add into line chart Legend, here I use CostCategory in sample.
I suggest you to try code as below.
Price xUM 1 = VAR MaxDate = CALCULATE ( MAX ( PurchasePrices[ApplDate] ), FILTER ( ALLEXCEPT ( PurchasePrices, PurchasePrices[CostCategory] ), PurchasePrices[CostType] <> "Upstream" && PurchasePrices[ApplDate] <= MAX ( _Calendar[Date] ) ) ) RETURN CALCULATE ( SUM ( PurchasePrices[CostValue (USD/HL)] ), PurchasePrices[CostType] <> "Upstream", _Calendar[Date] = MaxDate )Result is as below.
The new one show a better performance.
Best Regards,
Rico ZhouIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
6 Replies
- Abhilash_P
Super User
Hi Anonymous ,
Can you try with below DAX functionScope Wt Avg =
VAR MaxApplDate =
CALCULATE(
MAX(PurchasePrices[ApplDate]),
FILTER(
PurchasePrices,
PurchasePrices[ApplDate] <= MAX(_Calendar[Date])
)
)VAR WeightedValue =
CALCULATE(
SUM(Products[Weight]),
PurchasePrices[ApplDate] = MaxApplDate
)RETURN
IF(NOT ISBLANK(WeightedValue), WeightedValue)- AnonymousNot applicable
Hi, thanks a lot Abhilash however I'm so sorry I posted the wrong DAX measure .... I edited the post but the measure I needed help with was :
Price xUM =
CALCULATE(
LASTNONBLANKVALUE(
_Calendar[Date],
CALCULATE(SUM(PurchasePrices[CostValue (USD/HL)]), PurchasePrices[CostType] <> "Upstream")
),
_Calendar[Date] <= MAX(_Calendar[Date])
)However I think your approach of creating a custom measure to replace the Lastnonblank might be the solution I'm looking for .
- Abhilash_P
Super User
Hi Anonymous ,
Can you check with below daxPrice xUM =
VAR MaxDate =
MAX(_Calendar[Date])RETURN
CALCULATE(
SUM(PurchasePrices[CostValue (USD/HL)]),
PurchasePrices[CostType] <> "Upstream",
_Calendar[Date] <= MaxDate
)
Let me know if these works