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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
MarceloPereira
Frequent Visitor

Exclude weekend using dateadd

Hi Everyone.

 

I need help to compare the margin of sales.

 

When I compare Monday's sales margin with the day before, the DATEADD function searches for Sunday's margin, where I don't have sales. I need to compare Monday's margin to Friday's margin, excluding the weekend in my calculation.

 

powerbiHelp.PNG

 

In my case, number 6 is Saturday and 0 is sunday.

 

There is any way to do it?

 

Best regards

1 ACCEPTED SOLUTION

Hi @MarceloPereira ,
Try this instead:

C_MargemTeste = 
var _Date = MAX(DIM_CALENDARIO[Date])
var _isWeekday = WEEKDAY(_Date, 2)
RETURN
SWITCH(TRUE(),
_isWeekday = 1, CALCULATE([C_Margem], DIM_CALENDARIO[Data] = _Date - 3),
_isWeekday > 1 && _isWeekday <= 5, CALCULATE([C_Margem],  DIM_CALENDARIO[Data] = _Date - 1))

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

View solution in original post

10 REPLIES 10
amitchandak
Super User
Super User

Try if Earlier can work in this case

 

https://community.powerbi.com/t5/Desktop/Explanation-of-the-EARLIER-formula/td-p/529469

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks.

My Recent Blog - https://community.powerbi.com/t5/Community-Blog/Comparing-Data-Across-Date-Ranges/ba-p/823601

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Hi @amitchandak,.

 

I can't see how this function could help me in this case. What did you think?

 

regards

Hi @MarceloPereira ,

Try this as a measure:

PreviousMargin = 
var _Date = MAX(YOUR_TABLE[Date])
var _isWeekday = WEEKDAY(_Date, 2)
RETURN
SWITCH(TRUE(),
_isWeekday = 1, CALCULATE(MAX(YOUR_TABLE[Margins]), YOUR_TABLE[Date] = _Date - 3),
_isWeekday > 1 && _isWeekday <= 5, CALCULATE(MAX(YOUR_TABLE[Margins]),  YOUR_TABLE[Date]= _Date - 1))

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

Hi @hnguy71 ,

 

I understand your logic, but my table has other dimensions that should be taken into account at this point in the calculation.
Because of this, I am receiving the following message:
The MAX function only accepts a column reference as the argument number 1.

 

regards,

Hi @MarceloPereira ,
You need to adjust the measure. YOUR_TABLE needs to be replaced with whatever table name you're currently using with your date field.  The same goes for your other fields. If the information is not sensitive, provide a PBIX and I can help you further.

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

Hi @hnguy71 ,

 

follow my pbix: Example file 

 

regards

Hi @MarceloPereira ,
Try this instead:

C_MargemTeste = 
var _Date = MAX(DIM_CALENDARIO[Date])
var _isWeekday = WEEKDAY(_Date, 2)
RETURN
SWITCH(TRUE(),
_isWeekday = 1, CALCULATE([C_Margem], DIM_CALENDARIO[Data] = _Date - 3),
_isWeekday > 1 && _isWeekday <= 5, CALCULATE([C_Margem],  DIM_CALENDARIO[Data] = _Date - 1))

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

How would you account for holidays using this?

Hi @Jwheels27 ,

You would probably need a column or a dimension that says that particular day is a holiday. From there it's a similar logic where you find out if that holiday lands on a weekday or not.

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

@hnguy71 ,

 

Wowww... That worked!
Thanks a lot for the help

 

best reagards

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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