Forum Discussion

Natty004's avatar
Natty004
Frequent Visitor
3 years ago
Solved

Calculating % increase as dollar value - DAX

Hello,

 

I'm relatively new to powerBI and I'm trying to calculate the actual $ amount of margin increase compared to the margin % last month, and I'm struggling with the DAX (Therefore as we have increase the margin %, how much increase in margin have we generated).

 

Wondering if anyone can help, I am using the following measures in a Matrix table

 

Total Revenue - Measure : TOTAL Revenue = sum(Table1[Net])

Total Margin - Measure : Total Margin = Table1[Measure : TOTAL Revenue]-Table1[Measure : Total Oncosts]

Total Margin % - Measure : Margin % = divide(Table1[Measure : Total Margin],Table1[Measure : TOTAL Revenue],0)

 

In excel I have calculated the margin $ amount to be

Current Revenue * (current month Margin % - Last months Margin %)

 

I've been trying to use previous month etc.... but its not working, can anyone help write the correct DAX? I've attached an example in excel of what I am trying to write. can anyone assist?

 

 

Current set up on my matrix is - 

 

 

That has slicers that can change the months

Many thanks in advance,

 

 

  • tamerj1's avatar
    tamerj1
    3 years ago

    Natty004 

    Please try

    Prior Month % =
    CALCULATE (
        [Measure : Margin %],
        Table1[YearMonth Sequential Number]
            = MAX ( Table1[YearMonth Sequential Number] ) - 1,
        ALL ( Table1[Year & Month] )
    )

9 Replies

  • Natty004 , To calculate this you need add correct row context , assume these are measures

     

    Sumx(Summarize(Fact, Fact[Name], Fact[Description], Date[Month Year], "_1" , [Current Revenue] * ([current month Margin %] - [Last months Margin %]) ) ), [_1])

     

    Sumx(Summarize(Fact, Fact[Name], Fact[Description], Date[Month Year], "_1" , [Current Revenue] * ([current month Margin %] - [Last months Margin %]) ), [_1])

     

     

    For this month last month you can use TI with date table

     

    example

     

    MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))
    last MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
    last month Sales = CALCULATE(SUM(Sales[Sales Amount]),previousmonth('Date'[Date]))

     

     

    Power BI — Month on Month with or Without Time Intelligence
    https://medium.com/@amitchandak.1978/power-bi-mtd-questions-time-intelligence-3-5-64b0b4a4090e
    https://www.youtube.com/watch?v=6LUBbvcxtKA

    • Natty004's avatar
      Natty004
      Frequent Visitor

      Hello amitchandak,

       

      Thank you for your response, apologies I couldn't work out how to apply your example to a margin measure.

       

      How do I calculate the current month and previous month margin %  when the margin measure is:

       

       Measure : Margin % = divide(Table1[Measure : Total Margin],Table1[Measure : TOTAL Revenue],0) 

       

       

      These are my measures that I have added:

       

       MTD Margin % = calculate(DIVIDE(Table1[Measure : Total Margin], Table1[Measure : TOTAL Revenue]),DATESMTD(Dates[Date])) 

       

      Last MTD margin =

      CALCULATE (

      DIVIDE ( Table1[Measure : Total Margin], Table1[Measure : TOTAL Revenue] ),

      DATESMTD ( DATEADD ( Dates[Date], -1, MONTH ) )

       

       

      Previous month Margin % =

      CALCULATE (

      DIVIDE ( Table1[Measure : Total Margin], Table1[Measure : TOTAL Revenue] ), PREVIOUSMONTH(Dates[Date])) 

       

       

      I'm not getting any results return from the above measures.

       

      Apologies for the confusion, any assistance greatly appreciated.

       

      many thanks

      Nat

       

       

       

       

  • tamerj1's avatar
    tamerj1
    Community Champion

    Hi Natty004 
    First create the year month sequential number number (Calculated Column)

    YearMonth Sequential Number =
    RANKX (
        'Date',
        YEAR ( 'Date'[Date] ) * 100
            + MONTH ( 'Date'[Date] ),
        ,
        ASC,
        DENSE
    )

    Then the required measure would be

    $ MArgine Increase =
    VAR CurrentMonth =
        MAX ( 'Date'[YearMonth Sequential Number] )
    VAR CurrentMonthRevenue = [TOTAL Revenue]
    VAR CurrentMonthMarginPercent = [Margin %]
    VAR PreviousMonthMarginPercent =
        CALCULATE ( [Margin %], 'Date'[YearMonth Sequential Number] = CurrentMonth - 1 )
    RETURN
        CurrentMonthRevenue * ( CurrentMonthMarginPercent - PreviousMonthMarginPercent )
    • Natty004's avatar
      Natty004
      Frequent Visitor

      Hello tamerj1

      Thank you for your response, I have updated the following however I the $ increase value is the same as the $ margin value. 

       

      Here are my updates:

       

       

       

      YearMonth Sequential Number = 
      RANKX (
          'Table1',
          YEAR ('Table1'[Dt Invoice]) * 100
              + MONTH ( 'Table1'[Dt Invoice] ),
          ,
          ASC,
          DENSE
      )

       

       

       

      Measure

       

      $ MArgine Increase = 
      VAR CurrentMonth =
          MAX ( Table1[YearMonth Sequential Number] )
      VAR CurrentMonthRevenue = 'Table1'[Measure : TOTAL Revenue]
      VAR CurrentMonthMarginPercent = [Measure : Margin %]
      VAR PreviousMonthMarginPercent =
      CALCULATE([Measure : Margin %],Table1[YearMonth Sequential Number]=CurrentMonth-1)
      RETURN
          CurrentMonthRevenue * ( CurrentMonthMarginPercent - PreviousMonthMarginPercent )

       

      The value that returns is the same as the margin $ amount

       

       

      Measure : Total Margin = Table1[Measure : TOTAL Revenue]-Table1[Measure : Total Oncosts]

       

       

      I have tried to calculate the current Margin % and the pror month margin %, however the results when I am pulling in the Prior month % is the same as current month

       

       

      Current Month % = CALCULATE(
      divide(Table1[Measure : Total Margin],
      Table1[Measure : TOTAL Revenue],0), 
      Table1[YearMonth Sequential Number])

       

       

       

       

      Prior Month % = 
      VAR CurrentMonthMargin = CALCULATE([Measure : Margin %], Table1[YearMonth Sequential Number])
      VAR PriorMonthMargin = CALCULATE([Measure : Margin %], Table1[YearMonth Sequential Number] -1) 
      RETURN
      PriorMonthMargin

       

       

      Then I tried to write the following however it did not return any results

       

       

      $ margin increase = 
      SUMX (
          Table1,
          [Measure : TOTAL Revenue]
              * (
                  [Measure : Margin %]
                      - ( CALCULATE ( [Measure : Margin %], Table1[YearMonth Sequential Number] - 1 ) )
              ))

       

       Apologies for the multiple screen shots, are you able to see where the fault is. I can't seem to increase the dollar amount that has been increased.

       

      Any assitance would be greatly appreciated.

       

      Thank you in advance,

       

      Nat

    • Natty004's avatar
      Natty004
      Frequent Visitor

      Hello tamerji,

       

      Thanks for your response. I have added the above calculations and measures however the

       

      $ margin increase amount is the same value as margin amount.

       

      See below:

       

      YearMonth Sequential Number = 
      RANKX (
          'Table1',
          YEAR ('Table1'[Dt Invoice]) * 100
              + MONTH ( 'Table1'[Dt Invoice] ),
          ,
          ASC,
          DENSE
      )

       

      Measure

       

      $ MArgine Increase = 
      VAR CurrentMonth =
          MAX ( Table1[YearMonth Sequential Number] )
      VAR CurrentMonthRevenue = 'Table1'[Measure : TOTAL Revenue]
      VAR CurrentMonthMarginPercent = [Measure : Margin %]
      VAR PreviousMonthMarginPercent =
      CALCULATE([Measure : Margin %],Table1[YearMonth Sequential Number]=CurrentMonth-1)
      RETURN
          CurrentMonthRevenue * ( CurrentMonthMarginPercent - PreviousMonthMarginPercent )

       

      Margin $ measure

       

      Measure : Total Margin = Table1[Measure : TOTAL Revenue]-Table1[Measure : Total Oncosts]

       

      I then tried to calculate current margin % and Prior month Matgin % and add these to the matrix to see if they are calculating correctly.

       

      The current margin % measure below matches the same calculation [Measure : Margin %]

       

      Current Month % = CALCULATE(
      divide(Table1[Measure : Total Margin],
      Table1[Measure : TOTAL Revenue],0), 
      Table1[YearMonth Sequential Number])

       Prior month % - does not pull through the previous month - it just duplicates the current month %

       

      Prior Month % = 
      VAR CurrentMonthMargin = CALCULATE([Measure : Margin %], Table1[YearMonth Sequential Number])
      VAR PriorMonthMargin = CALCULATE([Measure : Margin %], Table1[YearMonth Sequential Number] -1) 
      RETURN
      PriorMonthMargin

       

      Apologies in advance, are you able to identfy where I am going wrong?

       

      many thanks in advance

      • tamerj1's avatar
        tamerj1
        Community Champion

        Natty004 

        Please try

        Prior Month % =
        CALCULATE (
            [Measure : Margin %],
            Table1[YearMonth Sequential Number]
                = MAX ( Table1[YearMonth Sequential Number] ) - 1,
            ALL ( Table1[Year & Month] )
        )