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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
SonaSinghA
Helper III
Helper III

How can I get last Friday date based on Today in a Custom Column of Power Query please?

Hi,

How can I get last Friday date based on Today in a Custom Column of Power Query please?
I tried the below solution, but if today is Friday, then the formula is giving today's date instead of last Friday.

Date.AddDays([DateColumn],-Date.DayOfWeek([DateColumn],5)))

 

 

Thanks

2 ACCEPTED SOLUTIONS
belvoir99
Resolver III
Resolver III

Hi @SonaSinghA 

 

CurrentDate = DateTime.Date( DateTime.LocalNow() ),
LastFridayColumn = Table.AddColumn(LastStepName, "LastFriday", each if Date.DayOfWeek(CurrentDate) >= 5 then Date.AddDays(CurrentDate, -Date.DayOfWeek(CurrentDate) - 3 + 7) else Date.AddDays(CurrentDate, -Date.DayOfWeek(CurrentDate) - 3), type date)

 

The default DayOfWeek is zero for Monday to 6 Sunday.

 

EDIT: today is Friday - this will choose the previous Friday, 29 March. You'll have to modify the formula slightly if you want today to return today.

View solution in original post

Thank you @belvoir99, it worked for me 
= Table.AddColumn(#"Filtered Rows4", "LastFriday", each if Date.DayOfWeek(DateTime.Date( DateTime.LocalNow() )) >= 5 then Date.AddDays(DateTime.Date( DateTime.LocalNow() ), -Date.DayOfWeek(DateTime.Date( DateTime.LocalNow() )) - 3 + 7) else Date.AddDays(DateTime.Date( DateTime.LocalNow() ), -Date.DayOfWeek(DateTime.Date( DateTime.LocalNow() )) - 3), type date)

View solution in original post

5 REPLIES 5
belvoir99
Resolver III
Resolver III

Hi @SonaSinghA 

 

CurrentDate = DateTime.Date( DateTime.LocalNow() ),
LastFridayColumn = Table.AddColumn(LastStepName, "LastFriday", each if Date.DayOfWeek(CurrentDate) >= 5 then Date.AddDays(CurrentDate, -Date.DayOfWeek(CurrentDate) - 3 + 7) else Date.AddDays(CurrentDate, -Date.DayOfWeek(CurrentDate) - 3), type date)

 

The default DayOfWeek is zero for Monday to 6 Sunday.

 

EDIT: today is Friday - this will choose the previous Friday, 29 March. You'll have to modify the formula slightly if you want today to return today.

Thank you @belvoir99, it worked for me 
= Table.AddColumn(#"Filtered Rows4", "LastFriday", each if Date.DayOfWeek(DateTime.Date( DateTime.LocalNow() )) >= 5 then Date.AddDays(DateTime.Date( DateTime.LocalNow() ), -Date.DayOfWeek(DateTime.Date( DateTime.LocalNow() )) - 3 + 7) else Date.AddDays(DateTime.Date( DateTime.LocalNow() ), -Date.DayOfWeek(DateTime.Date( DateTime.LocalNow() )) - 3), type date)

Hi @SonaSinghA above is the code you need to add in Power Query. Go to Transform Data > Advanced Editor, add a comma to the last step before the 'in' clause, then paste in the code -- something like this beforehand:

TheLastLine = Something Here
in
TheLastLine

and this afterwards:

TheLastLine = Something Here,
CurrentDate = DateTime.Date( DateTime.LocalNow() ),
LastFridayColumn = Table.AddColumn(TheLastLine, "LastFriday", each if Date.DayOfWeek(CurrentDate) >= 5 then Date.AddDays(CurrentDate, -Date.DayOfWeek(CurrentDate) - 3 + 7) else Date.AddDays(CurrentDate, -Date.DayOfWeek(CurrentDate) - 3), type date)
in
LastFridayColumn

things to note: TheLastLine step name is referenced in the LastFridayColumn step

 

if you are still not sure what to do, then paste the last few lines of code from your table and I'll post back final code.  

Ritaf1983
Super User
Super User

Hi @SonaSinghA 
Try to modify the formula to :
Date.AddDays(Date.From(DateTime.LocalNow()),-Date.DayOfWeek(Date.From(DateTime.LocalNow()),5)

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly

 

@Ritaf1983 , still same 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.