Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Monty0
Frequent Visitor

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

 

Example.PNG

 

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 3
v-lili6-msft
Community Support
Community 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:

8.JPG

here is pbix file, please try it.

 

Best Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Monty,

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

Pivot Table.png

 

It is assuming it restarts on the 1st of year

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

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.