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
May 23 2022 epsiode 5 without aka link.jpg

The Power BI Community Show

Welcome to the Power BI Community Show! Jeroen ter Heerdt talks about the importance of Data Modeling.

charticulator_carousel_with_text (1).png

Charticulator Design Challenge

Put your data visualization and design skills to the test! This exciting challenge is happening now through June10th!

Power BI Dev Camp Session 22 without aka link and time 768x460.jpg

Check it Out!

Watch Session 22 Ted's Dev Camp along with past sessions!

Power BI Release May 2022 768x460.png

Check it out!

Click here to read more about the May 2022 updates!

Top Kudoed Authors