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, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
DevLearner0
Frequent Visitor

Column for previous years months

Hi everyone,

 

I have a typical date table and another table with the column "Year", another one "Quantity" and a last one called "Sales".

 

I need to create a matrix visual where the rows you can see in the image below: The month-year selected in a slicer, the data of all past year (Not only december but I can't call it anything else than that), the data of 2 years ago, etc.

 

I have been able to create a measure that can display it correctly, but measures can't be used as rows in these visuals, so it has to be a column.

 

Captura de pantalla 2024-08-06 120210.png

 

 

 

 

 

 

 

 

The measure I created is

Previous_Years = 
var id_year_SALES = SELECTEDVALUE(SalesTable[ID_YEAR])
var id_year_DATE = SELECTEDVALUE(DIM_CALENDAR[NUM_YEAR])

RETURN
SWITCH(id_year_DATE - id_year_SALES,
0, FORMAT(SELECTEDVALUE(DIM_CALENDAR[DATE]), "MMMM YY"),
1, CONCATENATE("DECEMBER ", id_year_DATE - 1),
2, CONCATENATE("DECEMBER ", id_year_DATE - 2),
3, CONCATENATE("DECEMBER ", id_year_DATE - 3), 
"Error")

I didn't even try the part with "Difference with Dec. Previous Year".

 

If you need any more detail or explanation feel free to ask.

 

Thank you!

1 ACCEPTED SOLUTION
bhanu_gautam
Super User
Super User

@DevLearner0 , Create a date table if you do not have

 

DateTable =
ADDCOLUMNS (
CALENDAR (DATE (2000, 1, 1), DATE (2030, 12, 31)),
"Year", YEAR ( [Date] ),
"Month", MONTH ( [Date] ),
"MonthYear", FORMAT ( [Date], "MMM YYYY" )
)

 

Add calculated columns to your SalesTable to represent the different periods. Here’s how you can do it:

CurrentYear = YEAR(TODAY())

MonthYear = FORMAT(SalesTable[Date], "MMM YYYY")

PreviousYear =
IF (
SalesTable[Year] = CurrentYear - 1,
"DECEMBER " & (CurrentYear - 1),
BLANK()
)

TwoYearsAgo =
IF (
SalesTable[Year] = CurrentYear - 2,
"DECEMBER " & (CurrentYear - 2),
BLANK()
)

ThreeYearsAgo =
IF (
SalesTable[Year] = CurrentYear - 3,
"DECEMBER " & (CurrentYear - 3),
BLANK()
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






View solution in original post

1 REPLY 1
bhanu_gautam
Super User
Super User

@DevLearner0 , Create a date table if you do not have

 

DateTable =
ADDCOLUMNS (
CALENDAR (DATE (2000, 1, 1), DATE (2030, 12, 31)),
"Year", YEAR ( [Date] ),
"Month", MONTH ( [Date] ),
"MonthYear", FORMAT ( [Date], "MMM YYYY" )
)

 

Add calculated columns to your SalesTable to represent the different periods. Here’s how you can do it:

CurrentYear = YEAR(TODAY())

MonthYear = FORMAT(SalesTable[Date], "MMM YYYY")

PreviousYear =
IF (
SalesTable[Year] = CurrentYear - 1,
"DECEMBER " & (CurrentYear - 1),
BLANK()
)

TwoYearsAgo =
IF (
SalesTable[Year] = CurrentYear - 2,
"DECEMBER " & (CurrentYear - 2),
BLANK()
)

ThreeYearsAgo =
IF (
SalesTable[Year] = CurrentYear - 3,
"DECEMBER " & (CurrentYear - 3),
BLANK()
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

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! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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