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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
v-rzhou-msft
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
v-rzhou-msft
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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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