Search code examples
powerbidaxpowerbi-embedded

Calculate Percentage Difference between two given values from the same column in Power BI


I'm struggling to find a solution to get the percentage difference of two given values from the same column.

My data looks like this:

My data Looks like this - Image

At any given situation, how to calculate the difference of count_of_points between two different versions?

Example: Scenario1: I need to calculate the change between V110 and V113. It would be 400 (given V113 has 500 points & V110 has 100 points. Diff = 400)

Scenario2: I need to calculate the change between V112 and V115. It would be 400 (given V115 has 600 points & V112 has 200 points. Diff = 400)

I do this manually every time. Seeking some help on this to automate.


Solution

  • This would be a matter of data modeling rather than a measure implementation.

    When you present the difference between two versions, you first need to be able to place the two versions to compare on your report like the image below. Thus you need to have two different columns which you can select on slicers or matrices, etc.

    slicers image

    My approach is to create a new table which duplicates each row with its successor versions. For instance, you will replicate the row of V110 into four successor rows: V111, V112, V113, and V115. In this new table, each row represents every possible pair of versions which you want to compare. Then you can calculate the difference of the two versions in each row. The new table would look like below.

    new table image

    This table could be created like below using calculated table for instance. You should also be able to do that using Power Query.

    // DAX calculated table
    // In this code snippet, the original table is referred to as "Versions".
    Version Comparison = GENERATE (
        SELECTCOLUMNS (
            Versions,
            "Version Before", [version],
            "Points Before", [count_of_points]
        ),
        SELECTCOLUMNS (
            FILTER ( Versions, Versions[version] > [Version Before] ),
            "Version After", [version],
            "Points After", [count_of_points],
            "Points Diff", [count_of_points] - [Points Before]
        )
    )
    

    Then, it might be a good idea to define a measure for the diff, which is just referring to the "Point Diff" column value only when a single pair of versions is selected.

    // DAX measure
    Diff Measure = SELECTEDVALUE ( 'Version Comparison'[Points Diff] )
    

    Output images

    card image

    matrix image