Forum Discussion

thedataprofile's avatar
thedataprofile
New Member
2 years ago
Solved

Display Current Month/Year and Previous Month/Year counts based on slicer.

Hi ! 

 

I want to build a barchart so when I select one date in a slicer, Say Jan-22, the barchart would display the current month/year and the previous month/year.  In this example I'm selecting both dates, but I want to select only one date (Jan 2022) and still have Jan 2021 displaying in the barchart, If I selected Feb 2022 in the slicer, that also Feb 2021 is displayed. 

 

This is an example of the data 

DateMonthScoreStreamYearSubjectYear
1/1/2020 0:0080A2020English2020
1/1/2020 0:0060A2020Maths2020
1/2/2020 0:0080A2020English2020
1/2/2020 0:0060A2020Maths2020
1/3/2020 0:0080A2020English2020
1/3/2020 0:0060A2020Maths2020
1/4/2020 0:0080A2020English2020
1/4/2020 0:0070A2020Maths2020
1/5/2020 0:0080A2020English2020
1/5/2020 0:0060A2020Maths2020
1/6/2020 0:0080A2020English2020
1/6/2020 0:0060A2020Maths2020
1/7/2020 0:0080A2020English2020
1/7/2020 0:0060A2020Maths2020
1/8/2020 0:0080A2020English2020
1/8/2020 0:0070A2020Maths2020
1/9/2020 0:0080A2020English2020
1/9/2020 0:0070A2020Maths2020
1/10/2020 0:0080A2020English2020
1/10/2020 0:0070A2020Maths2020
1/11/2020 0:0080A2020English2020
1/11/2020 0:0050A2020Maths2020
1/12/2020 0:0080A2020English2020
1/12/2020 0:0060A2020Maths2020
1/1/2021 0:0040A2021English2021
1/1/2021 0:0040A2021Maths2021
1/2/2021 0:0040A2021English2021
1/2/2021 0:0050A2021Maths2021
1/3/2021 0:0040A2021English2021
1/3/2021 0:0090A2021Maths2021
1/4/2021 0:0040A2021English2021
1/4/2021 0:0090A2021Maths2021
1/5/2021 0:0060A2021English2021
1/5/2021 0:0090A2021Maths2021
1/6/2021 0:0070A2021English2021
1/6/2021 0:0089A2021Maths2021
1/7/2021 0:0070A2021English2021
1/7/2021 0:0078A2021Maths2021
1/8/2021 0:0060A2021English2021
1/8/2021 0:0067A2021Maths2021
1/9/2021 0:0060A2021English2021
1/9/2021 0:0090A2021Maths2021
1/10/2021 0:0070A2021English2021
1/10/2021 0:0090A2021Maths2021
1/11/2021 0:0070A2021English2021
1/11/2021 0:0090A2021Maths2021
1/12/2021 0:0070A2021English2021
1/12/2021 0:0090A2021Maths2021
1/1/2022 0:0090A2022English2022
1/1/2022 0:0080A2022Maths2022
1/2/2022 0:0090A2022English2022
1/2/2022 0:0080A2022Maths2022
1/3/2022 0:0090A2022English2022
1/3/2022 0:0080A2022Maths2022
1/4/2022 0:0090A2022English2022
1/4/2022 0:0080A2022Maths2022
1/5/2022 0:0090A2022English2022
1/5/2022 0:0080A2022Maths2022
1/6/2022 0:0090A2022English2022
1/6/2022 0:0079A2022Maths2022
1/7/2022 0:0090A2022English2022
1/7/2022 0:0085A2022Maths2022
1/8/2022 0:0090A2022English2022
1/8/2022 0:0086A2022Maths2022
1/9/2022 0:0085A2022English2022
1/9/2022 0:0088A2022Maths2022
1/10/2022 0:0085A2022English2022
1/10/2022 0:0090A2022Maths2022
1/11/2022 0:0084A2022English2022
1/11/2022 0:0090A2022Maths2022
1/12/2022 0:0094A2022English2022
1/12/2022 0:0090A2022Maths2022

 

And I have a date table as shown: 

 

 

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi,thedataprofile 

    Regarding the issue you raised, my solution is as follows:

    1.First I created the following table based on your description:

    2.Then create a calculated table that does not have a relationship with any other table to ensure that the slicer does not affect the source data, and then use the newly created calculated column as the slicer:

     

    Table 1 = CALENDAR(MIN('Table'[DateMonth]),MAX('Table'[DateMonth] ))

     

    3. Then create the following measure swapping it with the SCORE column in the visualization chart:

     

    Measure = 
    VAR __slicer_Year = YEAR(MAX('Table 1'[Table_DateMonth]))
    VAR __Slicer_Month = MONTH(MAX('Table 1'[Table_DateMonth]))
    RETURN 
    IF(MONTH(MAX('Table'[DateMonth]))=__Slicer_Month&&YEAR(MAX('Table'[DateMonth]))IN{__slicer_Year,__slicer_Year-1},AVERAGE('Table'[Score]))
    

     

    4.Here's my final result, which I hope meets your requirements.

    Can you share sample data and sample output in tabular format if I am misunderstanding? Or a sample pbix after removing sensitive data. We can better understand the problem and help you.

    Best Regards,

    Leroy Lu

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

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi,thedataprofile 

    Regarding the issue you raised, my solution is as follows:

    1.First I created the following table based on your description:

    2.Then create a calculated table that does not have a relationship with any other table to ensure that the slicer does not affect the source data, and then use the newly created calculated column as the slicer:

     

    Table 1 = CALENDAR(MIN('Table'[DateMonth]),MAX('Table'[DateMonth] ))

     

    3. Then create the following measure swapping it with the SCORE column in the visualization chart:

     

    Measure = 
    VAR __slicer_Year = YEAR(MAX('Table 1'[Table_DateMonth]))
    VAR __Slicer_Month = MONTH(MAX('Table 1'[Table_DateMonth]))
    RETURN 
    IF(MONTH(MAX('Table'[DateMonth]))=__Slicer_Month&&YEAR(MAX('Table'[DateMonth]))IN{__slicer_Year,__slicer_Year-1},AVERAGE('Table'[Score]))
    

     

    4.Here's my final result, which I hope meets your requirements.

    Can you share sample data and sample output in tabular format if I am misunderstanding? Or a sample pbix after removing sensitive data. We can better understand the problem and help you.

    Best Regards,

    Leroy Lu

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