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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
dokat
Post Prodigy
Post Prodigy

Last month function with dax

Hi,

 

I have a table that i am trying to add a column based on a condition. In the New column i want it to show "Last Year", "Last Month" and "YTD" based on the calue under CY columns.

 

I have a formula that works for the most part except last month. It doesnt return latest months value based CY. Can anyone help with below?

 

New Column = var max_date = calculate(max(Tier[CY]),all()) return switch(true(),
Tier[CY]= max_date,"YTD",
and(MONTH(Tier[CY]) = month(max_date),YEAR(Tier[CY]) = YEAR(max_date)),"Last Month",
Tier[CY]= DATE ( YEAR (max_date)-1, 12, 31 ),"Last Year")

 

Below is what i want final table to look like

 

CYTierSalesVolumeNew Column
12/31/2018225125 
12/31/2019250200 
12/31/20201100500 
12/31/20211200600Last Year
1/1/20213300700 
1/1/20223400800 
2/28/20213500900 
2/1/202236001000Last Month
2/28/2022310001800YTD
1 ACCEPTED SOLUTION
dokat
Post Prodigy
Post Prodigy

Corrected data table and below is working now

 

New Column = var max_date = calculate(max(Tier[CY]),all()) return switch(true(),
Tier[CY]= max_date,"YTD",
and(MONTH(Tier[CY]) = month(max_date),YEAR(Tier[CY]) = YEAR(max_date)),"Last Month",
Tier[CY]= DATE ( YEAR (max_date)-1, 12, 31 ),"Last Year")

View solution in original post

1 REPLY 1
dokat
Post Prodigy
Post Prodigy

Corrected data table and below is working now

 

New Column = var max_date = calculate(max(Tier[CY]),all()) return switch(true(),
Tier[CY]= max_date,"YTD",
and(MONTH(Tier[CY]) = month(max_date),YEAR(Tier[CY]) = YEAR(max_date)),"Last Month",
Tier[CY]= DATE ( YEAR (max_date)-1, 12, 31 ),"Last Year")

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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