cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Helper I
Helper I

Get total sum 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
Community Support
Community Support

Hi, @israabuhasna 

According to your description, you want to get the sum of the Code change group by year, you can try my steps:

This is my test data:

v-robertq-msft_0-1611306281018.png

 

  1. Create a calculated column:
Change tag =

var _lastcode=

CALCULATE(MAX('Table'[ClassCODE]),FILTER('Table',[Index]=EARLIER('Table'[Index])-1))

return

IF(

    [ClassCODE]<>_lastcode&&[Index]<>1,1,

0)
  1. Create a measure:
Measure =

SUMX(FILTER(ALLSELECTED('Table'),[TaxYear]=MAX('Table'[TaxYear])),[Change tag])

 

  1. Create a Matrix and place it like this:

v-robertq-msft_1-1611306281035.png

 

And you can get what you want.

You can download my test pbix file here

 

Best Regards,

Community Support Team _Robert Qin

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
Community Support
Community Support

Hi, @israabuhasna 

According to your description, you want to get the sum of the Code change group by year, you can try my steps:

This is my test data:

v-robertq-msft_0-1611306281018.png

 

  1. Create a calculated column:
Change tag =

var _lastcode=

CALCULATE(MAX('Table'[ClassCODE]),FILTER('Table',[Index]=EARLIER('Table'[Index])-1))

return

IF(

    [ClassCODE]<>_lastcode&&[Index]<>1,1,

0)
  1. Create a measure:
Measure =

SUMX(FILTER(ALLSELECTED('Table'),[TaxYear]=MAX('Table'[TaxYear])),[Change tag])

 

  1. Create a Matrix and place it like this:

v-robertq-msft_1-1611306281035.png

 

And you can get what you want.

You can download my test pbix file here

 

Best Regards,

Community Support Team _Robert Qin

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

Thank you! It worked like a charm for my problem. Much much appreciated.

Helpful resources

Announcements
secondImage

Congratulations!

We are excited to announce the Power BI Super Users!

Wave Release 2

Check out the updates in Power BI.

Overview of Power BI 2020 release wave 2!

Microsoft Ignite

Microsoft Ignite

Join digitally, March 2–4, 2021 to explore new tech that's ready to implement. Experience the keynote in mixed reality through AltspaceVR!

secondImage

The largest Power BI virtual conference

100+ sessions, 100+ speakers, Product managers, MVPs, and experts. All about Power BI. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors