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
israabuhasna
Helper I
Helper I

Get total count of Code change per Id in a year

Hi. I have the below table

PID

TaxYear

ClassCODE

AX123

2017

I3

AX123

2018

I3

AX123

2019

I3

AX123

2020

C3

AX123

2021

C3

AX2

2017

H2

AX2

2018

H2

AX2

2019

R3

AX2

2020

C5

AX2

2021

C5

AX3

2017

O2

AX3

2018

O2

AX3

2019

I3

AX3

2020

C3

AX3

2021

C3

BX1

2017

C3

BX1

2018

H9

BX1

2019

C3

BX1

2020

C3

BX1

2021

C3

BX2

2017

H2

BX2

2018

H2

BX2

2019

H2

BX2

2020

H2

BX2

2021

C4

 

I would like to add a measure that calculates the total number of Code changes by year so I can add it to a bar chart with the year as an x axis and the sum of code changes as the values. For example Id like a measure that outputs:

2018

1

2019

3

2020

3

2021

1

 

I already have two measures that give me the current years Code and another that gives me last years code change but I want a sum of all code changes by year:

 

CodeChangeLY = 
  VAR 
    CurrentDate = MAX('TOT Tax Parcels'[TaxYear])
  VAR LastYear =
    CALCULATE( 
     MAX('TOT Tax Parcels'[ClassCode]), 
     'TOT Tax Parcels'[TaxYear] = CurrentDate-1,ALL('TOT Tax Parcels'[ClassCode])
    )
CurrentDate,ALL('TOT Tax Parcels'[ClassCode]))
  Return LastYear
CodeChangeCY = VAR 
    CurrentDate = MAX('TOT Tax Parcels'[TaxYear])
    VAR ThisYear = CALCULATE(MAX('TOT Tax Parcels'[ClassCode]),'TOT Tax Parcels'[TaxYear]  = CurrentDate,ALL('TOT Tax Parcels'[ClassCode]))
    Return ThisYear

 

 

1 ACCEPTED SOLUTION
v-henryk-mstf
Community Support
Community Support

Hi @israabuhasna ,


According to the information you provided, I have done the following test as a reference: The created Total measure counts the grouping by PID, the row value corresponding to the ClassCode column in the current year, and finally sum counts the total value of the change in each year.

Total = 
VAR _curyear =
    MAX ( 'Table'[TaxYear] )
VAR _preyear =
    CALCULATE (
        MAX ( 'Table'[TaxYear] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[PID] = MAX ( 'Table'[PID] )
                && 'Table'[TaxYear] < _curyear
        )
    )
VAR _precc =
    CALCULATE (
        MAX ( 'Table'[ClassCODE] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[PID] = MAX ( 'Table'[PID] )
                && 'Table'[TaxYear] = _preyear
        )
    )
VAR CurrentPrice =
    MAX ( 'Table'[ClassCODE] )
RETURN
    IF ( MAX ( 'Table'[ClassCODE] ) <> _precc && NOT ( ISBLANK ( _precc ) ), 1, 0 )
Measure = 
SUMX('Table',[Total])

v-henryk-mstf_0-1611542922932.png

Here is the sample pbix file.


If the problem is still not resolved, please provide detailed error information and let me know immediately, looking forward to your reply.


Best Regards,
Henry


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

3 REPLIES 3
v-henryk-mstf
Community Support
Community Support

Hi @israabuhasna ,


According to the information you provided, I have done the following test as a reference: The created Total measure counts the grouping by PID, the row value corresponding to the ClassCode column in the current year, and finally sum counts the total value of the change in each year.

Total = 
VAR _curyear =
    MAX ( 'Table'[TaxYear] )
VAR _preyear =
    CALCULATE (
        MAX ( 'Table'[TaxYear] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[PID] = MAX ( 'Table'[PID] )
                && 'Table'[TaxYear] < _curyear
        )
    )
VAR _precc =
    CALCULATE (
        MAX ( 'Table'[ClassCODE] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[PID] = MAX ( 'Table'[PID] )
                && 'Table'[TaxYear] = _preyear
        )
    )
VAR CurrentPrice =
    MAX ( 'Table'[ClassCODE] )
RETURN
    IF ( MAX ( 'Table'[ClassCODE] ) <> _precc && NOT ( ISBLANK ( _precc ) ), 1, 0 )
Measure = 
SUMX('Table',[Total])

v-henryk-mstf_0-1611542922932.png

Here is the sample pbix file.


If the problem is still not resolved, please provide detailed error information and let me know immediately, looking forward to your reply.


Best Regards,
Henry


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

parry2k
Super User
Super User

@israabuhasna what is the business rule for counting the code change, sorry it is not super clear



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Sorry for not making it clear in the original post:

If the code changes from any code to another during the 5 years then it counts as a code change. For example for one ID AX2:

AX2

2017

H2

AX2

2018

H2

AX2

2019

R3

AX2

2020

C5

AX2

2021

C5

The code changed from H2 to R3 in 2019 so it counts as 1 code change in 2019. Then in 2020 the code changed from R3 to C5 again counting 1 code change for 2020. So if we only had this single ID i would want the measure to show 1 for 2019 and 1 for 2020 while 0 for the other years.

 

If more explanation is needed, let me know.

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.