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
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
— it helps the community and makes DAX errors feel less lonely

View solution in original post

v-yangliu-msft
Community Support
Community Support

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
v-yangliu-msft
Community Support
Community Support

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
— it helps the community and makes DAX errors feel less lonely

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.