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 August 31st. Request your voucher.

Reply
Krishna_Newuser
Resolver I
Resolver I

Week Filter in Power Query Editor(M Code)

Hello,

My report will be refreshed once a week (Monday) and it will have the data for the previous week.
For instance, if I’m refreshing the data on (9th December,2024) it will have the data from (1st Dec to 7th Dec). To filter this exact date in Power Query Editor I have used the below condition.

= Table.AddColumn(#"Inserted Date", "Duration", each if [TxnDt] >= Date.From(Date.StartOfWeek(DateTime.LocalNow() - #duration(7, 0, 0, 0),Day.Sunday))

and [TxnDt] <= Date.From(Date.StartOfWeek(DateTime.LocalNow()- #duration(1, 0, 0, 0),Day.Saturday))

then "Yes" else "No")

 

Now, I needed ensure that the report will have to filter the previous week of the last week data(24th Nov to 30th Nov 2024) and just tired to change the M Code as below but its not giving the required filter.

= Table.AddColumn(#"Inserted Date", "Duration", each if [TxnDt] >= Date.From(Date.StartOfWeek(DateTime.LocalNow() - #duration(14, 0, 0, 0),Day.Sunday))

and [TxnDt] <= Date.From(Date.StartOfWeek(DateTime.LocalNow()- #duration(7, 0, 0, 0),Day.Saturday))

then "Yes" else "No")

Can you please help me to pull the data for this.

4 REPLIES 4
danextian
Super User
Super User

Hi @Krishna_Newuser 

Use the following

EndDate

 let 
            //replace 8 with your UTC offset
            CurrentDate = Date.From(DateTimeZone.RemoveZone(DateTimeZone.SwitchZone(DateTimeZone.UtcNow(),8))),  
            DayOfWeek = Date.DayOfWeek(CurrentDate, Day.Sunday),  // Get the day of the week (0 = Sunday, 1 = Monday, ...)
            DaysToSubtract = if DayOfWeek = 0 then 7 else DayOfWeek,  // Calculate how many days to subtract to get to the previous Sunday
            PreviousSunday = Date.AddDays(CurrentDate, -DaysToSubtract)  // Subtract the necessary days
        in
            PreviousSunday

StartDate

Date.AddDays(EndDate, -6)

 

They can be in separate quries.

Filter your table like your nomally would but between ranges. Replace the star and end dates with those two queries.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Hi @danextian 
Thank you for the reply, 
I tried it but didnt worked, not sure what wrong i have done. Is there any possible that M code be changed in my query or will it be possible to share any PBI report from your end.

 

Thanks in advance!!!

The queries are just a simple as these

danextian_0-1733410672126.png

danextian_1-1733410692351.png

Name the queries ans EndDate and StartDate. Filter your table using date ranges and change the start and end  dates. These queries will work just like parameters sans manually indicating the value.

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Hello,

Its just replacing all the steps from the applied steps, can you help me here.

Krishna_Newuser_1-1733461631774.png

 

Krishna_Newuser_0-1733461541569.png

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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