Forum Discussion

Monty0's avatar
Monty0
Frequent Visitor
7 years ago

Dax Repeat last non empty measure for date sequence

Hi all

 

I am trying to implement some Dax to replicate an excel formula. The formula uses 2 measures (Actual, Budget) to determine the value for a third (Forecast). A date dimension is used to determine the sequence of values and calculate the budget remaining until the end of the year and the actual year to date.

 

-------------------

Row 1 to 4 show the required output, row 6 is the actual output using the dax I have so far

 

 

This link is the actual spreadsheet Forecast Measure

 

--------------------------------------

Sudocode for the required output

 

If first month in the sequence
  If Actual is blank
       Sum of Budget
  else
       Actual until this period plus sum of Budget for rest of the year
else
 If Actual is blank
      Use last non blank Forecast
 else
      Actual until this period plus sum of Budget for rest of the year
end

------------------

This is the dax I have so far

 

Forecast:=
IF(
/* No actual then use the value from the last calculated value */
ISBLANK([Actual]),
 CALCULATE(
     [Budget Rest Of Year] + [Actual YTD],
     LASTNONBLANK (
     'Accounting Period'[Month],
     [Budget Rest Of Year] + [Actual YTD]
  )
),
/* Otherwise calculate for the current period */

[Budget Rest Of Year] + [Actual YTD]
)

 

Any help very much appreciated

Monty

3 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Monty,

    Before I dive into an explanation, does this look about right?

     

    It is assuming it restarts on the 1st of year

    • Monty0's avatar
      Monty0
      Frequent Visitor

      Hi Nick

       

      Thanks for the response. Yes the value will reset after a fiscal year but yes, your example looks correct assuming that was 01/01.

       

      Monty

  • v-lili6-msft's avatar
    v-lili6-msft
    Icon for Community Support rankCommunity Support

    hi, Monty0 

    You may try to this way as below:

    Step1:

    Add an index column for date(month ) column.

    Index = RANKX(Table1,Table1[Date],,ASC ,Dense)

    Step2:

    Use this formula to create the result measure

    Result = var _firstmonth=CALCULATE(FIRSTDATE(Table1[Date]),ALLSELECTED(Table1)) 
    var no1=IF(
    
    ISBLANK(CALCULATE(SUM(Table1[Actual]),FILTER(Table1,Table1[Date]=_firstmonth))),SUM(Table1[Budget]),CALCULATE(SUM(Table1[Actual]),FILTER(ALLSELECTED(Table1),Table1[Index]<=MAX(Table1[Index])))+CALCULATE(SUM(Table1[Budget]),FILTER(ALLSELECTED(Table1),Table1[Index]>MAX(Table1[Index]))))
    var no2=IF(
    
    ISBLANK(CALCULATE(SUM(Table1[Actual]))),LASTNONBLANK(Table1[Forecast],SUM(Table1[Forecast])),CALCULATE(SUM(Table1[Actual]),FILTER(ALLSELECTED(Table1),Table1[Index]<=MAX(Table1[Index])))+CALCULATE(SUM(Table1[Budget]),FILTER(ALLSELECTED(Table1),Table1[Index]>MAX(Table1[Index]))))
    return
    
    
    IF(SELECTEDVALUE(Table1[Date])=_firstmonth,no1,no2)

    Result:

    here is pbix file, please try it.

     

    Best Regards,

    Lin