Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Below is a snippet of a facts table 'fTrans' containing historical purchase and sale transactions of shares of stock.
What I need is to calculate the AVERAGE of days each batch of shares was held until its respective SALE transaction took place.
As you can see by the expanded snippet below, this is quite a straightforward exercise when a particular sale transaction relates one-on-one to a prior purchase transaction.
However things get a bit more complicated as there are instances where a particular sale refers to TWO OR MORE prior purchase transactions, which I highlight some examples below.
And to complicate matters further I DO NOT want to calculate a simple straight average by just summing up the days and dividing them up by the number of occurrences.
That average calculation needs to be a weighted average using [Purchase total] as the weight basis for each transaction.
So I started constructing a couple of temporary tables and then use the second one as the source table to grab the calculated average days for each respective sale date from by writing a third and final RETURN block of the measure code.
On the intermediary table 'Supp_Tbl' I calculate the product between [Days] and [Purchase total], which will be used as the weight for each purchase transaction.
Then I use 'Supp_Tbl' to calculate the [Weighted average days] and setup the second table 'Days_Tbl' by grouping the [Product of Days and Purchase total] and the [Purchase total] by [Ticker] and [Sale date related to respective purchase] so I can divide them up and then get the result for each sale date.
And that's where I get stuck. I do have the results right in front of me and know what needs to be done on the RETURN block in order to get to the measure:
I just seem not to know how! I've tried several paths such as setting up a third temporary table, INDEX or LOOKUPVALUE but to no avail. Maybe my mind is still too stuck in Excel and prevent me from thinking the DAX way.
Any help is immensely appreciated...
Hi @leolapa_br in Row 13 in your measure you include filter criteria, to return BLANK(), so calculation is "good".
Did I answer correctly? Kudos appreciate / accept solution
Proud to be a Super User!
@some_bih both codes I shared on those two screenshots are OK, as I checked the results highlighted on the second screenshot and they are fine.
The problem is not with the code I've developed so far, since they get me 90% of the way to accomplish what I need.
My problem is getting the final 10% of the code that will get me there. I just don't know how to grab the results I calculated and get them to the rows they relate to on the fact table.
Maybe I didn't express myself right in the way I originally phrased my question and I apologize for that. I re-wrote the very end of the question to make my question/problem more clear.
User | Count |
---|---|
25 | |
12 | |
8 | |
7 | |
7 |
User | Count |
---|---|
25 | |
12 | |
11 | |
10 | |
6 |