Forum Discussion

rashidanwar's avatar
rashidanwar
Icon for Advocate II rankAdvocate II
1 year ago
Solved

Adding comparative rows at the bottom of Matrix Visual

Hello Community, I have a following Matrix Visual in Power BI, and I want to add two rows at bottom of it. It should be the difference between 2022/2023 and 2023/2024. Year NOV DEC JAN FEB...
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi rashidanwar ,

    Please follow these steps:

    1.Use Power Query to create a blank query and perform an "append" operation

    let
        Source = Table,
        selectColumn = Table.SelectColumns(Source,{"Month"}),
        distinct = Table.Distinct(selectColumn),
        #"Added Custom" = Table.AddColumn(distinct, "Year", each "Diff from last year")
    in
        #"Added Custom"

    2.Use the following DAX expression to create a measure

    MEASURE =
    VAR _row =
        SELECTEDVALUE ( Append1[Year] )
    VAR _column =
        SELECTEDVALUE ( Append1[Month] )
    VAR _difference =
        CALCULATE ( SUM ( Append1[Value] ), 'Append1'[Year] = "2023/2024" )
            - CALCULATE ( SUM ( Append1[Value] ), 'Append1'[Year] = "2022/2023" )
    VAR _result =
        IF (
            _row = "Diff from last year",
            FORMAT ( _difference, "0" ),
            IF (
                ISBLANK ( _row ),
                FORMAT (
                    DIVIDE (
                        _difference,
                        CALCULATE ( SUM ( Append1[Value] ), 'Append1'[Year] = "2022/2023" )
                    ),
                    "0%"
                ),
                FORMAT ( SUM ( 'Append1'[Value] ), "0" )
            )
        )
    RETURN
        _result

    3.Final output

     

     

    Best Regards,
    Wenbin Zhou

  • sanalytics's avatar
    1 year ago

    rashidanwar 

     

    I know this is late.  But you can have a short look on my solution as well.
    Assuming your dataset look like below

    then follow below steps

    Step 1 : 
    create a simple disconnected calculated table by DAX .

    ResultTable = 
    VAR _1 = 
    ROW( "Year","Diff from last Year" )
    VAR _2 =
    ROW( "Year","Diff%" )
    VAR _CombTable = 
    UNION(
    	VALUES( Source[Year] ),
    	_1,_2
    )
    VAR _Result = 
    ADDCOLUMNS(
        _CombTable, "MaxYear",MAX( Source[Year] )
    )
    
    RETURN
    _Result

    Step 2 : Create a consolidated DAX for showing value. Below code. (Assuming you have DAX code for Diff from Last year and Diff %)

    Show Value = 
    VAR _ValueBase = 
    CALCULATE(
        SUM( Source[Value] ),
        TREATAS(
            VALUES( 'ResultTable'[Year] ), Source[Year] 
        ) )
    VAR _Result = 
    IF(
        MAX( ResultTable[Year] ) = "Diff from last Year",
          CALCULATE(
        [Diff from Last Year],
        TREATAS(
            VALUES( 'ResultTable'[MaxYear] )
            ,Source[Year]
        )
    ) ,
    IF(
        MAX( ResultTable[Year] ) = "Diff%",
           CALCULATE(
        [Diff %],
        TREATAS(
            VALUES( 'ResultTable'[MaxYear] )
            ,Source[Year]
        )
    ),
    _ValueBase
    ) )
    RETURN
    _Result


    Step 3 : For formatting, go ti Dynamic formatting  and paste below DAX 

    IF(
        MAX( ResultTable[Year] ) = "Diff%",
        "0%","#,##0"
    )

     

    Below screenshot

     you can find the pbix file in below link

    https://we.tl/t-ll7HvM8Gz7

     

    Hope this will help

     

    Regards,

    sanalytics