Forum Discussion

GotData's avatar
GotData
New Member
3 years ago
Solved

Calculating the Difference between columns in matrix visual

I've been stumped with the solution to this problem. I have a matrix table that produces this output.     The desired output is something like this ( figures are made up)      ...
  • v-jingzhang's avatar
    3 years ago

    Hi GotData 

     

    Without knowing how your data looks like, I assume that your data is like below in the table. 

    You need to have a period sort table in the model with an Index column in chronological order. Connect this table to the source table on Period column. Then use this table's Period column in the matrix visual and the slicer visual. 

    Then you can create measures like below

    Difference in U = 
    var _curPeriodIndex = MAX('Period Sort'[Index])
    var _prePeriodIndex = CALCULATE(MAX('Period Sort'[Index]),ALLSELECTED('Period Sort'),'Period Sort'[Index] < _curPeriodIndex)
    return
    IF(ISBLANK(_prePeriodIndex),BLANK(),SUM('Table'[Units]) - CALCULATE(SUM('Table'[Units]),ALLEXCEPT('Table','Table'[Brand]),'Period Sort'[Index] = _prePeriodIndex))
    Rank Difference = 
    var _curPeriodIndex = MAX('Period Sort'[Index])
    var _prePeriodIndex = CALCULATE(MAX('Period Sort'[Index]),ALLSELECTED('Period Sort'),'Period Sort'[Index] < _curPeriodIndex)
    return
    IF(ISBLANK(_prePeriodIndex),BLANK(),MAX('Table'[Rank]) - CALCULATE(MAX('Table'[Rank]),ALLEXCEPT('Table','Table'[Brand]),'Period Sort'[Index] = _prePeriodIndex))
    %CT Difference = 
    var _curPeriodIndex = MAX('Period Sort'[Index])
    var _prePeriodIndex = CALCULATE(MAX('Period Sort'[Index]),ALLSELECTED('Period Sort'),'Period Sort'[Index] < _curPeriodIndex)
    return
    IF(ISBLANK(_prePeriodIndex),BLANK(),SUM('Table'[%CT]) - CALCULATE(SUM('Table'[%CT]),ALLEXCEPT('Table','Table'[Brand]),'Period Sort'[Index] = _prePeriodIndex))

    The result:

    You can use the slicer to select incontinuous periods. It will show the difference between selected periods correctly. 

    The sample pbix file has been attached at bottom. Hope this helps. 

     

    Best Regards,
    Community Support Team _ Jing
    If this post helps, please Accept it as Solution to help other members find it.