cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Syndicate_Admin
Administrator
Administrator

power query: current year to date versus previous year for the same period

Hello

I have a 2 year sales table (previous year to this day).

Example: today is 22/01/2022

I want to filter in power Query the column "FECHA_VENTA" in such a way to get only the sales from:

- from January 1 of the Current Year to Diadehoy/mesactual/current year (that is, from 01/01/2022 to 22/01/2022))

- and the same period but last year:

- from January 1 of the YearAnterior to Diadehoy/mesactual/añoaPrevious (that is, from 01/01/2021 to 22/01/2021)

you can please help me to correctly put these conditions in the date filter

1 ACCEPTED SOLUTION
RicoZhou
Community Support
Community Support

Hi @Syndicate_Admin ,

 

Here I suggest you to create a custom column by M code and then filter your table by this column. DateTime.LocalNow can help you get today's datetime, it is dynamic.

M Code:

Custom Filter
=
if 
(
Date.Year ([FECHA_VENTA])= Date.Year ( DateTime.LocalNow()) 
or

Date.Year ([FECHA_VENTA]) = Date.Year ( DateTime.LocalNow()) -1 
)

and 

Date.Month([FECHA_VENTA]) <= Date.Month (DateTime.LocalNow()) 

and

Date.Day([FECHA_VENTA]) <= Date.Day (DateTime.LocalNow()) 

then "Ture"

else "False"

Then select "True" in [Custom Filter] column.

1.png

Best Regards,
Rico Zhou

 

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

 

View solution in original post

2 REPLIES 2
RicoZhou
Community Support
Community Support

Hi @Syndicate_Admin ,

 

Here I suggest you to create a custom column by M code and then filter your table by this column. DateTime.LocalNow can help you get today's datetime, it is dynamic.

M Code:

Custom Filter
=
if 
(
Date.Year ([FECHA_VENTA])= Date.Year ( DateTime.LocalNow()) 
or

Date.Year ([FECHA_VENTA]) = Date.Year ( DateTime.LocalNow()) -1 
)

and 

Date.Month([FECHA_VENTA]) <= Date.Month (DateTime.LocalNow()) 

and

Date.Day([FECHA_VENTA]) <= Date.Day (DateTime.LocalNow()) 

then "Ture"

else "False"

Then select "True" in [Custom Filter] column.

1.png

Best Regards,
Rico Zhou

 

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

 

vanessafvg
Super User
Super User

I am not sure you can do this in power query.   Can you just bring back the last 2 years data and then  manage  the last year and this year combinations using dax in your measures?

 

you could probably create a function to do this but i think it would be a custom function.  





If I took the time to answer your question and I came up with a solution, please mark my post as a solution and /or give kudos freely for the effort 🙂 Thank you!

Proud to be a Super User!




Helpful resources

Announcements
June 2022 update 768X460.jpg

Check it out!

Click here to read more about the June 2022 updates!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power BI Dev Camp Session 23 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, June 30 at 11a PDT for a great session with Ted Pattison!

June 20 episode 7 with aka link 768x460.jpg

The Power BI Community Show

Join us on June 20 at 11 am PDT when Kim Manis shares the latest on Azure Synapse analytics, the Microsoft Intelligent Data Platform, and notable Power BI Updates from Microsoft Build 2022.

Top Solution Authors