Forum Discussion
sebastiennn
1 year agoRegular Visitor
Excel Measure - Latest price applicable
Hi, For the last 2 days, I have tried to find the measure I should use for this problem (without success obviously 🙂 So I have a table with 3 colums: Article, Price and Date_of_change. So th...
- 1 year ago
You've posted your question in the Power Query section so I answer with that (no measures).
Paste this into the advanced editor of PQ:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIw1AciIwMjEyDH0MBAKVYHIWGEkDBFkTCBSxhCJJwwjDI1QJEwRkig6kAYZQaRcEa33MgAVQKuwwhquQuGHSB/xAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Article = _t, Date = _t, Price = _t]), fnEOMPrices=(tbl)=> let #"Changed Type1" = Table.TransformColumnTypes(tbl,{{"Date", type date}}), #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"Date", Order.Ascending}}), #"Filled Down" = Table.FillDown(#"Sorted Rows",{"Price"}), #"Filtered Rows1" = Table.SelectRows(#"Filled Down", each ([Article] = null)), #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows1",{"Article"}) in #"Removed Columns", ChangedType = Table.TransformColumnTypes(Source,{{"Date", type date}}), Latest = List.Max(ChangedType[Date]), Earliest = List.Min(ChangedType[Date]), EsOM = List.Transform(List.Generate(()=>Earliest, each _ <= Latest, each Date.AddMonths(_, 1)),Date.EndOfMonth), TblEsOM = Table.FromList(EsOM, Splitter.SplitByNothing(),{"Date"}, null, ExtraValues.Error), #"Grouped Rows" = Table.Group(ChangedType, {"Article"}, {{"grp", each _ & TblEsOM}}), #"Invoked Custom Function" = Table.AddColumn(#"Grouped Rows", "EomPrices", each fnEOMPrices([grp])), #"Removed Columns" = Table.RemoveColumns(#"Invoked Custom Function",{"grp"}), #"Expanded EomPrices" = Table.ExpandTableColumn(#"Removed Columns", "EomPrices", {"Date", "Price"}, {"Date", "Price"}), #"Filtered Rows" = Table.SelectRows(#"Expanded EomPrices", each ([Price] <> null)), #"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"Date", type date}, {"Price", type number}}) in #"Changed Type"It produces a table thus:
which can be pivoted, by (1) as a last step in PQ or (2) by adding a pivot table based on that table or (3) by loading to Pivot Table report. Whichever, the pivot ends up like:
The code can no doubt be more elegant and efficient…
- 1 year ago
The LASTNONBLANKVALUE function does not exist in some versions of Excel (and possibly all versions).
DAX measure formula:
VAR vCurrentDate = MAX('DT'[Date]) RETURN IF( vCurrentDate = EOMONTH(vCurrentDate, 0), CALCULATE( MAX('CT'[Price]), LASTNONBLANK('DT'[Date] <= vCurrentDate, CALCULATE(MAX('CT'[Price]))) ), BLANK() )
ZhangKun
1 year agoSuper User
The LASTNONBLANKVALUE function does not exist in some versions of Excel (and possibly all versions).
DAX measure formula:
VAR vCurrentDate = MAX('DT'[Date])
RETURN
IF(
vCurrentDate = EOMONTH(vCurrentDate, 0),
CALCULATE(
MAX('CT'[Price]),
LASTNONBLANK('DT'[Date] <= vCurrentDate, CALCULATE(MAX('CT'[Price])))
),
BLANK()
)
sebastiennn
1 year agoRegular Visitor
Thanks a lot. I tested a lot of different measures without success.
It works perfectly. Thanks again !!