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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
U156531
Helper V
Helper V

Need to get previous month from slicer

I need to get the previous month from a slicer. I'm currently using this measure. Its gets the current month but I really need the previous month. Can someone show me how to change this to always retrieve the previous month?

MTDwithcur = IF(
      YEAR([Date]) = YEAR(TODAY()) && MONTH([Date]) = MONTH(TODAY()),
    "Cur Month",
    FORMAT('Calendar'[Date], "yyyy mmmm") )
1 ACCEPTED SOLUTION
v-jianpeng-msft
Community Support
Community Support

Thank you parry2k 

Hi, @U156531 

According to your description, I created three months of sample data:

vjianpengmsft_0-1728355270918.png

First, I created a date table using the following DAX expression:

DateTable = ADDCOLUMNS(CALENDAR(MIN('Table'[Date]),TODAY()),
    "Year",YEAR([Date]),
    "Month",MONTH([Date]),
    "YearMonth",FORMAT([Date],"YYYY-MM")
)

vjianpengmsft_1-1728355386302.png

I created a new calculated column in the Date table using the following DAX expression:

MTDwithcur = IF(
    YEAR([Date])=YEAR(TODAY())&&MONTH([Date])=MONTH(TODAY()),
    "CurrentMonth",
    IF(
        YEAR([Date])=YEAR(TODAY())&&MONTH([Date])+1=MONTH(TODAY()),
        "Previous Month",
        FORMAT([Date],"YYYY-MMMMM")
    )
)

vjianpengmsft_2-1728355467947.png

I created the following relationship between the date table and the fact table:

vjianpengmsft_3-1728355523419.png

The results are as follows:

vjianpengmsft_4-1728355552926.png

vjianpengmsft_5-1728355561856.png

vjianpengmsft_6-1728355572169.png

 

 

 

 

Best Regards

Jianpeng Li

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

3 REPLIES 3
v-jianpeng-msft
Community Support
Community Support

Thank you parry2k 

Hi, @U156531 

According to your description, I created three months of sample data:

vjianpengmsft_0-1728355270918.png

First, I created a date table using the following DAX expression:

DateTable = ADDCOLUMNS(CALENDAR(MIN('Table'[Date]),TODAY()),
    "Year",YEAR([Date]),
    "Month",MONTH([Date]),
    "YearMonth",FORMAT([Date],"YYYY-MM")
)

vjianpengmsft_1-1728355386302.png

I created a new calculated column in the Date table using the following DAX expression:

MTDwithcur = IF(
    YEAR([Date])=YEAR(TODAY())&&MONTH([Date])=MONTH(TODAY()),
    "CurrentMonth",
    IF(
        YEAR([Date])=YEAR(TODAY())&&MONTH([Date])+1=MONTH(TODAY()),
        "Previous Month",
        FORMAT([Date],"YYYY-MMMMM")
    )
)

vjianpengmsft_2-1728355467947.png

I created the following relationship between the date table and the fact table:

vjianpengmsft_3-1728355523419.png

The results are as follows:

vjianpengmsft_4-1728355552926.png

vjianpengmsft_5-1728355561856.png

vjianpengmsft_6-1728355572169.png

 

 

 

 

Best Regards

Jianpeng Li

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

parry2k
Super User
Super User

@U156531 not sure if I understood but try this:

 

MTDwithCur = 
VAR __EOMThisMonth = EOMONTH( TODAY (), 0 )
VAR __EOMLastMonth = EOMONTH ( TODAY(), -1 )
VAR __EOMDate = EOMONTH ( Table[Date], 0 )
RETURN
SWITCH ( 
   TRUE (),
   __EOMDate = __EOMTHisMonth = "Current Month",
   __EOMDate = __EOMTPrevMonth = "Previous Month",
   FORMAT ( __EOMDate, "yyyy mmmm" )
)


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

I tried using data type Text, Date and whole number but I keep getting this error.

U156531_0-1728048674252.png

 

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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