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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
rbalza
Helper III
Helper III

Help - display previous week from a certain given date in power query

Hi Folks,

So I have an excel file that looks like this and when I unpivot the day name (Sunday,Monday,Tuesday...), it should automatically calculate the previous week based on the given timesheet date. Say, the given timesheet date is 11.Apr.2021, it should calculate week backward and display it on another column which should be from (Sunday) 4.Apr.2021 until 10.Apr.2021 (Saturday).

01.JPG

And also, I was having a hard time to think about how am I going to do with the Pay Wages, Pay Rate and Margin column when I unpviot it, it comes like on the picture below.

 

I was actually trying to do a employee time calculation and its wages.  Thanks alot.

02.JPG

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @rbalza 

You can unpivot weekday columns and add a date column to the table to make it clearer.

    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Name", "Timesheet Date", "Pay Wages", "Pay Rate", "Margin"}, "Attribute", "Value"),
    #"Added Conditional Column" = Table.AddColumn(#"Unpivoted Columns", "Custom", each if [Attribute] = "Sunday" then [Timesheet Date]-#duration(7,0,0,0) else if [Attribute] = "Monday" then [Timesheet Date]-#duration(6,0,0,0) else if [Attribute] = "Tuesday" then [Timesheet Date]-#duration(5,0,0,0) else if [Attribute] = "Wednesday" then [Timesheet Date]-#duration(4,0,0,0) else if [Attribute] = "Thursday" then [Timesheet Date]-#duration(3,0,0,0) else if [Attribute] = "Friday" then [Timesheet Date]-#duration(2,0,0,0) else if [Attribute] = "Saturday" then [Timesheet Date]-#duration(1,0,0,0) else null)

041904.jpg

Regards,
Community Support Team _ Jing
If this post helps, please Accept it as the solution to help other members find it.

View solution in original post

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @rbalza 

You can unpivot weekday columns and add a date column to the table to make it clearer.

    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Name", "Timesheet Date", "Pay Wages", "Pay Rate", "Margin"}, "Attribute", "Value"),
    #"Added Conditional Column" = Table.AddColumn(#"Unpivoted Columns", "Custom", each if [Attribute] = "Sunday" then [Timesheet Date]-#duration(7,0,0,0) else if [Attribute] = "Monday" then [Timesheet Date]-#duration(6,0,0,0) else if [Attribute] = "Tuesday" then [Timesheet Date]-#duration(5,0,0,0) else if [Attribute] = "Wednesday" then [Timesheet Date]-#duration(4,0,0,0) else if [Attribute] = "Thursday" then [Timesheet Date]-#duration(3,0,0,0) else if [Attribute] = "Friday" then [Timesheet Date]-#duration(2,0,0,0) else if [Attribute] = "Saturday" then [Timesheet Date]-#duration(1,0,0,0) else null)

041904.jpg

Regards,
Community Support Team _ Jing
If this post helps, please Accept it as the solution to help other members find it.

lbendlin
Super User
Super User

You could unpivot only the weekday columns, but it would be much better if you would use a proper data model and separate the facts out from the dimension data.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors