cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Giorgi1989
Helper III
Helper III

Year to previous month end - AddColumns

Good day to all,

 

 

I am trying to build dynamic time filters: Month to Date, Previous Month, Rolling 12 Months, Year to Date, and Year to Previous Month. I have an issue with the latter only. All of the existing sales data would be shown on the basis of the selected time period. 

 

Giorgi1989_0-1639395416523.png

 

As I said above, I am struggling with year to previous month end. I have been trying with DATESINPERIOD and DATESINBETWEEN to no avail. Any guidance would be highly appreciated. 

 
How could we modify the following formula for year to previous month end
 
ADDCOLUMNS(
DATESYTD('Date'[Calendar Year/Month Level 01.Key]),
"Selection", "Year to Date"
),
5 REPLIES 5
v-yalanwu-msft
Community Support
Community Support

Hi, @Giorgi1989 ;

Sometimes when DATESINPERIOD and DATESINBETWEEN do not work, you can use similar statements instead.

filter(all('date'),Year([date])=Year(max('date'[date]))&&month([date])<month(max('date'[date]))


Second, your expression addcolumn is a table measure. If you want the same function as the image, as ValtteriN said, use

SWITCH(Max (),"Previous", if(condition, 1,0)...


Set filter in this Visual to 1.

vyalanwumsft_0-1639626605276.png

If not resolved,  can you provide more information about your table structure and expected output results?

Best Regards,
Community Support Team_ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

ValtteriN
Super User
Super User

Hi,

I am not completely sure I understood what you are trying to do but nevertheless here is DAX that returns 1 if [Date] column is between the first day of this year and last day of previous month and else it returns 0. So just replace 1 with "YTD" and 0 with "Selection" and you should achieve what I understood you are trying to achieve. After you have this column it should be pretty straightforward to make a new table from this column (if that is what you want to do).

ISYTD_LM =
var _sdate = DATE(YEAR(TODAY()),1,1)
var _edate = DATE(YEAR(TODAY()),MONTH(TODAY())-1,DAY(EOMONTH(TODAY(),-1))) return
IF(AND('Calendar'[Date]>=_sdate,'Calendar'[Date]<=_edate),1,0)

I hope this helps to solve your issue and if it does consider accepting this as a solution!




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

Proud to be a Super User!




Hi Valtteri,

Thank you for the reply. 

I have updated my original post to provide some context. 

I see, In that case my previous suggestion should work since it return values for 1.1.2021-30.11.2021.

ISYTD_LM =
var _sdate = DATE(YEAR(TODAY()),1,1)
var _edate = DATE(YEAR(TODAY()),MONTH(TODAY())-1,DAY(EOMONTH(TODAY(),-1))) return
IF(AND('Calendar'[Date]>=_sdate,'Calendar'[Date]<=_edate),"Previous Month","")

However given that you have different slicers I suppose you want your calculations to change based on the selection? If that is the case, I suggest that you build a switch measure in which you have a structure like this:

SWITCH( MAX('Time Slicer'[Selection],
"Year to Date",[Measure YTD],
"Previous Month",[Measure LMYTD] ..... )

(E.g. calculate(sum('Table'[value],DatesYTD(dateadd('calendar'[Date],-1,MONTH)))) )




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

Proud to be a Super User!




I fully akcknowledge that my original post was not detailed enough to fully express my needs or wishes, for which I do apologise. I will give it another try here:

 

The formula I currently have: 

 

Time Selection =
Union(
ADDCOLUMNS(
DATESMTD('Date'[Calendar Year/Month Level 01.Key]),
"Selection", "Month to Date"
),
ADDCOLUMNS(
DATESYTD('Date'[Calendar Year/Month Level 01.Key]),
"Selection", "Year to Date"
),
ADDCOLUMNS(
PREVIOUSMONTH(DATESMTD('Date'[Calendar Year/Month Level 01.Key])),
"Selection", "Previous Month"
),
ADDCOLUMNS(
DATESINPERIOD('Date'[Calendar Year/Month Level 01.Key],MAX('Date'[Calendar Year/Month Level 01.Key]),-12,Month),
"Selection", "Rolling 12 months"
))
 
This leads to the tabe which looks as follows (which is in turn related to my main date table):
 
Giorgi1989_0-1639736273205.png

 

Then I use the chiclet slicer to filter data across all different categories, which looks like this:

 

Giorgi1989_1-1639736337805.png

These are all working great, and filter data as expected. 

 

However, despite my various attempts, I still don't seem to get the 'Year to Previous Month' formula working.

Helpful resources

Announcements
August 2022 update 768x460.jpg

Check it Out!

Click here to learn more about the August 2022 updates!

August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community 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.

Top Solution Authors
Top Kudoed Authors