Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreGet certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now
I am trying to solve a problem. I have two tables and I need to filter for the current period value in Table 2 and divide it by the average of the filter of the first six months in Table 1.
Please see below for illutstration
Solved! Go to Solution.
This is the adjusted code:
AverageNext6Months =
var v_dates = DATESINPERIOD( DateNew[Date]; MAX( DateNew[Date] ); +5; MONTH )
return
AVERAGEX(v_dates;CALCULATE(SUM(Forecast[Forecast Value])))
Ratio:
Ratio = MAX(OH[OH Value]) / [AverageNext6Months]
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!
Hi @RDzeketey ,
If your YYYYMM is a Date Column.
You can try this.
Regards,
Harsh Nathani
Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)
Hi, @harshnathani ,
Thank you
Your method looks much simpler but the 6 month average only works for the first line (26,417). The second line 6 mo average does not compute correctly per your numbers. It should be a rolling 6 months(including current month) calculation. PLease see below.
THank you
Hi @RDzeketey ,
Made some changes and I think it shold work fine now.
Hi @RDzeketey ,
I have a question though, just a lil curious.
Rolling 6 months is calculated on average of 6 months before that particular date. But you seem to be doing it the opposite i.e calculating avg 6 months after a date. Any specific reasons.
Just curious.
Regards,
HN
Apologies, I meant forward looking. Current month plus 5. Thank you
Can you share some more info on why my method is not working? Perhaps we can make it work?
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!
Hi, @stevedep
Thank you for your help. I realized that it was partially my fault that it was showing that way because I had 5 products in the same table, so that is why my RANKING was jumping in increments of 5.
I am not able to attach the file so here is another screenshot. I got values, but they are not what I expect to get when I test it in excel.
Thank you
Van you share your excel logic? And an example with a difference?
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!
Hi,
Hope this makes more sense.
Thank you
Hi,
2 interesting findings;
1. My calculation was missing a 'VALUES' summary.
2. Your spreadsheet does not seem to calculate the average correctly? For the first 6 months it should be 3879,68?
See;
Power BI file availabe here, Excelfile available here.
Hope it helps.
Kind regards,
Steve.
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!
Thanks, @stevedep
The Average works. However, for the Ratio, the math does not work.
6,472/3,879.68 = 1.69, not 1.62.
It looks to be taking the average of the entire table 1 (3987.22); and hence 6,472/3,987.22 = 1.62
I am not sure how to fix that.
Thank you
Ah, I see now. Changed:
ratio =
var filtervar = FILTER(ALLSELECTED(Table1[YYYYMM]);[RANK] >= 1 && [RANK] <= 6)
var selectedvalue = SELECTEDVALUE(Table2[Value])
var v_avg = CALCULATE(CALCULATE(
AVERAGE(Table1[Value]));filtervar)
return
selectedvalue
/v_avg
Giving the expected result:
File available here.
Kind regards,
Steve.
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!
This is the adjusted code:
AverageNext6Months =
var v_dates = DATESINPERIOD( DateNew[Date]; MAX( DateNew[Date] ); +5; MONTH )
return
AVERAGEX(v_dates;CALCULATE(SUM(Forecast[Forecast Value])))
Ratio:
Ratio = MAX(OH[OH Value]) / [AverageNext6Months]
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!
Hi,
This should work, perhaps not the most elegant solution, but this should work.
2 steps, first a ranking on the monthnumber, second calculate the average for the next 6 months and then do the division.
rank formula:
RANK = var sv = SELECTEDVALUE(Table2[YYYYMM])
var sv_tbl1 = SELECTEDVALUE(Table1[YYYYMM])
var rankk = CALCULATE(COUNTROWS(Table1);FILTER(ALL(Table1[YYYYMM]);Table1[YYYYMM] >= sv && Table1[YYYYMM] <= sv_tbl1 ))
return
rankk
step 2, the calculation:
average =
var filtervar = FILTER(Table1;[RANK] >= 1 && [RANK] <= 6)
var selectedvalue = SELECTEDVALUE(Table2[Value])
return
selectedvalue /
CALCULATE(
AVERAGE(Table1[Value]);filtervar)
Result is as expected:
Link to the Power BI file is here.
Please accept as solution if this works for you.
Kind regards,
Steve.
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!
When I use the RANK, this is the output I get and then the next part of the formula does not produce any values.
Can you share a file or full screenshot with your selections and attributes?
Proud to be a Super User!
Awesome Keyboard Shortcusts in Power BI, thumbs up if you like the article
My Community Blog Articles (check them out!)
My Blog - Power M code to automatically detect column types -
How to create test data using DAX!
Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.
User | Count |
---|---|
23 | |
21 | |
20 | |
13 | |
12 |
User | Count |
---|---|
43 | |
31 | |
24 | |
23 | |
22 |