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
Sandeep_PBI
Frequent Visitor

Compare Columns based on a user selection

I have a table with columns (Student ID, Category, 2017,2018,2019,2020). following is the example 

 

f.PNG

 

I have a slicer (dropdown) on the report which shows distinct years (2018,2019,2020).

 

The task I am trying to complete is to show a report with a table as following (example assuming the user selected 2018 in the slicer and the results based on the sample data above should be as following) that shows if the Student changed the selection compared to the previous year(2017 in the following case) based on the year selected from the slicer (2018 for the).

 

r.PNG

 

I have tried several ways but was not able to get the desired output. how can this be achieved using DAX?

1 ACCEPTED SOLUTION
v-easonf-msft
Community Support
Community Support

Hi , @Sandeep_PBI 

Try measure as below:

change =
VAR _id =
    SELECTEDVALUE ( Category[Student ID] )
VAR _category =
    SELECTEDVALUE ( Category[Category] )
VAR _year =
    SELECTEDVALUE ( Category[Year] )
RETURN
    IF (
        CALCULATE (
            CONCATENATEX ( Category, Category[Value] ),
            FILTER (
                ALL ( Category ),
                Category[Student ID] = _id
                    && Category[Category] = _category
                    && Category[Year] = _year - 1
            )
        )
            = SELECTEDVALUE ( Category[Value] ),
        0,
        1
    )

 

Here is a demo.

Pbix attached

 

Best Regards,
Community Support Team _ Eason
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
v-easonf-msft
Community Support
Community Support

Hi , @Sandeep_PBI 

Try measure as below:

change =
VAR _id =
    SELECTEDVALUE ( Category[Student ID] )
VAR _category =
    SELECTEDVALUE ( Category[Category] )
VAR _year =
    SELECTEDVALUE ( Category[Year] )
RETURN
    IF (
        CALCULATE (
            CONCATENATEX ( Category, Category[Value] ),
            FILTER (
                ALL ( Category ),
                Category[Student ID] = _id
                    && Category[Category] = _category
                    && Category[Year] = _year - 1
            )
        )
            = SELECTEDVALUE ( Category[Value] ),
        0,
        1
    )

 

Here is a demo.

Pbix attached

 

Best Regards,
Community Support Team _ Eason
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

rajulshah
Super User
Super User

Hello @Sandeep_PBI,

You need to unpivot the columns and then use the measure.

Please see below my M Query below:

let
    Source = *Your Source Here*,
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Student ID", Int64.Type}, {"Category", type text}, {"2017", type text}, {"2018", type text}, {"2019", type text}, {"2020", type text}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Student ID", "Category"}, "Attribute", "Value"),
    #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Year"}})
in
    #"Renamed Columns"

And then you can use the following measure:

Change = 
VAR SelectedYear = IF(NOT(ISFILTERED(Category[Year])),MAX(Category[Year]),SELECTEDVALUE(Category[Year]))
VAR ValueSelectedYear = MINX(FILTER(Category,Category[Year]=2020),Category[Value])
VAR ValuePreviousYear = CALCULATE(MIN(Category[Value]),ALL(Category[Year]),Category[Year]=SelectedYear-1)
RETURN IF(ValuePreviousYear=ValueSelectedYear,1,0)


Hope this helps.

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.