Forum Discussion

gomezc73's avatar
gomezc73
Helper V
3 years ago
Solved

Create a comparative Matrix with data by year filtered with two slicers

Hi,

 

  I am having trouble trying to create a matrix with the data filtered by two slicers (to compare the data by year).

 

  In other words, my data have a monthly summary of sales by product/year.

  

FamilyDescriptionCodeDescriptionYearMonthAmount
A01Bicycles 10 inchesA01-001Bicycles sport Red202112300
A01Bicycles 10 inchesA01-001Bicycles sport Red202125000
A01Bicycles 10 inchesA01-001Bicycles sport Red202135050
A01Bicycles 10 inchesA01-001Bicycles sport Red202145100
A01Bicycles 10 inchesA01-001Bicycles sport Red202155250
A01Bicycles 10 inchesA01-001Bicycles sport Red202165300
A01Bicycles 10 inchesA01-001Bicycles sport Red202175350
A01Bicycles 10 inchesA01-001Bicycles sport Red202185400
A01Bicycles 10 inchesA01-001Bicycles sport Red202196300
A01Bicycles 10 inchesA01-001Bicycles sport Red2021106350
A01Bicycles 10 inchesA01-001Bicycles sport Red2021116400
A01Bicycles 10 inchesA01-001Bicycles sport Red2021126450
A01Bicycles 10 inchesA01-001Bicycles sport Red202216500
A01Bicycles 10 inchesA01-001Bicycles sport Red202226550
A01Bicycles 10 inchesA01-001Bicycles sport Red202236000
A01Bicycles 10 inchesA01-001Bicycles sport Red202247300
. . .. . .. . .. . .. . .. . .. . .
A01Bicycles 10 inchesA01-001Bicycles sport Red202338900
A01Bicycles 10 inchesA01-002Bicycles sport Black202112300
A01Bicycles 10 inchesA01-002Bicycles sport Black202125000
. . .. . .. . .. . .. . .. . .. . .
A01Bicycles 10 inchesA01-002Bicycles sport Black202326000
A01Bicycles 10 inchesA01-002Bicycles sport Black202337300
A02Bicycles 12 inchesA02-001Bicycles sport Red202113000
A02Bicycles 12 inchesA02-001Bicycles sport Red202123450
. . .. . .. . .. . .. . .. . .. . .
A02Bicycles 12 inchesA02-001Bicycles sport Red202325600
A02Bicycles 12 inchesA02-001Bicycles sport Red202335666
A02Bicycles 12 inchesA02-002Bicycles sport Black202112300
A02Bicycles 12 inchesA02-002Bicycles sport Black202125000
. . .. . .. . .. . .. . .. . .. . .
A02Bicycles 12 inchesA02-002Bicycles sport Black202325466
A02Bicycles 12 inchesA02-002Bicycles sport Black202336000

  

I have a slicer to select a first year to compare and another slicer to select the second year to compare (This second slicer has a disconnected table of years and i used the command USERELATIONSHIP).

 

It works fine to create a BAR CHART, but now the user needs a matrix to compare both years values by month and calculate a variation

 

Something like this:

First year selection: 2021

Second year selection: 2022

 

Result:

    

 JanFebMarapr
20219900184505050….
2022207221226000….
VARIATION782816328-950 

 

 Is it Possible??.

 

I tried using the command inscope, but i can show only 1 year.

 

Thank you in advance

 

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi gomezc73 ,

     

    I suggest you to inactive all relationships betwen two dimyear tables and your data table.

    Data model:

    Measure:

    Measure = 
    VAR _RecentYear =
        SELECTEDVALUE ( 'Year'[Year 1] )
    VAR _PreviousYear =
        SELECTEDVALUE ( 'Year 2'[Year 2] )
    VAR _Amount =
        CALCULATE (
            SUM ( 'Table'[Amount] ),
            FILTER ( 'Table', 'Table'[Year] IN { _PreviousYear, _RecentYear } )
        )
    VAR _Part1 =
        CALCULATE (
            SUM ( 'Table'[Amount] ),
            FILTER ( ALLEXCEPT ( 'Table', 'Table'[Month] ), 'Table'[Year] = _RecentYear )
        )
    VAR _Part2 =
        CALCULATE (
            SUM ( 'Table'[Amount] ),
            FILTER ( ALLEXCEPT ( 'Table', 'Table'[Month] ), 'Table'[Year] = _PreviousYear )
        )
    RETURN
        IF ( HASONEVALUE ( 'Table'[Year] ), _Amount, _Part2 - _Part1 )

    Result is as below.

     

    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.

     

2 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi gomezc73 ,

     

    I suggest you to inactive all relationships betwen two dimyear tables and your data table.

    Data model:

    Measure:

    Measure = 
    VAR _RecentYear =
        SELECTEDVALUE ( 'Year'[Year 1] )
    VAR _PreviousYear =
        SELECTEDVALUE ( 'Year 2'[Year 2] )
    VAR _Amount =
        CALCULATE (
            SUM ( 'Table'[Amount] ),
            FILTER ( 'Table', 'Table'[Year] IN { _PreviousYear, _RecentYear } )
        )
    VAR _Part1 =
        CALCULATE (
            SUM ( 'Table'[Amount] ),
            FILTER ( ALLEXCEPT ( 'Table', 'Table'[Month] ), 'Table'[Year] = _RecentYear )
        )
    VAR _Part2 =
        CALCULATE (
            SUM ( 'Table'[Amount] ),
            FILTER ( ALLEXCEPT ( 'Table', 'Table'[Month] ), 'Table'[Year] = _PreviousYear )
        )
    RETURN
        IF ( HASONEVALUE ( 'Table'[Year] ), _Amount, _Part2 - _Part1 )

    Result is as below.

     

    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.

     

    • gomezc73's avatar
      gomezc73
      Helper V

      It worked fine. thank you very much!!. the only change i did was change hasonevalue by Inscope and worked perfect. I really appreciate your help