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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
MT54
New Member

Percentage Difference Formula Help

Hi, 

 

I have searched the forums but didn't find anything that was what I'm trying to do and my attempts to change other formulae and use the quick measures haven't worked. I'm trying to create the two columns shown in the table below "Difference" and % Difference, I've populated these with my expected values already but can't do this using Power BI.

 

The step column is used as a counter and first "Step" is always 1 and the last "Step" is always 6.

 

For the "Difference" column, every step except 6 and 1 needs to subtract its value from the previous step "value" (1 just equals 0 and 6 needs to subtract from 1's value to get the overall change).  

 

For the "% Difference" column, every step except 6 and 1 should be Difference divided by the previous step "Value"  (1 just equals 0 and 6 needs to divide by 1's value to get the overall % diff).  

 

Please can anyone help? I'm running out of ideas!

 

Thanks, Mike

 

CategoryStepValueDifference% difference
A110000%
A210555%
A310611%
A480-26-25%
A690-10-10%
B15000%
B255510%
B340-15-27%
B4602050%
B5802033%
B6904080%
1 ACCEPTED SOLUTION
v-rzhou-msft
Community Support
Community Support

Hi @MT54 ,

 

I suggest you to try code as below to create calculated columns.

Difference = 
VAR _1 = 0
VAR _2to5 =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Category] ),
            'Table'[Step]
                = EARLIER ( 'Table'[Step] ) - 1
        )
    )
VAR _6 =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER ( ALLEXCEPT ( 'Table', 'Table'[Category] ), 'Table'[Step] = 1 )
    )
RETURN
    IF (
        'Table'[Step] = 1,
        _1,
        IF ( 'Table'[Step] = 6, 'Table'[Value] - _6, 'Table'[Value] - _2to5 )
    )
% difference = 
VAR _1 = 0
VAR _2to5 =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Category] ),
            'Table'[Step]
                = EARLIER ( 'Table'[Step] ) - 1
        )
    )
VAR _6 =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER ( ALLEXCEPT ( 'Table', 'Table'[Category] ), 'Table'[Step] = 1 )
    )
RETURN
    IF (
        'Table'[Step] = 1,
        _1,
        DIVIDE ( 'Table'[Difference], IF ( 'Table'[Step] = 6, _6, _2to5 ) )
    )

Result is as below.

vrzhoumsft_0-1682485992760.png

 

Best Regards,
Rico Zhou

 

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

1 REPLY 1
v-rzhou-msft
Community Support
Community Support

Hi @MT54 ,

 

I suggest you to try code as below to create calculated columns.

Difference = 
VAR _1 = 0
VAR _2to5 =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Category] ),
            'Table'[Step]
                = EARLIER ( 'Table'[Step] ) - 1
        )
    )
VAR _6 =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER ( ALLEXCEPT ( 'Table', 'Table'[Category] ), 'Table'[Step] = 1 )
    )
RETURN
    IF (
        'Table'[Step] = 1,
        _1,
        IF ( 'Table'[Step] = 6, 'Table'[Value] - _6, 'Table'[Value] - _2to5 )
    )
% difference = 
VAR _1 = 0
VAR _2to5 =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Category] ),
            'Table'[Step]
                = EARLIER ( 'Table'[Step] ) - 1
        )
    )
VAR _6 =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER ( ALLEXCEPT ( 'Table', 'Table'[Category] ), 'Table'[Step] = 1 )
    )
RETURN
    IF (
        'Table'[Step] = 1,
        _1,
        DIVIDE ( 'Table'[Difference], IF ( 'Table'[Step] = 6, _6, _2to5 ) )
    )

Result is as below.

vrzhoumsft_0-1682485992760.png

 

Best Regards,
Rico Zhou

 

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

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

Find out what's new and trending in the Fabric Community.