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

Year over year change in type string calculation

I would like to create a measure that compares data from two different years (in a single column) and returns one of four types of change then plot the proportions of change type in a pie chart (i.e count of ID per change type)

 

Here is my problem:

 

I have data from two years appended into one table. I’m trying to compare the results from one year to the next and create a field that indicates the change for each row and assigns one of the following:

“no change – result 1”

“no change – result 2”
“change – increase”

“change – decrease”

 

The data types of “year” and “result” are text.

 

Here is my data structure:

 

ID

Year

Result

1

2021

1

2

2021

2

3

2021

1

4

2021

2

1

2022

1

2

2022

2

3

2022

2

4

2022

1

 

Results – measure

 

ID

YoY

1

No change – result 1

2

No change – result 2

3

Change – increase

4

Change – decrease

 

I then want to be able to summarize the measure and show counts of ID per YoY result.

 

Hoping someone could describe the best way to achieve this! 

1 ACCEPTED SOLUTION
Barthel
Solution Sage
Solution Sage

Hey @apelleti,

Add an table visual and put the 'ID' in it.

Barthel_0-1672334819075.png

Then create a measure that assigns the label to the 'ID'.

 

 

Measure = 
VAR _2022 =
    CALCULATE ( 
        SELECTEDVALUE ( 'Table'[Result] ),
        'Table'[Year] = "2022"
    )
VAR _2021 =
    CALCULATE ( 
        SELECTEDVALUE ( 'Table'[Result] ),
        'Table'[Year] = "2021"
    )
VAR _result =
    SWITCH ( 
        TRUE,
        _2022 = "1" && _2021 = "1", "No change – result 1",
        _2022 = "2" && _2021 = "2", "No change – result 2",
        _2022 = "2" && _2021 = "1", "Change – increase",
        _2022 = "1" && _2021 = "2", "Change – decrease"
    )
RETURN
    _result

 

 

Place the measure in the table visual for the desired result.

Barthel_1-1672334893926.png

For your second request, we need to add a new table. I named this one 'Table (2)'.

YoY

No change – result 1
No change – result 2
Change – increase
Change – decrease

Place this new table in a table visual.

Barthel_2-1672334992985.png

Create a second measure to calculate the number.

 

 

Measure 2 =
SUMX (
    'Table (2)',
    COUNTROWS (
        FILTER ( DISTINCT ( 'Table'[ID] ), [Measure] = EARLIER ( 'Table (2)'[YoY] ) )
    )
)

 

 

Finally, place the measure in the visual.

Barthel_3-1672335088505.png

View solution in original post

2 REPLIES 2
Barthel
Solution Sage
Solution Sage

Hey @apelleti,

Add an table visual and put the 'ID' in it.

Barthel_0-1672334819075.png

Then create a measure that assigns the label to the 'ID'.

 

 

Measure = 
VAR _2022 =
    CALCULATE ( 
        SELECTEDVALUE ( 'Table'[Result] ),
        'Table'[Year] = "2022"
    )
VAR _2021 =
    CALCULATE ( 
        SELECTEDVALUE ( 'Table'[Result] ),
        'Table'[Year] = "2021"
    )
VAR _result =
    SWITCH ( 
        TRUE,
        _2022 = "1" && _2021 = "1", "No change – result 1",
        _2022 = "2" && _2021 = "2", "No change – result 2",
        _2022 = "2" && _2021 = "1", "Change – increase",
        _2022 = "1" && _2021 = "2", "Change – decrease"
    )
RETURN
    _result

 

 

Place the measure in the table visual for the desired result.

Barthel_1-1672334893926.png

For your second request, we need to add a new table. I named this one 'Table (2)'.

YoY

No change – result 1
No change – result 2
Change – increase
Change – decrease

Place this new table in a table visual.

Barthel_2-1672334992985.png

Create a second measure to calculate the number.

 

 

Measure 2 =
SUMX (
    'Table (2)',
    COUNTROWS (
        FILTER ( DISTINCT ( 'Table'[ID] ), [Measure] = EARLIER ( 'Table (2)'[YoY] ) )
    )
)

 

 

Finally, place the measure in the visual.

Barthel_3-1672335088505.png

Thank you @Barthel ! Works like a charm! 

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.