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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
RanHo
Helper V
Helper V

HOW TO GET LAST YEAR DATA IF FILTER CURRENT YEAR

Good Day!

   I have a problem wherein I need to get the last year code if I will use or filter the current year, this are my sample data:

  USER TABLE 
    
IDUSER NAMETRANSACTION CODE TRANSACTION DATE
1001PaulAC-123895/13/2023
1002DennisAC-129805/27/2023
1003GregoryAC-129826/2/2023
1004NathanAC-129916/18/2023
1005ScottAC-130016/27/2023
1006DennisAC-130585/27/2024
1007GregoryAC-132726/2/2024
1008PaulAC-135995/13/2024
1009ScottAC-136016/27/2024
1010NathanAC-136686/18/2024

 



as you can see I have users with transaction code with diff year, I need to create column or measure to display last year transaction code when I'm using current year as a filter.

Sample result: in this table let just say I will use filter = TRANSACTION YEAR =  2024
then it will show transaction code for 2023, also need to compare 2023 and 2024 transaction code side by side. Is it possible?

  TABLE 
NAMEID2023 TRANSACTION CODE2024 TRANSACTION CODE
Paul1001AC-12389AC-13599
Dennis1002AC-12980AC-13058
Gregory1003AC-12982AC-13272
Nathan1004AC-12991AC-13668
Scott1005AC-13001AC-13601


Thanks in advance!
~Ran

1 ACCEPTED SOLUTION
Ritaf1983
Super User
Super User

Hi @RanHo 
You can create a date table :

Ritaf1983_0-1725606939159.png

and 2 measures :

This year code = max ('Table'[TRANSACTION CODE ])
PY code = CALCULATE([This year code], PREVIOUSYEAR('Date'[Date]))
Result :
Ritaf1983_2-1725607044530.png

Note that you have 2 id's for every name ...

Ritaf1983_3-1725607094967.png

I aggregated them to the first name, but you can solve it in other ways according to the needed logic

Ritaf1983_4-1725607182828.png

The pbix is attached


 

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

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

View solution in original post

6 REPLIES 6
Ritaf1983
Super User
Super User

Hi @RanHo 
You can create a date table :

Ritaf1983_0-1725606939159.png

and 2 measures :

This year code = max ('Table'[TRANSACTION CODE ])
PY code = CALCULATE([This year code], PREVIOUSYEAR('Date'[Date]))
Result :
Ritaf1983_2-1725607044530.png

Note that you have 2 id's for every name ...

Ritaf1983_3-1725607094967.png

I aggregated them to the first name, but you can solve it in other ways according to the needed logic

Ritaf1983_4-1725607182828.png

The pbix is attached


 

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

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

@Ritaf1983 Thanks a lot! Your solution worked.

@Ritaf1983  I'll try this and get back to you.

@bhanu_gautam I'll try this and get back to you.

bhanu_gautam
Super User
Super User

@RanHo  , First extract year from transaction date Create a calculated column

    Year = YEAR('USER TABLE'[TRANSACTION DATE])
 
Then create a calculated column to get the transaction code from the previous year.
Previous Year Transaction Code =
VAR CurrentYear = 'USER TABLE'[Year]
VAR UserID = 'USER TABLE'[ID]
RETURN
CALCULATE(
MAX('USER TABLE'[TRANSACTION CODE]),
FILTER(
'USER TABLE',
'USER TABLE'[ID] = UserID &&
'USER TABLE'[Year] = CurrentYear - 1
)
)
 
create a table visual in Power BI and add the USER NAME, ID, Previous Year Transaction Code, and TRANSACTION CODE columns. Apply a filter on the Year column to show only the current year




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

Proud to be a Super User!




LinkedIn






@bhanu_gautam I already tried your solution but I'm getting no data, no data display on table maybe I'll try to recheck. But thanks for giving solution, appreciate it.

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

Check out the September 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.

Sept NL Carousel

Fabric Community Update - September 2024

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

Top Solution Authors
Top Kudoed Authors