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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Toora
New Member

A Measure That Tracks changes in data over a year

Hi, I'm trying to come up with an expression that captures when a customer changes branch. See table below which reports on when customers made a visit. Basically I need the measure will return "1" for customer Z2225 as they moved branch

Date                 Customer Code         Branch_Code

01/01/2022L55LIM
02/01/2022A11BEL
03/01/2022A55BEL
04/01/2022552135051LIM
05/01/2022Z2225BEL
06/01/2023Z2225LIM
07/01/2023s22BEL
08/01/2023d22BEL
2 ACCEPTED SOLUTIONS
timalbers
Super User
Super User

Hi @Toora 

try something like this as a new calculated column:

branch_change = 
VAR prev_date = 
    MINX( 
        TOPN( 
            1, 
            FILTER( your_table, your_table[Customer Code] = EARLIER ( your_table[Customer Code] ) && your_table[Date] < EARLIER( your_table[Date] ) ),
            your_table[Date], 
            DESC
        ),
        your_table[Date]
    )
VAR prev_branch = 
    LOOKUPVALUE( your_table[Branch_Code], your_table[Date], prev_date )
VAR change =
    SWITCH( TRUE(),
        prev_branch = BLANK(), BLANK(),
        prev_branch <> your_table[Branch_Code], 1
    )
RETURN change

 Remeber replacing "your_table" with your actual table name.

 

If this post helped you, please consider marking it as solution, thank you!

Cheers

Tim


—————————————————————————————
✔️ If my answer helped you, please consider marking it as a solution.

View solution in original post

Anonymous
Not applicable

Hi  @Toora ,

 

Here are the steps you can follow:

1. Create measure.

Measure =
var _count=COUNTX(FILTER(ALL('Table'),'Table'[Customer Code]=MAX('Table'[Customer Code])),[Branch_Code])
var _maxdate=MAXX(FILTER(ALL('Table'),'Table'[Customer Code]=MAX('Table'[Customer Code])),[Date])
var _mindate=MAXX(FILTER(ALL('Table'),'Table'[Customer Code]=MAX('Table'[Customer Code])&&'Table'[Date]<_maxdate),[Date])
var _newbranch=MAXX(FILTER(ALL('Table'),'Table'[Customer Code]=MAX('Table'[Customer Code])&&'Table'[Date]=_maxdate),[Branch_Code])
var _oldbranch=MAXX(FILTER(ALL('Table'),'Table'[Customer Code]=MAX('Table'[Customer Code])&&'Table'[Date]=_mindate),[Branch_Code])
return
IF(
  _count>1&&  MAX('Table'[Date])=_maxdate&&_newbranch<>_oldbranch,1,0)

2. Result:

vyangliumsft_0-1709011743793.png

 

 

Best Regards,

Liu Yang

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

2 REPLIES 2
Anonymous
Not applicable

Hi  @Toora ,

 

Here are the steps you can follow:

1. Create measure.

Measure =
var _count=COUNTX(FILTER(ALL('Table'),'Table'[Customer Code]=MAX('Table'[Customer Code])),[Branch_Code])
var _maxdate=MAXX(FILTER(ALL('Table'),'Table'[Customer Code]=MAX('Table'[Customer Code])),[Date])
var _mindate=MAXX(FILTER(ALL('Table'),'Table'[Customer Code]=MAX('Table'[Customer Code])&&'Table'[Date]<_maxdate),[Date])
var _newbranch=MAXX(FILTER(ALL('Table'),'Table'[Customer Code]=MAX('Table'[Customer Code])&&'Table'[Date]=_maxdate),[Branch_Code])
var _oldbranch=MAXX(FILTER(ALL('Table'),'Table'[Customer Code]=MAX('Table'[Customer Code])&&'Table'[Date]=_mindate),[Branch_Code])
return
IF(
  _count>1&&  MAX('Table'[Date])=_maxdate&&_newbranch<>_oldbranch,1,0)

2. Result:

vyangliumsft_0-1709011743793.png

 

 

Best Regards,

Liu Yang

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

timalbers
Super User
Super User

Hi @Toora 

try something like this as a new calculated column:

branch_change = 
VAR prev_date = 
    MINX( 
        TOPN( 
            1, 
            FILTER( your_table, your_table[Customer Code] = EARLIER ( your_table[Customer Code] ) && your_table[Date] < EARLIER( your_table[Date] ) ),
            your_table[Date], 
            DESC
        ),
        your_table[Date]
    )
VAR prev_branch = 
    LOOKUPVALUE( your_table[Branch_Code], your_table[Date], prev_date )
VAR change =
    SWITCH( TRUE(),
        prev_branch = BLANK(), BLANK(),
        prev_branch <> your_table[Branch_Code], 1
    )
RETURN change

 Remeber replacing "your_table" with your actual table name.

 

If this post helped you, please consider marking it as solution, thank you!

Cheers

Tim


—————————————————————————————
✔️ If my answer helped you, please consider marking it as a solution.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.