cancel
Showing results for
Did you mean:

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a \$100 discount. Register Now

## DAX Calculation - Total for Previous Work Day - regardless of date in context

Hello Power BI gurus,

I have amounts posted each work day, and would like to be able to reliably calculate the amount from the Previous Work Day, in the context of the current Work Day.  See the snip below - the red column is the one I'm trying to calculate.

Things that I have tried:

(a) The Previousday() function doesn't help, because I want to jump back to the previous work day, or the previous day that I have data.

(b) Using today() doesn't work, because I don't always want the calculation to be from today's standpoint. So logic that uses TODAY() (like the following), isn't a solution:

(weekday(today(),2)
,1,TODAY()-3,
7,TODAY()-2,
TODAY()-1)

Tom in Indy

1 ACCEPTED SOLUTION

After further review, with stimulus from Matt, along with the guys from SQLBI, I found a solution.

I added another column to my Calendar table - PreviousWorkDayDimID, populating it with the DateDimID (YYYYMMDD integer) of the Previous work day.

Then, I made my formula the following:

FILTER (

ALL ('Calendar'),

'Calendar'[DateDimID]=max('Calendar'[PreviousWorkDayDimID])

)

...

Thanks for the help!

7 REPLIES 7

Convert your surrogate date key to a date

Create a calendar table and join to your data table

make sure your calendar table has an ID column (integer) starting at 1 for the first working day and advancing by 1 for every subsequent working day. Leave non working days blank

then the following formula will work

sales yesterday = calculate(sum(table[sales]),filter(all(calendar),calendar[id]=max(calendar[id])-1))

here are some articles for you to read and learn

http://exceleratorbi.com.au/power-pivot-calendar-tables/

http://exceleratorbi.com.au/dax-time-intelligence-beginners/

* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
Frequent Visitor

I'm not sure what go wrong but when I apply below code, it return total sale of the second last working day of my Calendar table. For example: today is 16 Jan 2019, last working day is 15 Jan 2019, it rerurn sale of last working day of my calendar table is 30 Dec 2019

sales yesterday = calculate(sum(table[sales]),filter(all(calendar),calendar[id]=max(calendar[id])-1)).

Here is my file.

Anonymous
Not applicable

Hey this still works hepled me a ton 🙂

Matt,

Your proposed calc gets the NEXT TO LAST day's sales, but won't do it backward throughout the calendar (I don't think). If I have the following dates, it won't show previous work day sales for each line.

Sales Date -----  Sales -----  PrevWorkdaySales

3/14/2017          100                90

3/13/2017            90               125

3/10/2017          125

I believe your proposed formula will just show PrevWorkdaySales (90) on the 3/14 line.

I was looking to find PrevWorkdaySales in context.

After further review, with stimulus from Matt, along with the guys from SQLBI, I found a solution.

I added another column to my Calendar table - PreviousWorkDayDimID, populating it with the DateDimID (YYYYMMDD integer) of the Previous work day.

Then, I made my formula the following:

FILTER (

ALL ('Calendar'),

'Calendar'[DateDimID]=max('Calendar'[PreviousWorkDayDimID])

)

...

Thanks for the help!

Frequent Visitor

HI @tljthree,

Could you pls show me how you create your column PreviousWorkDayDimID?

Employee

Hi @tljthree,

Great to hear the problem got resolved! Could you accept the helpful reply as solution to help others who may have similar issue easily find the answer and close this thread?

Regards

Announcements

#### Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

#### Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

#### Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

#### Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors