简体   繁体   中英

Power BI, Get the last available value

Good Day,

The problem I have is that I have a set of data that comes from an evaluation that is not made on a monthly basis for all the shops, but what I want to use for evaluating is the most recent one. An example of the data is as following

Shop     Date        Total
A        15/01/18    85
A        15/03/18    78

B        15/01/18    73
B        15/02/18    69       

C        15/03/18    92

And for example in the shop A and C there is no problem showing the results from March, but for the shop BI cannot display the information because if I filter by month I don't get anything.

So is it possible to get the last available value?

This is a really complex problem, unless I'm missing something. Using LASTNONBLANK() will only get you part of the way to the solution.

I have never yet seen anyone post a method to address your situation, though I have seen a few that are similar in some ways

Matt Allington: https://exceleratorbi.com.au/lastnonblank-explained/ shows how to get the last value to show correctly in the subtotal area when there is missing data at the end of the column.

Marco Russo: https://www.sqlbi.com/articles/semi-additive-measures-in-dax/ is instructive but it carries the final balance on until you run out of dates in the date table.

I'm not saying that this measure will be the most efficient, but it should achieve what you seek:

Most Recent Balance = 
-- The first day of the period in context (not just for which we have data)
VAR context_first_date = FIRSTDATE(data[Date].[Date]) 

-- The last day of the period in context (not just for which we have data)
VAR context_last_date = LASTDATE(data[Date].[Date]) 

-- See if there are any transactions after the beginning of this period
VAR transactions_before_end = CALCULATE(COUNTROWS(data), FILTER(ALL(data), data[Date] <= context_last_date))

-- See if there are any transactions before the end of this period
VAR transactions_after_beginning = CALCULATE(COUNTROWS(data), FILTER(ALL(data), data[Date] >= context_first_date)) 

-- Find the last date for which there was a transaction
VAR transaction_last_date = 
    CALCULATE(
        MAX(data[Date]), 
        ALL(data[Date].[Date]),
        ALL(data[Date].[Day]),
        ALL(data[Date].[Month]),
        ALL(data[Date].[MonthNo]),
        ALL(data[Date].[Quarter]),
        ALL(data[Date].[QuarterNo]),
        ALL(data[Date].[Year]),
        data[Date] <= context_last_date)
RETURN 
IF(
    -- If there are either no transactions before the end of this period or none after the start, that means we are in a period 
    -- for which no data exists (e.g. the future) so show nothing
    ISBLANK(transactions_after_beginning) || ISBLANK(transactions_before_end), 
    BLANK(), 
    CALCULATE(SUM(data[Total]), data[Date].[Date] = transaction_last_date)
)

Note that at the moment this does not produce a good total when aggregationg all stores together.

CALCULATE(SUM('Table'[Total]), FILTER('Table','Table'[Date]=MAX('Table'[Date])))

根据上表

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM