Forum Discussion

srk_powerbi's avatar
srk_powerbi
Icon for Helper II rankHelper II
5 years ago
Solved

calculate measure help..

hi ,

i have a table like below. I want to calculate a measure base on current month. Lets say we are in june month now, so I want to calcuate  a measure called '% expected' by using this logic

% cexpected = ( ( sum(Amount) where CalcType = sales ) -  ( sum(Amount) where CalcType = sales expected ))/ ( sum(Amount) where CalcType = sales )

 

for the calculation only consider the months  Jan to May because current month is June.  If we are in next month July, i want this to be calculated for months 'Jan- June' , and so on dynamically. Can anyone suggest please.

 

v-luwang-msft Jihwan_Kim  parry2k v-deddai1-msft Greg_Deckler 

 

CustomerProduct   FyFiscalMonthYear    Month              CalcType                                      Amount
Amazonxyz2021202101  Jansales             4000
Amazonxyz2021202102   febsales             1400
Amazonxyz2021202103 Marsales             850
Amazonxyz2021202104 Aprsales           2100
Amazonxyz2021202105Maysales          2300
Amazonxyz2021202106Junsales          4270
Amazonxyz2021202107Julsales2320
Amazonxyz2021202108Augsales3220
Amazonxyz2021202109Sepsales2100
Amazonxyz2021202110Octsales   900
Amazonxyz2021202111Novsales  5300
Amazonxyz2021202112Decsales  7200
Amazonxyz2021202101Jansales expected6500
Amazonxyz2021202102febsales expected200
Amazonxyz2021202103Marsales expected3000
Amazonxyz2021202104Aprsales expected2800
Amazonxyz2021202105Maysales expected1200
Amazonxyz2021202106Junsales expected2500
Amazonxyz2021202107Julsales expected850
Amazonxyz2021202108Augsales expected1500
Amazonxyz2021202109Sepsales expected4000
Amazonxyz2021202110Octsales expected2300
Amazonxyz2021202111Novsales expected8500
Amazonxyz2021202112Decsales expected6500
  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi srk_powerbi ,

     

    Based on your sample data, you can create the following measure. My measure is updated dynamically, no slicer selection is required.

     

     

    % cexpected = 
    var _aa=CALCULATE(SUM('Table'[Amount]),FILTER('Table',[CalcType]="sales"&&[Fy]<=YEAR(TODAY())&&[Fm]<=MONTH(TODAY())))
    var _bb=CALCULATE(SUM('Table'[Amount]),FILTER('Table',[CalcType]="sales expected"&&[Fy]<=YEAR(TODAY())&&[Fm]<=MONTH(TODAY())))
    return DIVIDE(_aa-_bb,_aa)
    

     

     

    Tips: [Fm] column is a calculated column which retruns the month number.

    Best Regards,

    Stephen Tao

     

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

     

6 Replies

  • srk_powerbi , In case you have date or can create a date then you can use time intelligence with date table

     

    example

    calculate([% cexpected], datesmtd('Date'[Date])

     

    else create a separate table with year month  (say date) 

    add rank column

    Month Rank = RANKX(all('Date'),'Date'[year Month],,ASC,Dense)

     

    create measures
    This Month = CALCULATE([% cexpected], FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])))
    Last Month = CALCULATE([% cexpected], FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])-1))

    • srk_powerbi's avatar
      srk_powerbi
      Icon for Helper II rankHelper II

      thanks for reply.  here I need to calculate for all the months prior to current month dynamically.

  • srk_powerbi although Ashish_Mathur  solution will work in that case you have to select a period. if your ask is to always calculate % until previous month without selecting a month, add this measure (it is based on the file that Ashish_Mathur  shared in his response)

     

    % Expected until Last Month = 
    VAR __date = EOMONTH ( TODAY(), -1 )
    RETURN
    CALCULATE ( DIVIDE ( [Numerator], [Sales YTD] ), 'Calendar'[Date] <= __date ) 

     

    Check my latest blog post Comparing Selected Client With Other Top N Clients | PeryTUS  I would ❤ Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

    Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi srk_powerbi ,

     

    Based on your sample data, you can create the following measure. My measure is updated dynamically, no slicer selection is required.

     

     

    % cexpected = 
    var _aa=CALCULATE(SUM('Table'[Amount]),FILTER('Table',[CalcType]="sales"&&[Fy]<=YEAR(TODAY())&&[Fm]<=MONTH(TODAY())))
    var _bb=CALCULATE(SUM('Table'[Amount]),FILTER('Table',[CalcType]="sales expected"&&[Fy]<=YEAR(TODAY())&&[Fm]<=MONTH(TODAY())))
    return DIVIDE(_aa-_bb,_aa)
    

     

     

    Tips: [Fm] column is a calculated column which retruns the month number.

    Best Regards,

    Stephen Tao

     

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