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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

LookUP the person's UNIT if he exists in the last year

Hello everyone,

 
I'm trying to find the previous year's (YEAR-1) UNIT for each PERSON ID in each row.
Can someone help me with this? How can I do this using a MEASURE?  I'm so new to power BI, struggling with various approaches.
 

ACTUAL DATA:

 

 

 

PERSON ID | DATE | UNIT

 

 

 

123 | 01-01-2021 | SBS-EM

 

123 | 01-01-2020 | SBS-EM

 

123 | 01-01-2018 | SBS-EM

 

123 | 01-01-2017 | SBS-EM

 

789 | 01-01-2021 | XYZ

 

789 | 01-01-2020 | XYZ

 

789 | 01-01-2019 | XYZ

 

789 | 01-01-2018 | XYZ

 

789 | 01-01-2017 | XYZ

 

 

 

DESIRED OUTPUT (PREVIOUS YEAR UNIT column):

 

 

 

PERSON ID | DATE | UNIT | PREVIOUS YEAR UNIT

 

 

 

123 | 01-01-2021 | SBS-EM | SBS-EM

 

123 | 01-01-2020 | SBS-EM |

 

123 | 01-01-2018 | SBS-EM | SBS-EM

 

123 | 01-01-2017 | SBS-EM |

 

789 | 01-01-2021 | XYZ | XYZ

 

789 | 01-01-2020 | XYZ | XYZ

 

789 | 01-01-2019 | XYZ | XYZ

 

789 | 01-01-2018 | XYZ | XYZ

 

789 | 01-01-2017 | XYZ |          

 

PFB the screenshot of the same.

Screenshot_20220123-105521_Office.jpg

 

 

 
Thanks a ton for your help
 
Regards
Santosh
1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
AlexisOlson
Super User
Super User

I think LOOKUPVALUE should work.

 

As a calculated column, it would look like this:

PY Unit =
LOOKUPVALUE (
    Data[Unit],
    Data[Person ID], Data[Person ID],
    Data[Date], DATE ( YEAR ( Data[Date] ) - 1, 1, 1 )
)

 

There are plenty of other ways to do this though. I might do this if I were writing a measure:

PY Unit =
CALCULATE (
    SELECTEDVALUE ( Data[Unit] ),
    ALLEXCEPT ( Data, Data[Person ID] ),
    DATEADD ( Data[Date], -1, YEAR )
)
Anonymous
Not applicable

Thank you for the quick response and solution.

It almost worked, but even when there is no data in the previous year, this measure is giving the unit.

For example for the person with ID 123 for DATE 01-01-2020,even when there is no record in 2019, the measure is giving PY UNIT as SBS-EM

Odd. I'm not seeing that in my testing.

 

If you have a date dimension (calendar) table, you should be able to simplify it a bit.

PY Unit = 
CALCULATE (
    SELECTEDVALUE ( Data[Unit] ),
    DATEADD ( dimDate[Date], -1, YEAR )
)

You can also use PARALLELPERIOD or SAMEPERIODLASTYEAR instead of DATEADD.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Kudoed Authors